Basic Concepts
The level of a node is defined from the root. The root layer is the first layer, and the child of the root layer is the second layer.
The height of a binary tree: the maximum level of a node in the tree is called the depth or height of the tree.
Binary Tree
In computer science, a binary tree is an ordered tree with a maximum of two Subtrees at each node. Generally, the root of a subtree is called left subtree and right subtree ). Binary Trees are often used as binary search trees and binary heaps. Each node of a binary tree has at most two Subtrees (nodes with no degree of presence greater than 2). the Subtrees of a binary tree have left and right points, and the order cannot be reversed. The second layer of the binary tree has at most 2 (I-1) to the power of the node; the depth of K of the binary tree at most 2 k-1 node; for any binary tree T, if the number of terminal nodes isN0. The number of knots whose degree is 2 isN2, thenN0 =N2 + 1.
Two major differences between trees and binary trees:
1. There is no limit on the maximum number of nodes in the tree, while the maximum number of nodes in the binary tree is 2;
2. Tree nodes have no left or right, while Tree nodes have left or right .......
A tree is an important non-linear data structure. It is a structure in which data elements (called nodes in a tree) are organized by branches, just like trees in nature. Tree structures are widely used in the objective world, such as the genealogy of human society and various social organizations. Tree is also widely used in the computer field. For example, when the source program is compiled as follows, a tree can be used to indicate the following syntax structure of the source program. Another example is that tree structure is also an important form of information organization in database systems. All hierarchical issues can be described in a tree.
I. Overview of trees
The tree structure features that each of its nodes can have more than one direct successor. All nodes except the root node have only one direct forward trend. The following describes the definition of the tree and the data structure of the tree.
(1) tree Definition
A tree is a finite set of one or more nodes:
A worker must have a specific node called the root node;
The remaining worker nodes are divided into N> = 0 sets of T1, T2,... TN, and each of these sets is a tree. Tree T1, T2,... TN are called the root subtree ).
The recursive definition of a tree is as follows: (1) there must be at least one node (called the root) (2) other child trees that are not mutually exclusive
1. The degree of the tree, that is, the width, that is, the number of branches of the node. The maximum degree in each node of the tree is used as the degree of the tree. For example, the degree of the tree is 3. the node with a moderate degree of zero in the tree is called a leaf node or a terminal node. A node with a moderate degree of non-zero in a tree is called a branch node or a non-terminal node. All branch nodes except the root node are called internal nodes.
2. Tree depth: the maximum layer of each node in the tree. For example, the depth is 4;
3. forest refers to the set of several trees that do not meet each other. For example, Remove root node A. The set of the original two subtree T1, T2, and T3 {T1, T2, t3} is the forest;
4. Ordered Tree: it refers to a tree with Layer Nodes arranged in order from left to right. The order between them cannot be exchanged. Such a tree is called an Ordered Tree, otherwise it is called an unordered tree.
5. Tree Representation
There are many methods to represent the tree. The common method is to enclose the root node in a pair of parentheses, and then place the child tree from left to right into parentheses, the child tree uses the same method. The child tree and its root node are enclosed in parentheses, separated by commas (,), and enclosed in closed brackets. The format is as follows:
(A (B (E (K, L), F), C (G), D (H (M), I, j )))
5. 2 binary tree
1. Basic Binary Tree forms:
Binary Trees are also defined recursively. Their nodes have left and right Subtrees. Logically, binary trees have five basic forms:
(1) null binary tree -- ();
(2) binary tree with only one root node -- (B );
(3) only the right subtree -- (C );
(4) only the left subtree -- (d );
(5) Complete Binary Tree -- (E)
Note: Although Binary Trees and trees have many similarities, binary trees are not a special case of trees.
2. Two important concepts:
(1) Complete Binary Tree-a binary tree with only two levels of nodes at the bottom of the layer smaller than 2, and the nodes at the bottom of the layer are all at the leftmost position of the layer;
(2) Full Binary Tree-except for leaf nodes, each node has left and right leaves and leaf nodes are at the bottom of the binary tree ,.
3. The nature of Binary Trees
(1) In a binary tree, the total number of nodes in the I layer cannot exceed 2 ^ (I-1 );
(2) h depth of the binary tree can have a maximum of 2 ^ h-1 nodes (h> = 1), at least H nodes;
(3) For any binary tree, if the number of leaf nodes is N0, and the number of nodes with a degree of 2 is N2,
Then N0 = n2 + 1;
(4) the depth of the Complete Binary Tree with N nodes is int (log2n) + 1
(5) If the nodes of the Complete Binary Tree with N nodes are stored in sequence, the nodes have the following relationships:
If I is a node number, if I <> 1, the ID of its parent node is I/2;
If 2 * I <= N, the number of the Left son (that is, the root node of the Left subtree) is 2 * I. If 2 * I> N, NO LEFT son exists;
If 2 * I + 1 <= N, the node number of the right son is 2 * I + 1. If 2 * I + 1> N, no right son exists.
(6) given n nodes, different Binary Trees of H (n) can be formed.
H (n) is the nth entry of the catlan number. H (n) = C (n, 2 * N)/(n + 1 ).
4. Binary Tree storage structure:
(1) sequential Storage
Type node = record
Data: datatype
L, R: integer;
End;
VaR TR: array [1 .. n] of node;
(2) linked list storage methods, such:
Type btree = ^ node;
Node = record
Data: datatye;
Lchild, rchild: btree;
End;
5. convert a common tree into a binary tree: any brother connects to the tree with a line, and then removes the line between his father and his son, leaving only the line between his parents and his/her first child.
A binary tree is like a inverted tree, from the root to the big branches, small branches, until the leaves link the data. This data structure is called a tree structure, or tree for short. Each split point in a tree is called a node, and the Start Node is called a root node. The connection between any two nodes is called a branch. No branches under a node are called leaves. The front node of a node is called the "parent" of the node, and the back node of the node is called the "child" or "child" of the node ", the "children" of the same node are mutually called "brothers ".
Binary Tree: a binary tree is a very important tree structure. It features that each node in a tree can only have two Subtrees at most, that is, the degree of any node in the tree cannot be greater than 2. A binary tree has left and right sub-trees. In addition, the left and right orders of sub-trees are important, even if there is only one sub-tree, the left or right subtree should also be distinguished. Definition: a binary tree is a finite set of knots. This set is either empty or composed of a root node and a binary tree of the left and right subtree that are mutually exclusive.
(3) Complete Binary Tree
For a full Binary Tree, starting from the first layer of the node (that is, the root), from the bottom up, from the left and right, according to the order of node numbers, you will get an order of full Binary Trees. According to the serial number, the full binary tree is defined as follows: a binary tree with N nodes and a depth of K is defined, if and only when all nodes correspond to the knots numbered from 1 to n in the full binary tree with a depth of K, the binary tree is a Complete Binary Tree. Figure 4 is a Complete Binary Tree.
Balanced Binary Tree
When and only when the height difference between the two Subtrees cannot exceed 1, the tree is a balanced binary tree. (Sorting binary tree at the same time)
Balanced binary tree, also known as AVL Tree. It is either an empty tree or a binary tree of the following nature: Its left and right subtree are both balanced binary trees, the absolute value of the height difference between the left subtree and the right subtree cannot exceed 1 ..
Common algorithms include red/black trees, AVL trees, and treap trees.
Balance Binary Tree Adjustment Method
A balanced binary tree is used to construct a binary tree. When a new node is inserted, first check whether the balance of the binary tree is damaged due to the insertion of the new node. If yes, then, the minimum unbalanced subtree is found. When the binary sorting tree feature is maintained, the links between nodes in the minimum unbalanced subtree are adjusted and rotated accordingly, make it a new balance subtree. The procedure is as follows:
(1) When a new node is inserted, the balance factor of each node is calculated from the beginning of the node, that is, the balance factor of the ancestor node of the node is calculated, if the absolute value of the balance factor of the ancestor node of the node does not exceed 1, the balanced binary tree does not lose the balance and continues to insert the node;
(2) If the absolute value of the balance factor of an ancestor node of the inserted node is greater than 1, find the root node of the Child tree with the minimum imbalance;
(3) determine the relationship between the newly inserted node and the root node of the Child tree with the minimum imbalance and determine the type of adjustment;
(4) If it is ll or RR type, you only need to apply the flat principle to rotate once. If there is a conflict during the rotation process, apply the rotation priority principle to adjust the conflict. If it is LR or LR type, you need to apply the flat load principle to rotate twice. For the first time, the root node of the sub-tree is not moved, adjust the sub-tree where the inserted node is located, and for the second time adjust the sub-tree with the minimum imbalance, if a conflict occurs during rotation, apply the rotation priority principle to adjust the conflict;
⑸ Calculates the balance factor of each node in the adjusted balance Binary Tree, and checks whether the balance factor of other nodes is damaged due to rotation, and whether a node with a balance factor greater than 1 exists in the adjusted balance binary tree.
(B) the height of the left sub-number of the graph on the left is 3, and the height of the right sub-tree is 1, with a difference greater than 1
(B) the height of the Left subtree in Figure 2 on the right is 0. The height of the right subtree is 2, with a difference greater than 1.
Complete Binary Tree)
Full Binary Tree Definition
Full Binary Tree
If the height of a binary tree is set to H, except for layer H, the other layers (1 ~ H-1) are all reached the maximum number of points, the H layer from the right to the left consecutive lack of a number of nodes, This Is The Complete Binary Tree.
Full Binary Tree Features
1. Leaf nodes may only appear on the maximum two layers. For any node, if the maximum hierarchy of the Child branches under the right branch is l, the maximum hierarchy of the child of the left branch must be l or L + 1;
2. For simplicity, full Binary Trees are usually stored in arrays instead of linked lists. The storage structure is as follows:
VaR tree: array [1 .. n] of longint; {n: integer; n> = 1}
Tree [I] has the following features:
(1) If I is an odd number and I> 1, then the left brother of tree [I] is tree [I-1];
(2) If I is an even number and I <n, the right brother of tree [I] is tree [I + 1].
(3) If I> 1, the parent of tree [I] is tree [I Div 2];
(4) If 2 * I <= N, the left child of tree [I] is tree [2 * I]. If 2 * I + 1 <= n, the right child of tree [I] is tree [2 * I + 1].
(5) If I> N Div 2, tree [I] is the leaf node (corresponding to (3 ));
(6) If I <(n-1) Div 2. Then tree [I] must have two children (corresponding to (4 )).
In particular, full binary trees must be full binary trees, and full Binary Trees may not be full Binary Trees.
Algorithms for completely binary tree leaf nodes
If a binary tree with a depth of K with N nodes, each of its nodes is numbered 1 ~ from the full binary tree with a depth of K ~ N nodes correspond one to one. This binary tree is called a Complete Binary Tree.
According to the formula, we can deduce that N0 is the total number of nodes whose degree is 0 (that is, the number of leaf nodes), N1 is the total number of nodes whose degree is 1, and N2 is the total number of nodes whose degree is 2, according to the nature of the Binary Tree: N0 = n2 + 1, n = N0 + N1 + N2 (where N is the total number of nodes of the Complete Binary Tree), the above formula removes N2: N = 2n0 + n1-1, because the Complete Binary Tree is moderate to 1 of the knot points only two possible 0 or 1, resulting in N0 = (n + 1) /2 or N0 = n/2, and combine them into a formula: N0 = (n + 1)/2. Then, the number of leaf knots can be calculated based on the total number of nodes in the Complete Binary Tree.
Full Binary Tree
A binary tree with a depth of K and a power of 2 (k)-1 node features: the number of knots on each layer is the maximum number of knots
Full Binary Tree definition: the depth is K, A binary tree with N nodes is called a Complete Binary Tree when each node is one-to-one with numbers ranging from 1 to n in a full binary tree with a depth of K. Features: leaf nodes may only appear on the maximum two layers of layers. For any node, if the maximum level of the child of the right branch is l, the maximum hierarchy of the Child branches under the left branch must be l or L + 1 full Binary Tree: a tree with a depth of K and a binary tree with a power of 2 (k)-1 node features: the number of knots on each layer is the maximum number of knots. Full binary trees must be full Binary Trees. Full Binary Trees are not necessarily full Binary Trees.