We have introduced the linear structure. The data elements in the linear structure are one-to-one relationships. This chapter and the next chapter introduce two important non-linear structures: Tree Structure and graph structure. The tree structure is a one-to-many nonlinear structure, which is very similar to the trees in nature. Data elements have both branch and hierarchical relationships. Tree structures are widely used in the real world, such as family trees (figure 1) and administrative organizations of a unit (figure 2.
Tree structures are also widely used in the computer field, such as disk file management in the Windows operating system and representation of the syntax structure of the source program in the compiler. In the database system, the tree structure is also an important form of data organization. Tree Structure: Multi-tree and binary tree. The implementation of multi-Tree operations is complicated, but multi-tree can be converted to binary tree for processing. Therefore, this chapter mainly discusses binary tree.
Windows resource manager is a typical tree structure.
A typical binary tree in nature.
The first question is what is a tree. A tree is a finite set of n (n ≥ 0) data elements of the same type. The Data Element in the tree is called a node ). A tree with n = 0 is called an empty tree. For any non-empty tree with N> 0, T has:
(1) There is only one special node called the root node of the tree, and the root has no parent node ;.
(2) If n> 1, the other nodes except the root node are divided into M (M> 0) sets that do not meet each other T1, T2 ,..., TM, where each set of Ti (1 ≤ I ≤ m) itself is a tree. Tree T1, T2 ,..., TM
A subtree ). :
From the definition of the tree, we can see that the definition of the tree is recursive, and the tree is used to define the tree. Therefore, many algorithms of Binary Trees use recursion. The tree form is defined as: tree (tree) is a binary group, where T = (D, R) Where: D is a finite set of nodes; is a finite set of links between nodes.
As shown in figure 5.2, none of the above two features are trees.
Tree has many terms. Let's introduce them one by one.
1. node: indicates the data element in the tree, which is composed of the relationship between the data item and the data element. There are seven nodes in total.
2. Degree of node: Number of Subtrees owned by the node. In the middle, the degree of the child node is 3.
3. Degree of tree: the maximum degree of each node in the tree. In, the degree of the tree is 3.
4. leaf node: A node with a degree of 0, also called a terminal node. In, the child node and the grandson node are both leaf nodes.
5. branch node: A node with a degree of less than 0. It is also called a non-terminal node or an internal node. In, the root node and subnode are the branch nodes.
6. Child: the root of the tree. In, the child node is the root node.
7. Parent: the upper node of a node is the parent node of the node. In, the parent node of the child node is the root node.
8. Ancestor: All nodes from the root node to the branch on which the node passes. In, the ancestor of the Sun Tzu node is the child node and the root node.
9. descendant (descendant): Any node in the subtree with a node as the root. In, all nodes except the root node are the children of the root node.
10. Brother: children of the same parent. In, the child nodes are mutual brothers.
11. Level of node: the number of branches in the path from the root node to a node in the tree is called the level of the node. The level of the root node is defined as 1, and the level of the other nodes is equal to the level of the parent node plus 1.
12. Cousin (sibling): the two sides of the same layer have different nodes.
13. Depth of tree: the maximum number of layers of nodes in the tree. In, the depth of the tree is 3.
14. unordered tree: The order between the child nodes of any node in the tree forms an irrelevant tree. A tree usually refers to an unordered tree.
15. Ordered Tree: each child node of any node in the tree has a strictly ordered tree. A binary tree is an Ordered Tree, because each child node in a binary tree is definitely defined as the Left or Right child node of the node.
16. Forest (forest): a set of M (M ≥ 0) trees. The concepts of trees and forests in nature are quite different, but the concepts of trees and forests are quite different in data structures. According to the definition, a tree consists of a root node and a sub-tree. If the root node of the tree is deleted, the tree becomes a forest containing M trees. Of course, a tree can also be called a forest.
There are many logical Representation Methods for the tree. Here we only talk about several common representation methods.
1. Intuitive Representation
It is like a tree in daily life. The entire figure is like an inverted tree, which is constantly expanded from the root node. The root node is on the top and the leaf node is on the bottom, as shown in.
2. Concave Representation
Each node corresponds to a rectangle. The rectangles at all nodes are right aligned. The root node is represented by the longest rectangle. The rectangles at the same layer have the same length. The higher the hierarchy, the shorter the length of the rectangle, shows the concave representation of the tree in.
3. Generalized table Representation
In the form of a generalized table, the root node is placed at the top, and its subtree nodes are enclosed by a pair of parentheses. the subtree nodes are separated by commas. The generalized table of the tree is as follows:
(Root Node (child node (Sun Tzu node, Sun Tzu node), child node (Sun Tzu node, Sun Tzu node )))
4. nested Representation
Similar to the literary Graph Representation in mathematics, as shown in.
There are five binary tree forms: empty Binary Tree, binary tree with only root nodes, binary tree with empty right subtree, binary tree with empty left subtree, and non-empty binary tree with left and right subtree. Five Binary Tree forms.
(1) Full Binary Tree: If a binary tree has only nodes with a degree of 0 and a degree of 2, and nodes with a degree of 0 are on the same layer, this binary tree is a full binary tree, as shown in (.
According to the definition, for a full binary tree with a depth of K, the number of nodes is 2k-1.
(2) Complete Binary Tree: a binary tree with a depth of K and N nodes. if and only when each node has a depth of K, the full binary tree with N knots numbered from 1 to n is called a full binary tree, as shown in (B. The full Binary Tree feature that the leaf node can only appear on the maximum two layers of the hierarchy, in addition, the maximum level of the child of the left branch of a node is equal to or greater than 1 of the child of the right branch.
Nature 1 A non-empty binary tree has a maximum of 2
I-1 node (I ≥ 1 ).
Proof: mathematical induction is used for proof. When n = 1, the binary tree has only one layer. This layer has only one root node and one node. Therefore, the number of nodes in layer 1st is 21-1 = 1. The conclusion is true. Assume that when n = N, the conclusion is true, that is, layer N has a maximum of 2n-1 nodes. When n = n + 1, according to the binary tree definition, each node in layer N has a maximum of two subnodes. Therefore, layer n + 1 has a maximum of 2n-1*2 = 2n = 2 (n + 1)-1 nodes,
Conclusion. To sum up, Nature 1 was established.
Property 2 If the depth of the required empty tree is 0, a maximum of 2 binary trees with a depth of K
K-1 node (k ≥ 0 ).
Proof: When k = 0, the number of knots in the empty tree is 20-1 = 0, and the conclusion is true. When the depth is K (k> 0,
From Nature 1, we can see that the I (1 ≤ I ≤ k) layer has a maximum of 2 I-1 nodes, so the maximum number of knots of a binary tree is: 1 + 2 ^ 1 + ....... + 2 ^ (I-1) = 2 ^ I-1
Nature 3 the depth of a Complete Binary Tree with N nodes K-Is log2n + 1.
Proof: according to the definition of property 2 and full Binary Tree, when the number of knots in a full binary tree is n and the depth is K, 2k-1-1 <n ≤ 2k-1
That is, 2k-1 ≤ n <2 K to obtain the logarithm of the inequality, K-1 ≤ log2n <k because K is an integer, So k = log2n + 1.
Property 4 for a non-empty Binary Tree, if the number of nodes with a degree of 0 is N0, and the number of nodes with a degree of 2 is N2, N0 = n2 + 1.
Proof: set N as the total number of nodes of the Binary Tree, and n = N0 + N1 + N2 in the binary tree, except for the root node, the other nodes have a unique entry branch. If B is set to the total number of branches in a binary tree, the branches B = N-1 are issued by the nodes with the degree of 1 and the degree of 2. A node with the degree of 1 sends a branch, A node with a degree of 2 sends two branches, so B = N1 + 2 N2 combines the above three sub-statements to obtain N0 = n2 + 1
Property 5 for a Complete Binary Tree with N nodes, if the number of all nodes starts from 1 in the order of top to bottom and left to right, for nodes with the serial number of I, there are:
(1) If I> 1, the number of the parent node of the node with the serial number I is I/2 ("/" indicates division); if I = 1, the node is the root node without any parent node.
(2) If 2I ≤ n, the number of the left child node of the node is 2I; If 2I> N, the node has no left child.
(3) If 2I + 1 ≤ n, the number of the right child node of the node is 2I + 1; if 2I + 1> N, the node has no right child
This is a basic introduction to the nature of Binary trees, which will be implemented at the source code level.