How to apply heap in Java data structure
This article introduces the knowledge of "how to apply heap in Java data structure". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. creation of heap 1. Downward adjustment (take small heap as an example)
Let parent mark the nodes that need to be adjusted, and child marks the left child of parent (Note: if parent has a child, there must be a left child first)
If the left child of parent exists, that is, child < size, do the following until the left child of parent does not exist:
Whether the right child of parent exists, find the youngest of the left and right children, and let child mark it.
Compare parent with the child of a younger child if:
The parent is less than the younger child's child, otherwise the adjustment ends: exchange the parent with the younger child's child. After the exchange is completed, the larger elements in the parent move downward, which may cause the subtree not to satisfy the nature of the heap, so you need to continue to adjust downward, that is, parent = child;child = parent*2+1; and then continue 2.
Public void shiftDown (int [] elem,int parent,int len) {int cur=parent*2+1; while (cur)