[Data structure] Binary heap

Reference: " Comic Algorithm - Xiao Hui's Algorithmic Journey"

Table of contents

1. What is a binary heap

Second, the operation of the binary heap

1. Insert a node

2. Delete node

3. Build a binary heap

3. Time complexity and space complexity

Fourth, the storage method of the binary heap


1. What is a binary heap

Binary heap is essentially a complete binary tree, which is divided into two categories: maximum heap and minimum heap . The value of any parent node of the largest heap is greater than or equal to the value of its left and right child nodes; the value of any parent node of the smallest heap is less than or equal to the value of its left and right child nodes.

The root node of the binary heap is called the top of the stack. The characteristics of the largest heap and the smallest heap determine that: the top of the largest heap is the largest element in the entire heap; the top of the smallest heap is the smallest element in the entire heap.

Second, the operation of the binary heap

There are several operations for binary heaps: inserting nodes, deleting nodes, and building binary heaps. These kinds of operations are based on the self-tuning of the heap. The so-called self-adjustment of the heap is to adjust a complete binary tree that does not conform to the nature of the heap into a heap. The following are examples of minimum heap.

1. Insert a node

When inserting a node, the insertion position is the last position of the complete binary tree . For example inserting a new node with a value of 0.

 The parent node 5 of the new node is greater than 0, which does not conform to the nature of the heap. So let the new node float up and exchange positions with the parent node.

 Continue to compare node 0 with parent node 3, because 0 is less than 3, let the new node continue to "float".

 Continue to compare, and finally the new node 0 "floats" to the top of the heap.

2. Delete node

The process of deleting a node in a binary heap is just the opposite of the process of inserting a node, and the node at the top of the heap is deleted. For example, delete the top node 1 of the minimum heap.

 In order to continue to maintain the structure of the complete binary tree, we temporarily fill the last node 10 of the heap to the original position at the top of the heap.

Let node 10, which is temporarily at the top of the heap, compare it with its left and right children. If the smallest of the left and right child nodes (obviously node 2) is smaller than node 10, then let node 10 "sink".

Continue to compare node 10 with its left and right children. The smallest of the left and right children is node 7. Since 10 is greater than 7, let node 10 continue to "sink".

3. Build a binary heap

To build a binary heap, that is, to adjust an unordered complete binary tree into a binary heap, the essence is to let all non-leaf nodes sink sequentially.

example:

First, start from the last non-leaf node, which is node 10. A node 10 "sinks" if it is larger than the smallest of its left and right children.

Next is node 3's turn. If node 3 is larger than the smallest of its left and right child nodes, node 3 "sinks".

Then it's node 1's turn, and if node 1 is larger than the smallest of its left and right child nodes, then node 1 "sinks". In fact node 1 is smaller than its left and right children, so no changes are needed. Next it is the turn of node 7. If node 7 is larger than the smallest of its left and right child nodes, then node 7 "sinks".

 Node 7 continues to compare and continues to "sink".

After the above rounds of comparison and "sinking" operations, each node is finally smaller than its left and right child nodes, and an unordered complete binary tree is constructed into a minimum heap.

3. Time complexity and space complexity

The insertion operation of the heap is the "floating" of a single node, and the deletion operation of the heap is the "sinking" of a single node. The average number of exchanges between these two operations is half of the heap height, so the time complexity is O(logn). The time complexity of building a heap is not O(n).

Fourth, the storage method of the binary heap

Although the binary heap is a complete binary tree, its storage method is not chain storage, but sequential storage. In other words, all nodes of the binary heap are stored in the array.

In the array, how to locate the left child and right child of a parent node without left and right pointers? As shown in the figure above, you can rely on array subscripts to calculate. Suppose the subscript of the parent node is parent, then the subscript of its left child is 2×parent+1; the subscript of its right child is 2×parent+2.

Guess you like

Origin blog.csdn.net/weixin_45922730/article/details/129711217