Heap (binary heap)

A bunch of definitions:

       The heap is a logical data structure, the English name heap, often for a period of continuous space, such as an array. This is a heap storage structure.

  Stack and logical structure, are intrinsically sorted binary tree.

  By heap sort, divided into piles, and a small pile.

         

 

  Relationship stack storage structure, and logical structure is as follows:

  1. Storage structure:

              Array is provided Array [N], is assumed here that N = 9

     

         This array is a heap storage structure

  2. The logical structure, i.e., the relationship between the elements and the elements in the array:

   Elements in the array, who is the root node, who is a child node? This relationship is determined in the following manner.

   In the above array, the element name is Ai (i = 1 ~ N) is satisfied:

    1> is the parent node number parent = i / 2. If the parent = 0, no parent node, Ai is the root node (obviously, A1 is the top of the root node).

    2> at most two child nodes, namely left = i * 2, right = i * 2 +1. If there is no child node, the node (the leaves do not grow on the leaves of the tree, it represents the most end) leaf nodes.

    3> piles (pile small), the value of the parent node must be greater than (less than) all the child nodes. As shown below.

     

 

  Popular explanations relations under the heap:

  Heap is like a family tree, or family relationship diagram

  Piles, that is, from the elders to the children of the diagram, the node represents Age: Mother is always greater than the child's age. But the grandchildren's age, but there is no fixed order, grandchildren age, may be greater than uncle, aunt. Note that since the heap is a binary tree, so this genealogy only a maximum of two children.

  Small heap, diagrams is their children to elders, node represents age. The root of the age, the age must be less than about nodes. But there is no fixed order between nodes grandfather and parents (grandparents may be less than paternal age)

Two heap initialization

       To become an array heap must first be initialized ordered such disorderly array into order heap.

       We assume an array as follows, and converts it into piles:

No.

0

1

2

3

4

5

6

7

name

A1

A2

A3

A4

A5

A6

A7

A8

value

49

38

65

97

76

13

27

50

 

  1. First disordered array becomes disordered binary tree.

    Setting i = 1 to 8, a child node i * 2, i * 2 + 1, sequentially add child node. The results as shown below:

 

 (Disordered binary tree)

  1. From the leaf node to the root node, finishing sorted binary tree.

    a> non-leaf nodes to find the outermost layer (away from the root) of the. Since N = 8, the distal-most node is i = INT (N / 2) = 4.

    b> From the A4 Start, finishing A3, A2, A1 is the root of the subtree (which is a cycle)


 

    c> Ai of the current node, if no child node, loop back to the big b>.

      If a child node to identify the child nodes is greater. And compared with Ai.

      If Ai large, this consolidation is complete, return to a cycle b>

      If Ai is small, then the switch Ai to the large value of the sub node. Then i = No. exchange Repeat c>.

(For example, sub-tree of A2 to do finishing :)

     

 

    Initialization summary:

      You can see, disorder organize the tree, there are two loops.

      Inner loop is that for sub-tree has been ordered, adding a new root, then from the root to the leaves, do the sort.

      Outer loop, the inner loop when a subtree is complete, continue to traverse all complete subtree (N = 4,3,2,1)

      This policy is, first collated grassroots (subtree); then gradually the middle, finishing into the top range (larger sub-tree), until the entire system consolidation is complete.

Three in the heap and add data refresh

       If a data a_new, add to an already serialized and fixed nodes of the stack, then need to add as follows ( here exemplified in the piles, it is noted that the maximum piles always eject, save small value, the number of piles are often used to save the minimum number N k ):

  1. Comparative A_new root A1:

    If A_new greater than A1, the stack A_new not added, this update is complete.

    If a_new is less than A1, then A1 = A_new, i = 1. 2 and then enters ..

  2. The current node is Ai, if no child node, complete finishing.

    If a child node to identify the child nodes is greater. And compared with Ai.

    If Ai large, this consolidation is complete.

    若Ai小,则交换 Ai 与该大值子节点。 然后i = 交换序号,重复执行2.。

       可以看出,其实就是执行的堆的初始化中的“2.c>” 步。

       如下图是一个例子,经过三次交换后,堆更新完成。

 

 

 

 

 

四 堆的其他运算:

       包括堆的减小,堆的增加,堆头的删除等。

  暂时待加入。

五 堆的优点

       当我们在堆中,增加新的值时,需要重新排序. 若堆有N个元素,那么这个排序的时间复杂度为 Log2N

       这是由于,一个具有N个元素的堆,它最多具有Log2N 层 。 而最坏的情况下,每层需要执行一次交换。 

       不过,对于时间复杂度,我们一般计为:

     LogN                                   

       这是由于,不论底数是多少,例如(ln N)和( lg N ) ,它们的比例是常数,说明不论底是多少,他们都是一个数量级的。所以直接用logN来表示时间常数。

       而如果采用数组标准的排序法,则最坏要执行N次交换。

       显然,堆的执行效率高很多。

       例如:

    一个长度为1024的堆,要更新一个新元素,执行次数最多为:10次。

    一个长度为1024的数组,则跟新一个元素,执行次数最多为:1024次。

       数据越大,则这个差距也越大。

             

Guess you like

Origin www.cnblogs.com/stonenox/p/11243935.html