1. Basic terms:
Degree:There are two types of degrees: "node degree" and "tree degree ". The degree of a node refers to the number of knots in a tree, and the degree of a tree refers to the maximum degree of knots in the tree.
Leaf node:Indicates a node without a subtree..
Layer:The tree is layered. Generally, the root node is Layer 3. Specify the path length of the root node to a node as the number of layers of the node.
Depth:Maximum number of layers of nodes in the tree
Brother:Nodes of the same parent and mutual siblings
Cousin:The two parents are at the same level of node.
Ancestor:All nodes from the root node to the path of the node are the ancestor of the node.
Descendant:All the nodes in the subtree rooted in a certain node are the children of this node.
Forest:N trees
2. Binary Tree:
Different from the tree, the degree of the node <= 2, and the sub-tree has left and right points, such:
The numbering rules are left to right and top to bottom, for example:
Property 1:The number of nodes at layer I cannot exceed the I power of 2.
Nature 2:The depth of the binary tree is N, and the total number of nodes of the binary tree is not greater than the n + 1 power of 2 minus 1.
Nature 3:Number of leaf nodes = number of nodes with a degree of 2 + 1
Full Binary Tree:Except for the underlying node, the degree of the other nodes is 2, as shown in figure
Complete Binary Tree:Not necessarily full. If a binary tree has n nodes, it corresponds to the nodes numbered 1-N of the full binary tree. Such a binary tree is called a Complete Binary Tree.
Property 1:The number of the parent node with id I is I/2, and the result is rounded up.
Nature 2:The left child ID of the node I is 2I, And the right child id is 2I + 1.
Nature 3:If the total number of nodes in the Complete Binary Tree is N, the height of the Complete Binary Tree is the log base 2 to calculate the logarithm of N, and the result is rounded up.
Binary Tree implementation:
Sequential storage:
For the full binary tree and full binary tree, you can use the full binary tree property 2 to locate the Parent and Child locations. For example:
For a common binary tree, the value population method is used to complete the binary tree into a Complete Binary Tree, and then the Complete Binary Tree is stored in sequence.
Chain storage:
The node contains three values: Data Field, pointer to the left subtree, and pointer to the right subtree.
Binary tree traversal:
First root traversal:DLR first accesses the root node, then traverses the left subtree, and finally traverses the right subtree. When traversing the left and right subtree, you can still access the root node first, then traverse the left subtree, and finally traverse the right subtree.
Root traversal:LDR first traverses the left subtree, then accesses the root node, and finally traverses the right subtree. When traversing the left and right subtree, we still traverse the left subtree first, then access the root node, and finally traverse the right subtree.
Root traversal:LRD first traverses the left subtree, then the right subtree, and finally accesses the root node. When traversing the left and right subtree, we still traverse the left subtree first, then traverse the right subtree, and finally access the root node.
How to convert a tree into a binary tree?
Step 1: Keep the connection between the leftmost child and the root node and connect the sibling node. For example, B is the leftmost child of a, so keep the connection between A and B and disconnect the connection between A and C, A and D. B, C, and D are brothers, so they connect BCD. The sub-trees with B, C, and D as the root still follow these rules.
Step 2: rotate the left subtree 45 degrees clockwise and the converted binary tree is obtained.
How to convert a binary tree into a tree?
Step 1: opposite to the step of converting a tree to a binary tree, the left subtree rotates 45 degrees counter-clockwise.
Step 2: disconnect the brothers and connect the two parents.
How to convert a forest into a binary tree?
Step 1: each tree is first converted to a binary tree
Step 2: Use the root node of the first tree as the root node and connect the root node in sequence
Step 3: rotate 45 degrees clockwise at the root node
How can a binary tree be converted into a forest?
Step 1: disconnect the root node from the right subtree. The right subtree is still disconnected from the right subtree as required. In this way, multiple Binary Trees are obtained.
Step 2: convert each binary tree to a tree