The concept and structure of tree and binary tree

Table of contents

1. Tree concept and structure

1.1 The concept of a tree

1.2 Related concepts of trees

1.3 Tree Representation

1.4 Application of trees in practice

2. Concept and structure of binary tree

2.1 Concept

2.2 Special Binary Trees

2.3 Properties of Binary Trees

2.4 Storage structure of binary tree


A dream is what you want to do in your dream, and try to realize it when you wake up.


1. Tree concept and structure

1.1 The concept of a tree

A tree is a non-linear data structure, which is a set of hierarchical relationships composed of n (n>=0) finite nodes. It is called a tree because it looks like an upside-down tree, that is, with the roots pointing up and the leaves pointing down .
(1) There is a special node called the root node , and the root node has no predecessor nodes
(2) Except the root node, other nodes are divided into M (M>0) disjoint sets T1 , T2 , ... , Tm , each of which Ti (1<= i <= m) is a A subtree whose structure is similar to a tree. The root node of each subtree has one and only one predecessor, and can have 0 or more successors
(3) Therefore, the tree is defined recursively .
3b593ead410f439aa4939ef1b08bbf29.png
 

 Note: (1) In the tree structure, there can be no intersection between subtrees , otherwise it is not a tree structure. [For example, in the above picture, E and F cannot be connected] (2) The subtrees are disjoint (3) Except for the root node, each node has and only one parent node (4) A tree with N nodes The tree has N-1 edges.

1.2 Related concepts of trees

21ef46ae12934d299f8e6707dcccc739.png

Degree of node : The number of subtrees contained in a node is called the degree of the node; as shown in the figure above: A is 6
Leaf node or terminal node : a node with a degree of 0 is called a leaf node; as shown in the figure above: B, C, H, I... and other nodes are leaf nodes
Non-terminal nodes or branch nodes : nodes whose degree is not 0; as shown in the above figure: nodes such as D, E, F, G... are branch nodes
Parent node or parent node : If a node contains child nodes, this node is called the parent node of its child nodes; as shown above: A is the parent node of B
Child node or child node : the root node of the subtree contained in a node is called the child node of the node; as shown above: B is the child node of A
Brother nodes : nodes with the same parent node are called brother nodes; as shown above: B and C are brother nodes
Degree of the tree : In a tree, the degree of the largest node is called the degree of the tree; as shown above: the degree of the tree is 6
The level of nodes : starting from the definition of the root, the root is the first level, the child nodes of the root are the second level, and so on;
Tree height or depth : the maximum level of nodes in the tree; as shown above: the height of the tree is 4
Cousin nodes : nodes whose parents are on the same layer are cousins; as shown in the figure above: H and I are sibling nodes
Ancestors of a node : all nodes on the branch from the root to the node; as shown in the figure above: A is the ancestor of all nodes
Descendants : Any node in the subtree rooted at a node is called a descendant of the node. As shown above: all nodes are descendants of A
Forest : A collection of m (m>0) disjoint trees is called a forest
The nearest common ancestor of P and Q is J; the nearest common ancestor of K and F is F. The ancestors of Q can be considered to contain Q.

1.3 Tree Representation

The tree structure is more complicated than the linear table, and it is more troublesome to store and express. Since the value range is saved, the relationship between nodes and nodes is also saved . In practice, there are many ways to represent trees, such as: parent representation , child representation, child parent representation, and child sibling representation. Here we simply understand the most commonly used child brother notation .

 code show as below:

//孩子兄弟表示法
struct TreeNode
{
	struct Node* firstChild1; // 第一个孩子结点
	struct Node* pNextBrother;//下一个兄弟
	DataType data;
};

 d7b9596265404a6c88915cadc69f27f5.png

 If there is no next sibling or child, it is NULL. Looking horizontally, it looks like a singly linked list.

1.4 Application of trees in practice

Represents the directory tree structure of the file system

2. Concept and structure of binary tree

2.1 Concept

A binary tree is a finite set of nodes that:
1. or empty
2. It consists of a root node plus two binary trees called left subtree and right subtree
 

225d219012254e999a3d61c6af3776b3.png

1. There is no node with degree greater than 2 in the binary tree
2. The subtrees of the binary tree are divided into left and right, and the order cannot be reversed, so the binary tree is an ordered tree
Note: For any binary tree, it is composed of the following situations:
a7a0f543d7a64f75b7aa2c88e531b9c4.png

2.2 Special Binary Trees

