table of Contents:
1. Tree
A tree is a non-linear data structure. It is a hierarchical collection composed of n (n>=0) finite nodes. It is called a tree because it looks like an upside-down tree, which is it is the root up and leaves down the
如下图所示为一棵普通的树
• 节点的度:一个节点含有的子树的个数称为该节点的度; 如上图:A的为6
• 叶节点或终端节点:度为0的节点称为叶节点; 如上图:B、C、H、I…等节点为叶节点
• 非终端节点或分支节点:度不为0的节点; 如上图:D、E、F、G…等节点为分支节点
• 双亲节点或父节点:若一个节点含有子节点,则这个节点称为其子节点的父节点; 如上图:A是B的父节点
• 孩子节点或子节点:一个节点含有的子树的根节点称为该节点的子节点; 如上图:B是A的孩子节点
• 兄弟节点:具有相同父节点的节点互称为兄弟节点; 如上图:B、C是兄弟节点
• 树的度:一棵树中,最大的节点的度称为树的度; 如上图:树的度为6
• 节点的层次:从根开始定义起,根为第1层,根的子节点为第2层,以此类推;
• 树的高度或深度:树中节点的最大层次; 如上图:树的高度为4
• 堂兄弟节点:双亲在同一层的节点互为堂兄弟;如上图:H、I互为兄弟节点
• 节点的祖先:从根到该节点所经分支上的所有节点;如上图:A是所有节点的祖先
• 子孙:以某节点为根的子树中任一节点都称为该节点的子孙。如上图:所有节点都是A的子孙
• 森林:由m(m>0)棵互不相交的树的集合称为森林;
Two. Binary Tree
1. What is a binary tree
A binary tree is a finite set of nodes. The set is either empty or consists of a root node plus two binary trees called left subtree and right subtree.
2. Features of Binary Tree
• Each node has at most two subtrees, that is, no node with degree greater than 2 exists in the binary tree.
• The subtrees of a binary tree are divided into left and right, and the order of the subtrees cannot be reversed.
3. Special Binary Tree
3.1 Full Binary Tree
Full binary tree : A binary tree, if the number of nodes in each layer reaches the maximum, then the binary tree is a full binary tree. In other words, if the number of levels of a binary tree is K, and the total number of nodes is (2^k) -1, then it is a full binary tree.
3.2 Complete Binary Tree
Complete binary tree: Complete binary tree is a very efficient data structure. Complete binary tree is derived from full binary tree. For a binary tree with a depth of K and n nodes, if and only if each node corresponds to a node numbered from 1 to n in the full binary tree with a depth of K, it is called a complete binary tree. It should be noted that the full binary tree is a special kind of complete binary tree.
4. The nature of the binary tree
• If the level of the root node is specified as 1, then a non-empty binary tree has at most 2^(i-1) nodes on the i-th level.
• If the level of the root node is specified as 1, the depth is h The maximum number of nodes of the binary tree is 2^h-1 (the number of previous i nodes)
• 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 n0=n2+1
• If the level of the root node is 1 and the depth of a full binary tree with n nodes, h=Log2(n+1). (ps: Log2(n+1) is log with 2 Is the base, n+1 is logarithm)
• For a complete binary tree with n nodes, if all nodes are numbered starting from 0 in an array order from top to bottom, from left to right, then for the node with sequence number i There are:
a. If i>0, the parent sequence number of the node i: (i-1)/2; i=0, i is the root node number, and there is no parent node
b. If 2i+1<n, the left child sequence number: 2i+1, 2i+1>=n otherwise there is no left child
c. If 2i+2<n, the right child sequence number: 2i+2, 2i+2>=n otherwise there is no right child
5. Practice
1. A binary tree has a total of 399 nodes, including 199 nodes with a degree of 2, then the number of leaf nodes in the binary tree is ( B )
A does not exist such a binary tree
B 200
C 198
D 199
解析:设度为0其叶子节点数为n0(叶子节点),度为1其叶子节点数为n1,度为2其叶子节点数为n2
则由题意可得:n0+n1+n2=399
n0=n2+1
n2=199
解得:n0(叶子节点数)=200
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
解析:由题可得:n0+n1+n2=2n;
n0=n2+1
=>2n0+n1-1=2n
由于为完全二叉树,所有n1为1或0
=>2n0=2n或2n0=2n+1
由上述公式可得n1只能为1,所以n0=n
3. The number of nodes in a complete binary tree is 531, then the height of the tree is ( B )
A 11
B 10
C 8
D 12
解析: