And full binary tree complete binary tree


The heap is used to achieve complete binary tree


Complete binary tree:

Complete binary tree is a highly efficient data structures, heap is a complete binary tree or nearly complete binary tree, so very efficient, very common sorting algorithm like, Dijkstra algorithm, Prim algorithm to optimize the heap so use almost every time to test the binary sort tree efficiency should be improved by means of balance, and balance based on complete binary tree.

Complete binary tree (Complete Binary Tree)

Assuming that the depth h of the binary tree, except the h layer, other layers (1 ~ h-1) has reached the maximum number of nodes, the h layer all nodes are continuously concentrated in the left, which is entirely binary Tree.
Complete binary tree is a full binary tree and lead out. The depth is K, there is a binary tree of n nodes, if and only if called binary tree nodes each of which are the full depth of K binary tree node numbers following from 1 to n.
A binary tree node degrees at most on the bottom two layers may be less than 2, and the nodes are concentrated in the lowermost layer of the plurality of positions on the left, this binary tree becomes complete binary tree.

Features complete binary tree

Leaf nodes may appear only on the two largest of any node, if the maximum level at which descendants right branch is L, the maximum level at which the children of the left branch will be for L or L + 1;
for brevity, a complete binary usually array instead of a linked list memory which stores the following structure:
var Tree: array [. 1 ... n-] of LongInt; {n-: Integer; n-> =. 1}
for the tree [i], has the following characteristics:
(1) where i is an odd number and i> 1, then the tree in the left sibling of tree [i-1];
(2) if i is even and i <n, then the tree right brothers tree [i + 1];
(3) If i> 1, tree parents of tree [I div 2];
(. 4) if 2 I <= n-, then the tree in the left child of tree [2 I]; if 2 I +. 1 <= n-, then the right child tree is tree [2 I +. 1];
(. 5) if i> n div 2, then the tree [I] is a leaf node (corresponding to (3));
(6) If i <(n- 1) div 2. then tree [i] must have two children (corresponding to (4)).
(7) must be a full binary tree complete binary tree, not necessarily a complete binary tree is a full binary tree.
Complete binary tree has at most i-th layer 2 (. 1-i) th node, i-layer co-complete binary tree has at most 2 i-nodes. 1.
If the depth having an n-node binary tree is k, each of its nodes are full binary tree of depth k is numbered 1 ~ n correspondence node, called a binary tree complete binary tree.
Can be carried out according to the formula derived, assuming n0 is the degree of node point total of 0 (i.e., the leaf node points), N1 is the degree of the total number of nodes 1, n2 is the degree of the total number of nodes 2, seen from the nature of a binary tree: n0 = n2 + 1, then n = n0 + n1 + n2 (wherein n is the total number of complete binary tree nodes), from the above equation to obtain the elimination of n2: n = 2n0 + n1-1, due to moderate complete binary junction 1 only two possible points 0 or 1, whereby n0 = (n + 1) / 2 or n0 = n / 2.
To sum up, it is n0 = [n / 2], where the [] indicates rounding. It can be calculated according to the total number of leaf nodes of nodes complete binary tree.

In fact, a full binary tree complete binary tree is a special case, because the full binary tree is already full, but completely does not mean full. So you should also form imagined it, it refers to a full node outside each leaf node has two children, and complete the final layer of meaning is not full and not full.

Full Binary (Full Binary Tree):

Except the last one without any child node, all the nodes on each layer has two child nodes (the last node without a child node of the leaf node is one). Can also be understood, all nodes except leaf node has two child nodes. Reached the maximum number of nodes. All leaf nodes must be on the same layer.

Tree depth is h, the maximum number of layers is k, the maximum number of layers with the same depth, k = h;

It is the number of leaves: 2 ^ h
  the number of nodes k-th layer are: 2 ^ (k-1)
  summarizes the points are: 2 ^ k-1 (k minus a power of 2)
  the total number of nodes must be odd.

Complete binary tree (Complete Binary Tree)

Assuming that the depth h of the binary tree, except the h layer, other layers (1 ~ h-1) has reached the maximum number of nodes, the h layer all nodes are continuously concentrated in the left, which is entirely binary Tree.
  Complete binary tree is a full binary tree and lead out. The depth is K, there are N nodes in the binary tree, known as a complete binary tree if and only if each of which are related to the depth of node K in the full binary numbers following from node 1 to n.
  If a node in the binary tree of degree at most 2 may be less than the lowest layers, and the nodes are concentrated in the lowermost layer of the plurality of positions on the left, this binary tree becomes complete binary tree.

Here Insert Picture Description

Published 65 original articles · won praise 0 · Views 1296

Guess you like

Origin blog.csdn.net/hellobettershero/article/details/104407432