/* * @author Lip * @date 2015-04-23 */public class heap{public static void Main (string[] args) {//TODO auto-generated Meth OD stub//system.out.println ((int) -1.5); int []array={4,2,7,9,3,6,1,12,10,5}; System.out.println ("Original:");p Rintheapbylevel (array); SYSTEM.OUT.PRINTLN ("Minimum Heap:"), Getminheap (array);p Rintheapbylevel (array),//SYSTEM.OUT.PRINTLN (Math.log (i+1)/ Math.log (2)); Sort (array); Printheapbylevel (array);} */* Heap properties (minimum heap for example): * 0. The root node is the minimum value * 1. The heap can be seen as a complete binary tree (that is, the child node is arranged from left to right) * 2. The height of the heap lgn/lg2 (n is the number of nodes) * 3. The left child node of node I is 2*i+1 The node is 2*i+2 * 4. With either node as the root node, the node is heap * 5. You can use one data to represent the heap *//* * The principle of building a heap: * On filter * Create an empty node after the last node, the newly inserted node and parent node are compared: * 1, Larger than the parent node, then directly inserted at this empty heap * 2, smaller than the parent node, then the parent node moves down to the empty node * 3, repeats until the insert heap */public static void getminheap (int []array) {for (int i=1;i<array.length;i++) Insert (array, i);} From inserting the value of the K position into the heap, the worst case of inserting one is lgnpublic static void insert (int []arrary,int k) {int insert=arrary[k];while (k>0)// Always compare with parent node {if (insert<arrary[(k-1)/2])//The element that needs to be inserted is less than the parent node, then the parent nodeMove Down {arrary[k]=arrary[(k-1)/2]; k= (k-1)/2; if (k==0) Arrary[k]=insert;} else//The element that needs to be inserted is greater than the parent node, which can be plugged in at the last position {Arrary[k]=insert; Break;}}} Print a heap public static void Printheapbylevel (int []array) {int height=0;for (int i=0;i<array.length;i++) {int) by level tempheight= (int) (Math.log (i+1)/math.log (2)), if (tempheight>height) {System.out.println (); System.out.print (array[i]+ ""); Height=tempheight;} else System.out.print (array[i]+ "");} System.out.println ();} /* * Binary tree sort */public static void sort (int []array) {for (int i=0;i<array.length;i++) {deletemin (array, array.length-1-i );}} /* Delete a root node, which is the minimum value * then the child node will be floating * can be sorted by this heap * The minimum value of the deletion is placed in the last position of the array, saving space * Root node is I (0,1,2,3 ...) * Left child node: 2*i+1 * Right child node: 2*i+2 * n is the remaining node */public static void Deletemin (int []array,int n) {int min=array[0];int temp=0;//looking for a child to fill the root node while (2*temp+1<n) No child node {if (array[2*temp+1]>array[2*temp+2]) {array[temp]=array[2*temp+2]; temp=2*temp+2;} else {array[temp]=array[2*temp+1]; temp=2*temp+1;}} Temp is empty at the moment, and the last node value is placed in the TEMPThis position, and move temparray[temp]=array[n];array[n]=min;if (temp<n)//The node to be adjusted is also followed by a node insert (array, temp);}}
Binary heap of data structures (build heap, heap sort)-(vii)