Algorithm Notes: Heap

[Unless otherwise stated, all are minimum binary heaps]

1 Introduction

2 Features

  • Structural: conforms to the structure of a complete binary tree
  • Orderliness: satisfy that the parent node is smaller than the child node (minimize heap) or the parent node is greater than the child node (maximize heap)

3 Binary heap storage

sequential storage

The ordering of a binary heap can be easily reflected by subscripts

4 Insert new elements into the heap

  • Heap insertion is to insert new elements or nodes after the element with the largest sequence number, otherwise it will violate the structural nature of the heap.
  • If the new element is placed without violating the order of the heap, the operation ends.
  • Otherwise, let the node move toward the parent node until ordering is satisfied or the root node is reached.
  • The upward movement of new nodes is called upward filtering

4.1 Time Efficiency

  • The worst case is O(logn) [exchange all the way to the root node]
  • On average, filtering ends early.
    • Some data show that the average is 2.6 comparisons, so the elements are moved up 1.6 levels on average.

5 Push operation (DeQueue)

  • When the smallest element is deleted, an empty node appears at the root. The size of the heap is 1 smaller than before. The structure of the heap tells us that the last node should be deleted.
  • If the last item can be placed in this empty node, put it in. However, this is usually not possible
  • You have to play a "game" similar to the insertion operation: put some items into empty nodes, and then move the empty nodes.
    • The only difference is that for DeQueue operations, empty nodes are moved downwards.

  • Find a smaller child node of the empty node. If the value of the child is smaller than the item we want to put, put the child into the empty node and push the empty node down one level.
    • Repeat this action until the item is placed in the correct position.

5.1 Time Complexity

  • In the worst case, deQueue is a logarithmic time operation
  • According to the orderliness of the heap, the value of the last node in the heap is generally relatively large. Therefore, downward filtering rarely ends one or two levels earlier, so the deQueue operation takes logarithmic time on average.

6 pile

  • It can be seen as N consecutive insertions, which should be completed in O(NlogN) time.

  • In fact, during the construction process, we do not care about the state of the heap after each element is added. What we care about is the final state after all N elements are added. The final state is to ensure the orderliness of the heap. It doesn't matter whether the orderliness in the intermediate process is established.
  • With this premise, we can reduce the time complexity of constructing the heap to O(N)
    • Using the recursive definition of the heap
      • If the function buildHeap can adjust a complete binary tree into a heap, then just call buildHeap recursively on the left sub-heap and the right sub-heap.
      • At this point, we can ensure that except for the root node, the order of the heap has been established in other places.
      • Then call downward filtering on the root node to create orderliness of the heap
      • If we call downward filtering on nodes in reverse hierarchical order, then when filtering node i downward, all descendants of node i have already called downward filtering.
        • No need to perform downward filtering on leaf nodes
        • Filter downwards starting from the non-leaf node with the highest number

6.1 Examples

Initially, a complete binary tree is built based on the order of data.

Filter downwards starting from the non-leaf node with the largest sequence number (30)

 

6.2 Time analysis

  •  For a node h (leaf node height is 0), the maximum number of exchanges in downward filtering is h
  • The maximum time to build a heap is the sum of the number of exchanges required for adjustment of all nodes, that is, the sum of the heights of all nodes

7 D stack

  • Each node has d sons, so the resulting heap is shorter.
  • insert:O(\log_dN)
  • Deletion: You need to find the smallest one among d elements. The time complexity is:O(\log_dN)
  • Pros: fast insertion
  • Disadvantages: slow deletion
  • use:
    • Queue with more insertions than deletions
    • The queue is too large to be stored in the memory. When it needs to be placed in external storage

Guess you like

Origin blog.csdn.net/qq_40206371/article/details/132712733