Package Cn.xf.algorithm.ch09greedy.util;import Java.util.arraylist;import java.util.list;/** * Heap Construction and Sorting * *. function: Heap construction * 1, The heap can be defined as a binary tree, the node of the tree contains the key, and satisfies the condition * 1) The shape requirements of the tree: This binary tree is a basic complete (full binary tree), each layer of the tree is full, except the last layer of the rightmost element may be absent * 2) parental advantage, heap characteristics, Each node's key is greater than or equal to the key of his child (for any leaf we think it is automatically satisfied) * * for heaps: * There is only one n node full binary tree his height: the logarithm of N of the log2 of the removed bounds * The root of the heap always contains the largest element of the heap * A node of the heap and that section The descendants of the point are also a heap * you can use arrays to implement heaps by recording the elements of the heap from top to bottom, left to right. * * @author xiaof * @version Revision 1.0.0 * @see: * @ Date Created: August 25, 2017 * Function Description: * */public class Heap {private LIST&L T;integer> Heap; Constructor public Heap () {//create heap heap = new arraylist<integer> (); } public Heap (List<integer> heap) {//create heap this.heap = heap; Createheaddowntoup (THIS.HEAP); }/** * from small to large heap * @param heap * @return */private void Createheaddowntoup (list<integer> he AP) {//array for heap sort if (heap = = NULL | | heap.size () <= 0) return; int len = Heap.size (); Start loop in the middle of the tree for (int i = LEN/2; i > 0; -i) {/////index and value int selectindex = i-1, first pre-save the current operation int selectvalue = Heap.get (Selectindex); Boolean isheap = false; Used to determine if there are no other nodes under the current node that are smaller than this node, as if there is a heap of identities while (!isheap && 2 * (Selectindex + 1) <= len) {////The position of the largest child node of the current node , start by default is the position of the first child node int childindex = 2 * i-1; Determine if there are two child nodes, if present, then select the largest if (2 * i < len) {//Get the smaller node as alternate replacement node Childindex = Heap.get (childi Ndex) < Heap.get (Childindex + 1)? Childindex:childindex + 1; }//To determine if the current node is smaller than the smallest node below if (Selectvalue <= heap.get (childindex)) {//If the largest is larger than the following, it indicates that this node is the root of the subtree is already a tree. Isheap = true; } else {//If the node is not small, replace Heap.set (Selectindex, Heap.get (Childindex)); and swap the node of the current traversal interchange Selectindex = Childindex; After all the nodes and child nodes have been traversed, swap out the selected node Heap.set (Selectindex, Selectvalue) that was originally used for the interchange; }}}}/** * A single transform to the node of the heap * @param i the first few nodes */private void Shifheaddowntoup (int i) {if (heap = = NULL | | heap.size () <= 0) Return;int len = This.heap.size ();//Index I needs to exist in this The node in the if (I >= len) return;//first pre-saves the currently operating node '//index and value int selectindex = I-1;int Selectvalue = Heap.get (Selectindex); Sheap = false; Used to determine if there are no other nodes under the current node that are smaller than this node, as if there is a heap of identities while (!isheap && 2 * (Selectindex + 1) <= len) {////Current node's maximum child node location, start default Is the position of the first child node int childindex = 2 * (Selectindex + 1)-1;//Determine if there are two child nodes, if present, then select the largest if (2 * (Selectindex + 1) < Len) { Get the smaller node as alternate replacement node Childindex = Heap.get (Childindex) < Heap.get (Childindex + 1)? Childindex:childindex + 1;} Determine if the current node is smaller than the smallest node below if (Selectvalue <= heap.get (childindex)) {//If the largest is larger than the following, it indicates that this node is the root of the subtree is already a tree Isheap = true;} else {//If the node is not small, replace the Heap.set (Selectindex, Heap.get (Childindex));//and swap the node for the current traversal interchange Selectindex = childindex;// After this node and the child node are all traversed, swap out the selected node Heap.set (Selectindex, Selectvalue) that was originally used for the interchange;}} Adding elements to the heap public void Add (int element) {//int Oldlen = Heap.size (); Heap.add (Element);//Then start from the parent node of the joined position, from the bottom up all the parent nodes, all transformations once for (int i = Heap.size ()/2; i > 0; i = i/2) {this.shifheaddowntoup (i);}} /** * Removes a specified element in the heap * @param index * @return *///public int Remove (int index) {//int result = Heap.get (index-1);////idea. The last element is filled in as a reference element//int Lastvalue = Heap.get (Heap.size ()-1);//heap.set (index-1, Lastvalue);//heap.remove (Heap.size ( )-1);////then from the bottom up, the data for the position of the node corresponds to the recursive//for (int i = index; i > 0; i = i/2) {//this.shifheaddowntoup (i);//}//return Resul t;//}public int Remove (Integer object) {int index = Heap.indexof (object);//idea is the last element remaining as a reference element, filled in int lastvalue = Heap.get (Heap.size ()-1); Heap.set (index, Lastvalue); Heap.remove (Heap.size ()-1);//Then from the bottom up, the data for the location of this node is recursive for (int i = index + 1; i > 0; i = I/2) {this.shifheaddowntoup (i);} return index;} /** * Default Delete root node * @return */public int Remove () {int result = Heap.get (0);//thought is the last element remaining as a reference element, filled in int lastvalue = Heap.get (Heap.size ()-1); Heap.set (0, Lastvalue); Heap.remove (Heap.size ()-1);//Then from the bottom up, the data for the location of this node is recursivelyfor (int i = 1; i > 0; i = i/2) {this.shifheaddowntoup (i);} return result;} @Overridepublic String toString () {return heap.tostring ();}}
"Algorithm Design and Analysis Basics" 23, heap sort-2