One, if you want to understand the structure of a heap you have to understand what is a complete binary tree
Two sentences, the middle node must be full, the last layer is either full, or from left to right is a full state.
Heap.
1. The heap is a complete binary tree
There are only large and small piles
What do you mean? Take the big root heap, where each node is the largest in the tree with its vertices.
Some formulas for complete binary trees
If the index starts at 0, then for any I, its left child is 2i+1, its right child is 2i+2, and its father is (I -1)/2
Four. The common algorithm about heap
1. Let’s say you’re given one tree at a time and told to make a big root pile
Each time you add a number, compare the number with its parent node, and continue the process to the root node
2. Let’s say you return the largest value in the heap, and then delete that value, leaving the rest of the heap as one large root
First record the number of the first position (the large root heap), then replace the element of the last position to the first position, and then look at the left and right children of the value, which is the largest and which is the other, continue this process until you have no left and right children
3. The heap sort
Form a heap of numbers into a heap structure, and pop up the largest/smallest number in turn.