Literacy data structure – Introduction to heap

Time:2021-10-18

What is a heap

  • The pile is a【Complete binary tree
  • All of the heap【Root node】”Greater than”【Child node

    The greater than here can be defined.

<img style=”zoom:50%;” />

As shown in the figure above, it satisfies the properties above the heap. It is a complete binary tree, and all root nodes are larger than child nodes

Shown above is the maximum heap (the corresponding minimum heap can also be defined)

  • Use array representation

    <img style=”zoom:50%;” />

    Because the heap meets the definition of a complete binary tree, the heap can be represented by an array [as shown in the figure above].

    The following formula can be deduced from the node at the index position in the figure aboveparent(i) = i / 2 left child (i) = 2 * i right child (i) = 2 * i + 1

    However, in the above figure, the zero position of the array is actually wasted. If the elements are arranged from the zero position, the following structure will be used

    <img style=”zoom:50%;” />

    The formula can be deduced from the above figure as follows:

    parent(i) = (i - 1) / 2 left child (i) = 2 * i + 1 right child (i) = 2 * i + 2

Common operations in heap

  • Sift up (add element to heap)

    Floating up, because the bottom layer of the heap is implemented as an array, when we add elements, we directly add elements to the end of the array, so as to always ensure that the heap is a complete binary tree, but we need to maintain the second property of the binary tree. The root node element is larger than the child node, so we need the SIFT up operation

    Assume the following heap structure:

    <img style=”zoom:50%;” />

    Suppose that the element we need to add now is 52, and now the position of the element isarr[arr.length - 1]However, this violates the structure of the heap because52 > 16Sift up is to exchange two elements if the current element is greater than the value of the root node element, the [iteration] is executed until the child node is smaller than the root node.

    At this point, we need to exchange the values of elements 52 and 16

    <img style=”zoom:50%;” />

    This is what happens after the exchange

    <img style=”zoom:50%;” />

    But at this time, you will find that the heap structure is still not satisfied because52 > 41, so 52 and 41 need to be exchanged

    <img style=”zoom:50%;” />

    The structure of the heap is satisfied only after the exchange

    <img style=”zoom:50%;” />

    The whole process of element 52 moving above is called sift up floating

    <img style=”zoom:80%;” />

  • Sift down (take out the largest element in the heap)

    According to the characteristics of the heap, the element of the root node is the largest element in the heap, so we only need to take out the root node.

    <img style=”zoom:50%;” />

    However, if the root node is taken out directly, the original heap will be cut into two heaps, which will become extremely troublesome in the subsequent merging. Therefore, we change our thinking here, directly exchange the position of the end element arr [arr. Length – 1] 16 with the root node 62, and then process it into a heap structure, which will be much simpler

    <img style=”zoom:50%;” />

    As shown in the figure above, directly replace the end element with the U-turn node. At this time, the heap structure is violated, and we need to operate sift down.

    The current element is compared with its left and right children, exchanged with the older of the left and right children, iterated, and finally the SIFT down can be completed

    <img style=”zoom:50%;” />

    For the first exchange of 16 and 50, because 52 > 30, the effect after exchange is

    <img style=”zoom:50%;” />

    The second exchange is 16 and 42 because 42 > 16

    <img style=”zoom:50%;” />

    After the previous two exchanges, the nature of the maximum heap is now satisfied.

    The above two exchanges are called sift down

    <img style=”zoom:80%;” />

  • replace
  • heapity

This paper is based on the operation tool platform of blog group sending one article and multiple sendingOpenWriterelease