2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
This article will start with the concepts related to trees and binary trees to help us further study binary trees.
🌈个人主页:It's the waiter.
🌈C语言笔记专栏:C language notes
🌈C++笔记专栏: C++ Notes
🌈初阶数据结构笔记专栏: Beginner Data Structure Notes
🌈喜欢的诗句:无人扶我青云志 我自踏雪至山巅
A tree is a nonlinear data structure consisting ofn(n>=0)
A finite number of nodes form a hierarchical set, however, the tree is not very valuable in practice, but the binary tree is of great practical value (the reason why this set is called a tree is that it has its roots facing upwards and its leaves facing downwards, which looks very much like a tree)
M(M>0)
mutually disjoint setsT1、T2、....、Tm
, where each setTi(1<=i<=m)
This is another subtree with a structure similar to a tree. The root node of each subtree has one and only one predecessor and can have zero or more successors.Since the tree structure is more complex than the linear table, the storage method is also more complicated. It is necessary to save both the value range and the relationship between nodes.
Here are some methods based on previous knowledge:
typedef int DataType;
struct Node
{
struct Node* _firstChild1; // 第一个孩子结点
struct Node* _pNextBrother; // 指向其下一个兄弟结点
DataType _data; // 结点中的数据域
};
Of course, it is not limited to the above methods. There are also parent representation, child representation, child-parent representation, and child-sibling representation. Here we will briefly understand the most commonly used ones.Child sibling representation
A binary tree is a finite set of nodes. There are two possible cases:
Two conclusions can be drawn from the figure:
There is no node with degree greater than 2 in a binary tree.
The subtrees of a binary tree are divided into left and right, and the order cannot be reversed, so the binary tree is an ordered tree
Notice:For any binary tree, it is composed of the following situations (The empty tree case is the easiest to forget)
In brief summary:
Every level of a full binary tree is full
If the height of a complete binary tree is n, then the first n-1 layers are full, but the last layer is not necessarily full.But it must be continuous from left to right
A complete binary tree is a very efficient data structure. A full binary tree is a special type of complete binary tree.
Necessary and sufficient conditions for a full binary tree to be a complete binary tree
This is an ordinary binary tree, which is not continuous from left to right.
Binary trees can generally be stored in two structures: a sequential structure and a chain structure.
Sequential structure storage is to use arrays to store,Generally, arrays are only suitable for representing complete binary trees., because if the binary tree is not full, there will be a waste of space. In reality, only heaps use arrays to store data. Binary tree sequential storage is physically an array and logically a binary tree. In the following questions, we need to solve a problem based on the combination of the physical array and the logical binary tree.
leftchild = parent * 2 + 1;
rightchild = paretn * 2 +2;
parent = (child - 1) / 2;
(Does not distinguish between left and right children)
Regarding the third point, based on my personal reasoning,leftchild
Subscript split intoleftchild- 1
and1
,forleftchild-1
forparent
Subscript twice, for(child - 1) / 2
The operation willleftchild
Disassemble it into1
The integer of the part divided by 2 is 0.leftchild -1
Part of it can be seen asleftchild
,andrightchild与leftchild相差1
,becauserightchild = leftchild - 1
and through the aboveleftchild - 1 ~= leftchild
, it can be inferred thatrightchild = leftchild(在进行/2运算,取整数情况下)
The chain storage structure of a binary tree means that a binary tree is represented by a linked list, that is, a chain is used to indicate the logical relationship of elements. The usual method is that each node in the linked list consists of three fields, the data field and the left and right pointer fields. The left and right pointers are used to give the storage address of the link point where the left child and right child of the node are located. The chain structure is divided into binary chain and ternary chain. Currently, we are generally learning binary chains. Later, we will learn higher-order data structures such as red-black trees and use ternary chains.
typedef int BTDataType;
// 二叉链
struct BinaryTreeNode
{
struct BinTreeNode* _pLeft; // 指向当前节点左孩子
struct BinTreeNode* _pRight; // 指向当前节点右孩子
BTDataType _data; // 当前节点值域
}
// 三叉链
struct BinaryTreeNode
{
struct BinTreeNode* _pParent; // 指向当前节点的双亲
struct BinTreeNode* _pLeft; // 指向当前节点左孩子
struct BinTreeNode* _pRight; // 指向当前节点右孩子
BTDataType _data; // 当前节点值域
};
Sequential structure storage is stored through arrays.Generally speaking, arrays are only suitable for complete binary trees. Incomplete binary trees are not suitable for array structure storage. Ordinary binary trees are only suitable for chain structure storage.However, in reality, only when a heap is used will an array be used for storage, and most of the storage is still done through a linked structure.
the reason is:
The above is all the content of this article. Thank you for watching! Here are the notes on the entry-level data structure of the waiter. I hope it will be helpful for you in learning the entry-level data structure!