1. Full binary tree : a binary tree, if the number of nodes in each layer reaches the maximum value, then this binary tree is a full binary tree. That is to say, if a binary tree has K layers and the total number of nodes is 2^k-1, then it is a full binary tree.
2. Complete binary tree : A complete binary tree is a very efficient data structure, and a complete binary tree is derived from a full binary tree. For a binary tree with a depth of K and n nodes, it is called a complete binary tree if and only if each of its nodes has a one-to-one correspondence with the nodes numbered from 1 to n in the full binary tree with a depth of K. It should be noted that a full binary tree is a special kind of complete binary tree. [The first N-1 layers are full, the last layer is not full, but the last layer is continuous from left to right]

4d649c3f0e664954a00c6f0c516561b5.png

(1) Arrays can be used to store binary trees. [Put the tree level by level into an array] [Physical structure: how to actually store it in memory. Logical structure: virtual, made us imagine]

(2) The formula for calculating the parent-child subscript relationship:

leftchild = parent*2+1;rightchild=parent*2+2;leftchild+1=rightchild【Conversely, you can calculate the subscript of the father through the child】parent = (child-1)/2

2.3 Properties of Binary Trees

1. If the number of layers of the root node is specified as 1, then there are at most 2^(i-1) nodes on the i-th layer of a non-empty binary tree .
2. If the number of layers of the root node is specified as 1, then the maximum number of nodes in a binary tree with a depth of h is 2^h-1 .
3. For any binary tree, if the degree is 0 , the number of leaf nodes is n0 , and the number of branch nodes with  degree 2 is n2 , then there is n0 = n2 + 1
4. If the number of layers of the root node is specified as 1, the depth of a full binary tree with n nodes , h=log2 (n+1  (ps:
Is log base 2, n+1 is logarithm)
5. For a complete binary tree with n nodes, if all nodes are numbered from 0 according to the order of the array from top to bottom and from left to right, then for the node with the sequence number i:
1. If i>0 , the parent number of the node at i position: (i-1)/2 ; i=0, i is the root node number, no parent node
2. If 2i+1<n , the left child number: 2i+1 , 2i+1>=n otherwise there is no left child
3. If 2i+2<n , the serial number of the right child: 2i+2 , 2i+2>=n , otherwise there is no right child
 
Multiple choice questions:
1. Among the following data structures, the one that is not suitable for sequential storage structure is (A)
A incomplete binary tree
B stack
C queue
D stack
2. In a complete binary tree with 2n nodes, the number of leaf nodes is (A)
A n
B n+1
C n-1
D n/2
There are N0 with degree 0, N1 with degree 1, and N2 with degree 2. N1+N2+N0=2n; because N0=N2+1, N1+N0-1+N0=2n; because the number of N1 is 0 or 1.N1+N0-1+N0=2n, this formula should Want to be established, it can only be 1, so choose A
3. The number of nodes in a complete binary tree is 531, then the height of this tree is (B)
A 11
B 10
C 8
D 12
The number of full-level binary tree nodes is 2^k-1.9 layers 2^9-1=511, and the 10-layer is 1023. The 10-layer complete binary tree is at least 512 and at most 1023. So choose B
4. A complete binary tree with 767 nodes, the number of leaf nodes is (B)
A 383
B 384
C 385
D 386
You can use the idea of ​​the second question

2.4 Storage structure of binary tree

Binary trees can generally be stored using two structures, a sequential structure and a chain structure.
1. Sequential storage
Sequential structure storage is to use arrays for storage . Generally, arrays are only suitable for representing complete binary trees , because not complete binary trees will waste space. In reality, only the heap will be stored in an array, and we will specifically explain the heap in the following chapters. Binary tree sequential storage is physically an array and logically a binary tree.
d34adae974754d70acccf4d9eccc0bfd.png
2. Chain storage
The linked storage structure of the binary tree means that a linked list is used to represent a binary tree, that is, a link is used to indicate the logical relationship of elements. The usual method is that each node in the linked list is composed of three fields, the data field and the left and right pointer fields, and the left and right pointers are used to give the storage addresses of the link points where the left child and right child of the node are located. The chain structure is further divided into binary chains and triple chains. At present, we generally use binary chains in our studies. Later courses will learn high-level data structures such as red-black trees, which will use triple 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; // 当前节点值域
} 

 

bd273229935b46369d13e7c52df81775.png    

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_57388581/article/details/131585285