Storage binary tree complete binary tree, and why there is this concept and definitions? ? ?

Binary Tree's storage

Storing a binary tree, there are two methods, one is based on a pointer or reference to the chain of binary storage method A sequence storage method is based on the array.

 

Chain store law

Chain is represented by a binary tree, i.e. to indicate a chain of logic elements. Binary chain

Each node has three fields, one of the storage data, the other two are pointing to the left and right child node pointers.

 

Method sequential storage

Sequence structure of the memory array is used to store.

We labeled the root node stored in the next position i = 1, then the left child superscript 2 * stored in the next position i = 2, the position of the right child node is stored in the 2 * i + 1 = 3 in.

So, the left child node of the node B is stored in position 2 * 2 * 2 = I = 4, the right child node is stored in position 1 + I 2 * 2 * 2 + 1 = 5 =.

 , Subscript i is stored in the position 2 * is the left child node if the node stored in the array X labeled position i.

The subscript is the right child of the stored position of the 2 * i + 1.

In turn, the position storage labeled i / 2 is its parent node.

In this way, as long as we know the position of the root node is stored, it can be calculated by the following standard, the whole tree string together.

(In general, in order to facilitate the calculation of child nodes, the root node is stored at an index position 1)

 

Why the concept of complete binary tree, and requires complete binary tree node to the last level of the entire arrangement left it?

 

On just sequentially stored method, cited is an example of a complete binary tree. If the non-complete binary tree it?

Can be seen, the non-complete binary tree will waste a lot of storage space.

The complete binary tree only just "wasted" at index 0 position.

 

Advantages sequential storage method is not as linked storage method as to store pointers about additional child nodes.

Therefore, a binary tree is a complete binary tree, it is undoubtedly the most storage arrays with a way to save memory.

 

That is why the complete binary tree can carry out alone, but also the reason why the child requires complete binary tree nodes are left of the last layer.

 

Heap is actually well represented, in fact, a complete binary heap, the most common way is stored in an array.

 

 

Published 114 original articles · won praise 25 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_42006733/article/details/104580717