Concept
The level of the node is defined from the root and the root is the first layer and the child of the root is the second layer.
Height of the binary tree: the maximum level of the node in the tree is called the depth (Depth) or height of the tree.
What is the degree of the tree in the data structure? It is the maximum value of the degree of each node within the tree.
Why the degree of the node? The number of sub-trees owned by a node is called the degree of the node.
Two-fork Tree
In computer science, a binary tree is an ordered tree with a maximum of two subtrees per node. Usually the root of the subtree is referred to as the left subtree and right subtree. A binary tree is often used as a binary search tree and a two-fork heap. Each node of a binary tree has a maximum of two subtrees trees (no nodes with a degree greater than 2), and the subtree of the binary tree has left and right points, and the order cannot be reversed. The first layer of the binary tree has a maximum of 2 (i-1) sub-nodes, and a two-tree with a depth of K has a maximum of 2 k times? 1 nodes; n0 =N2 + 1 for any binary tree T, if its terminal node is n0 and the node with degrees 2 is n2.
The 2 main differences between a tree and a two-fork tree:
1. There is no limit to the maximum number of nodes in the tree, and the maximum degree of the binary tree nodes is 2;
2. The nodes of the tree have no left and right points, while the nodes of the binary tree have left and right points. ......
A tree is an important nonlinear data structure, visually, it is the structure of data elements (called nodes in a tree) organized by branches, much like the trees in nature. Tree structure is widely existed in the objective world, such as the Genealogy of human society and various social organization organizations can be expressed in tree image. Tree is also widely used in the field of computer, such as when compiling the source program as follows, the tree can be used to represent the following syntax structure. As in the database system, the tree structure is also one of the important organizational forms of information. All problems with hierarchical relationships can be described by a tree.
I. Overview of the Tree
Tree structure is characterized by: each of its nodes can have more than one direct successor, all nodes outside the root node have and only a direct forward. The following gives a detailed definition of the tree and the representation of the tree's data structure.
(a) The definition of a tree
A tree is a finite set of one or more nodes, where:
⒈ must have a specific node called root (Root);
The remaining nodes of the ⒉ are divided into n>=0 disjoint collections T1, T2 、...... Tn, and each of these sets is a tree. Tree T1, T2 、...... TN is called the root of the subtree (subtree).
The recursive definition of the tree is as follows: (1) at least one node (called the root) (2) Other disjoint subtrees
1. The degree of the tree-that is, the width, simply, is the number of branches of the node. To make up the maximum degree of each node of the tree as the degree of the tree, such as the tree, its degree is 3; the nodes with zero degrees in the tree are called leaf nodes or terminal nodes. Nodes with non-zero degrees in the tree are called branching nodes or non-terminal nodes. The branching nodes outside the root nodes are collectively referred to as internal nodes.
2. The depth of the tree-the maximum level of the nodes that comprise the tree, for example, its depth is 4;
3. Forest-refers to the collection of a number of disjoint trees, such as removing the root node A, its original two subtrees tree T1, T2, T3 collection {T1,T2,T3} is the forest;
4. Ordered tree-refers to the tree in the same layer nodes from left to right in order, the order between them is not interchangeable, such a tree is called ordered tree, otherwise called unordered tree.
5. Representation of the tree
There are many ways to represent trees, the usual method is to use parentheses: first put the root node in a pair of parentheses, and then put its subtree from left to right in parentheses, and the subtree is also handled in the same way; the same layer subtree is enclosed in parentheses with its root node, and the same layer subtree is separated by commas, and finally enclosed in parentheses. If you can write the following form:
(A (B (E (k,l), F), C (G), D (H (M), i,j)))
5.22 Fork Tree
1. The basic form of a two-fork tree:
Binary tree is also a recursive definition, its nodes have left and right sub-tree, logically two fork tree has five basic forms:
(1) Empty binary tree-(a);
(2) A two-fork tree with only one root node--(b);
(3) Only the right sub-tree-(c);
(4) Only left dial hand tree-(d);
(5) Complete binary tree-(e)
Note: Although there are many similarities between the binary tree and the tree, the binary tree is not a special case of the tree.
2. Two important concepts:
(1) A complete binary tree-only the bottom two levels of the node is less than 2, and the bottom layer of the nodes are concentrated in the left-most position of the layer of the two-fork tree;
(2) Full two fork tree-in addition to the leaf node, each node has left and right sub-Ye Yi nodes are at the bottom of the two fork tree.
3. The nature of the two-fork tree
(1) In the binary tree, the total number of nodes in layer I does not exceed 2^ (i-1);
(2) The two-fork tree with a depth of H has a maximum of 2^h-1 nodes (h>=1), at least h nodes;
(3) For any binary tree, if its leaf node is N0, and the number of nodes with a degree of 2 is N2,
Then n0=n2+1;
(4) The depth of a complete binary tree with n nodes is int (log2n) +1
(5) If the nodes of a complete binary tree with n nodes are stored sequentially, the nodes have the following relationship:
If I is the node number, if i<>1, then the parent node is numbered I/2;
If 2*i<=n, then the left son (i.e. the root node of Zuozi) is numbered 2*i; if 2*i>n, there is no left son;
If 2*i+1<=n, then his right son's node number is 2*i+1, if 2*i+1>n, there is no right son.
(6) Given n nodes, it can form an H (n) of different two-fork trees.
H (n) is the nth item of the Cattleya number. H (N) =c (n,2*n)/(n+1).
4. Two the storage structure of the fork tree:
(1) Sequential storage mode
Type Node=record
Data:datatype
L,r:integer;
End
var tr:array[1..n] of node;
(2) List storage methods, such as:
Type Btree=^node;
Node=record
Data:datatye;
Lchild,rchild:btree;
End
5. Normal tree converted to Binary tree: all the brothers connected with the line, and then remove the father to the son of the connection, leaving only the parents to their first child connection.
The binary tree is like a hanging tree, from the roots to the branches, small branches, until the leaves to connect data, this data structure is called the tree structure, referred to as tree. Each fork point in the tree is called the node, and the starting node is called the root, and the connection between any two nodes is called a branch, and no branches are called leaves below the node. The node's forward node is called the "parent" of the node, and the post-nodal point of the node is called the "child" or "child" of the node, and the "children" of the same node are referred to as "brothers".
Binary tree: Two fork tree is a very important tree-type structure. It is characterized in that each node in the tree has a maximum of two subtrees trees, that is, any node in the tree should not be more than 2 in degrees. The sub-tree of the binary tree has the left and right points, and the order of the sub-tree is important, even in the case of only one subtrees tree, it should be clear whether it is the Zuozi tree. Definition: Two a fork tree is a finite set of nodes, which is either empty or consists of a root node and two disjoint tree of two forks called the Saozi right subtree.
(iii) Complete binary tree
For a full two fork tree, from the first layer of the node (that is, the root), from the bottom up, from the left and right, in order to the number of nodes, and then get full two of the tree of a sequential representation. The complete binary tree is defined as follows: A two-fork tree with n nodes, with a depth of k, when and only if all nodes correspond to those nodes that are numbered from 1 to N in a full two-fork tree with a depth of K, the two fork tree is a complete binary tree. Figure 4 is a completely binary tree.
Balanced two-pronged tree
when and only if the height difference of two subtrees does not exceed 1 o'clock, the tree is a balanced binary tree. (Same time is sort of binary tree)
Balanced binary tree, also known as AVL tree. It is either an empty tree or a two-fork tree with the following properties: Its Saozi right subtree is a balanced binary tree, and the difference between the height of the Saozi right subtree is not greater than 1.
Common algorithms are: red black tree, AVL tree, Treap and so on.
Adjustment method of Balanced binary tree
The Balanced binary tree is the process of constructing a two-fork sort tree, whenever a new node is inserted, first check whether the balance of the two-fork sort tree is broken by inserting a new node, and if so, find the smallest unbalanced subtree, and adjust the link relationship between the nodes in the least-balanced subtree under the premise of preserving the binary sort tree. Rotate accordingly to make it a new balance subtree. The steps are as follows:
⑴ whenever a new node is inserted, the equilibrium factor of each node is calculated from the node, that is, the balance factor of the ancestor node of the node is calculated, and if the absolute value of the balance factor of the ancestor node of the node is not more than 1, then the balanced binary tree does not lose its equilibrium and continue inserting the node;
⑵ if the absolute value of the equilibrium factor of an ancestor node of the insertion node is greater than 1, the root node of the smallest unbalanced subtree is found;
⑶ determine the relationship between the newly inserted node and the root node of the least-balanced subtree, and decide which type of adjustment to make;
⑷ if it is a ll-type or RR type, just apply the principle of rotation, in the rotation process, if there is a conflict, apply rotation priority principle to adjust the conflict; if it is LR or LR type, it is necessary to apply the principle of rotation two times, the first minimum unbalanced subtree root node is not moved, adjust the insertion node is the subtree, The second adjustment of the minimum unbalanced subtree, in the course of rotation, if there is a conflict, apply rotation priority principle to adjust the conflict;
⑸ calculates the balance factor of each node in the adjusted balanced binary tree, tests whether the balance factor of other nodes is destroyed by rotation, and whether there is a balance factor greater than 1 in the adjusted balanced binary tree.
(b) The height of the Tuzok number on the left is 3, the right subtree is 1, and the difference is over 1.
(b) On the right, figure 2 of the Zuozi height is 0 The height of the right subtree is 2, the difference is more than 1
Full binary tree (complete binary trees)
Complete binary Tree definition
If the height of the two-fork tree is H, except for the H layer, the nodes of the other layers (1~h-1) reach the maximum number, and the H layer is continuously missing several nodes from right to left, which is the complete binary tree.
Full binary tree Features
One, the leaf node can only appear on the largest two layers, to any node, if its right branch of the descendants of the largest level of L, then the left branch of the descendants of the largest level must be L or l+1;
For simplicity, a complete binary tree is usually stored in an array instead of a linked list, with the following storage structure:
var tree:array[1..n]of longint; {N:integer;n>=1}
For Tree[i], there are the following features:
(1) If I is odd and i>1, then Tree[i] 's left brother is tree[i-1];
(2) If I is even and i<n, then Tree[i] 's right brother is tree[i+1];
(3) If I>1,tree[i] 's parents are tree[i Div 2];
(4) If 2*i<=n, then Tree[i] 's left child is tree[2*i], if 2*i+1<=n, then tree[i] the right child for tree[2*i+1];
(5) If I>n Div 2, then tree[i] is the leaf node (corresponds to (3));
(6) if i< (n-1) Div 2. Then tree[i] must have two children (corresponding to (4)).
Specifically: Full two fork tree must be completely binary tree, complete binary tree is not necessarily full of two fork tree
An algorithm of complete binary tree leaf node
If a two-fork tree with a depth of n nodes is K, each of its nodes corresponds to a node one by one with a depth of k in the full two-fork tree numbered 1~n, which is called a complete binary tree.
Can be deduced according to the formula, assuming that N0 is a degree of 0 of the total number of nodes (that is, leaf nodes), N1 is a degree of 1 of the total number of nodes, N2 is 2 of the total number of nodes, by the nature of the two fork tree: n0=n2+1, then n= n0+n1+n2 (where n is the total number of binary tree nodes) By the above formula to eliminate the N2: n= 2n0+n1-1, because the total binary tree is 1 of the nodes only two possible 0 or 1, resulting in n0= (n+1)/2 or N0=N/2, combined into a formula: n0= (n+1)/2, The leaf nodes can be calculated based on the total number of nodes of the complete binary tree.
Full two fork Tree
A depth of K, and a 2 of the (k)-1 nodes of the two-tree feature: The number of nodes on each layer is the maximum node count
The definition of a complete binary tree: A depth of K, a two-fork tree with n nodes when and only if each of its nodes corresponds to a node one by one with a number from 1 to n in a full two-tree with a depth of K, called a complete binary tree. Features: Leaf nodes may appear only on the two layers with the largest level; for any node, if the maximum level of the descendants of the right branch is L, then the maximum level of the descendants under the left branch must be L or l+1 two fork tree: A depth of K, and 2 (k) square-1 nodes of two fork tree Features: nodes on each layer are the maximum node number full Two The tree must be completely binary tree complete binary tree is not necessarily full two fork tree
Full binary tree full two fork tree