Detailed explanation of heap creation, adjustment, insertion, deletion and other operations in data structure heap sorting (the explanation of the topic is simple and easy to understand)

heap definition

First of all, we need to clarify what a heap is. In short, a heap is a complete binary tree with special properties.

Complete binary tree: a binary tree with a depth of k and n nodes . The nodes in the tree are numbered from top to bottom and from left to right. If the number is i (1≤i≤n) The position of the node in the binary tree is the same as that of the node numbered i in the full binary tree, then this binary tree is called a complete binary tree

The special nature of the heap is reflected in the size relationship between the node and the child node. When the value of the parent node is greater than or equal to the value of its child node, it is a large root heap, otherwise it is a small root heap

heap operations

In the data structure of the heap , the maximum value in the heap is always located at the root node (if the heap is used in the priority queue, the minimum value in the heap is located at the root node). The following operations are defined in the heap:

Max Heapify: Adjust the end child nodes of the heap so that the child nodes are always smaller than the parent node

Create the largest heap (Build Max Heap): reorder all the data in the heap

Heap sort (HeapSort): remove the root node of the first data, and do the recursive operation of the maximum heap adjustment

Next, we mainly make adjustments by manually building piles of questions and performing operations such as insertion and deletion. This is also one of the question types of 408 common exams over the years.

 heap insertion

The insertion operation is relatively simple, just insert the insertion keyword into the empty leaf node of the heap, and then adjust the nodes according to the nature of the large root heap or the small root heap

Topic: The known keyword sequence 5, 8, 12, 19, 28, 20, 15, 22 is a small root pile, insert keyword 3, and what is the small root pile obtained after adjustment?

For this kind of problem, we first draw the original small root pile. Note that the corresponding node number here is that the parent node is i, then the left child is 2i, and the right child is 2i+1

heap deletion

 The deletion of the heap is a little more complicated. Heap deletion generally refers to the root node, which actually deletes the maximum/minimum value in the sequence. At this time, we insert the root node with the last element in the heap , and then use the large root heap or The nature of the small root pile can be adjusted

Topic: Sort the keyword sequence 23, 17, 72, 60, 25, 8, 68, 71, 52 in the minimum heap, what is the remaining heap after outputting the two minimum keywords?

Create a heap first, and then use the last node to make up and adjust after each output/deletion

 

Empty heap creation 

Next, we will explain with the real questions of 408 in 2021

Topic: Insert the keywords 6, 9, 1, 5, 8, 4, and 7 into the initially empty large root heap H in sequence. What is the H obtained?

How to do the question: According to the insertion method above, insert into the leaf nodes in turn, and adjust immediately if there is any inconsistency

 heap adjustment

With the above foundation, the adjustment of the heap is very easy to understand. The adjustment of the heap means that an original heap is adjusted to form a large root heap or a small root heap. Note that the insertion and establishment of the empty heap above the root is different.

Question: When the sequence 6, 1, 5, 9, 8, 4, 7 is built into a big root pile, what is the correct sequence change process?

Problem-solving method: first write the initial heap. At this time, it generally does not satisfy the properties of the large root heap or the small root heap, and then adjust it from the end of the sequence. After one round of adjustment, look down to see if there is any need to adjust Until it conforms to the nature of the large root pile or the small root pile

 It's not easy to create and find it helpful, please like, follow and collect~~~

Guess you like

Origin blog.csdn.net/jiebaoshayebuhui/article/details/130399376