Maximum Heap
DATA[1] Start saving, data[0] empty. You can also use data[0] as size.
public class Maxheap<t extends Comparable<? Super t>> {protected t[] data; protected int size; protected int capacity; public maxheap (int capacity) {This.data = (t[]) new Comparable[capacity + 1]; size = 0; This.capacity = capacity; } public int size () {return this.size; } public boolean IsEmpty () {return size = = 0; } public int getcapacity () {return this.capacity; }/** * @return View maximum root (see not delete, compare with Popmax) */public T Seekmax () {return data[1]; } public void Swap (int i, int j) {if (i! = j) {T temp = data[i]; Data[i] = Data[j]; DATA[J] = temp; }} public void Insert (T item) {size++; Data[size] = Item; Shiftup (size); }/** * @return pop up maximum root (popup means delete, compared to Seekmax) */public T Popmax () {swap (1, size--); Shiftdown (1); return data[size + 1]; }/** * @paramChild children node The next corner mark is CHILD/2 */public void Shiftup (int.) {while (Kids > 1 && data[c Hild].compareto (DATA[CHILD/2]) > 0) {swap (child, CHILD/2); Child = CHILD/2; }}/** * @param the lower corner of an element in the data array * @param the lower corner of an element in the B data array * @return which element is large returns the lower corner of the label */private int max (int a, int b) {if (Data[a].compareto (Data[b]) < 0) {//if DATA[B] Big return b;//returns B} el SE {//if DATA[A] big return a;//Returns a}}/** * @param the lower corner of an element in the data array * @param the element in the B data array Subscript * @param the lower corner of an element in the C data array * @return which element returns the lower corner of the index * */private int max (int A, int b, int c) {in T biggest = Max (A, b); Biggest = Max (biggest, c); Return biggest; }/** * @param father the bottom corner of the parent node is father, the lower corner table of the left and right two child nodes are: father*2 and father*2+1 */public void Shiftdown (int fathe R) {while (true) {int lchild = father * 2;//left child int rchild = father * 2 + 1;//right child int newfather = Father;//newfather is about to be updated, parent, left, and right three nodes who are big, Newfather is who's lower corner mark if (Lchild > Size) {//If the Father node has neither left child nor right child return; } else if (Rchild > Size) {//If the Father node has only left child, there is no right child Newfather = max (father, Lchild); } else {//If the Father node has both a left child and a right child Newfather = max (father, Lchild, rchild); if (Newfather = = father) {//Description father is larger than two sub-nodes, the table name is already a large heap, do not continue to adjust the return; } else {///otherwise, you will need to continue adjusting the heap until the large root heap condition is met (father, newfather);//value is exchanged father = newfather;//update fat Her value, equivalent to continuing to adjust Shiftdown (Newfather)}}} public static void Main (string[] args) {//Create large root heap maxheap<integer> maxheap = new maxheap<integer> (100); Deposit for (int i = 0; i < i++) {maxheap.insert (int) (Math.random () * 100) to the heap; }//create array integer[] arr = new integer[100]; Take from the heap, put in the array for (int i = 0; i < i++) {Arr[i] = Maxheap.popmax (); System.out.print (Arr[i] + ""); } System.out.println (); }}
public class Maxheap<t extends Comparable<? Super t>> {protected t[] data; protected int size; protected int capacity; public maxheap (int capacity) {data = (t[]) new Comparable[capacity + 1]; This.capacity = capacity; size = 0; } public int size () {return size; } public boolean IsEmpty () {return size = = 0; } public void Insert (T item) {data[size + 1] = Item; size++; Shiftup (size); }/** * @return pop up maximum root (popup means delete, compared to Seekmax) */public T Popmax () {t ret = data[1]; Swap (1, size); size--; Shiftdown (1); return ret; }/** * @return View maximum root (see not delete, compare with Popmax) */public T Seekmax () {return data[1]; } public void Swap (int i, int j) {if (i! = j) {T temp = data[i]; Data[i] = Data[j]; DATA[J] = temp; }} public void Shiftup (int k) {while (K > 1 && Data[k/2].compareto (Data[k]) < 0) {swap (k, K/2); K/= 2; }} public void Shiftdown (int father) {while (2 * father <= size) {int newfather = 2 * father ; if (Newfather + 1 <= size && data[newfather + 1].compareto (Data[newfather]) > 0) {//data[j] data[j+1] Both take the bigger one. Newfather = Newfather + 1; } if (Data[father].compareto (Data[newfather]) >= 0) {break; } else {swap (father, newfather);//value is exchanged father = Newfather;//newfather Yes (2*father) or Yes (2*fath ER+1), i.e. continue Shiftdown (newfather); }}} public static void Main (string[] args) {//create Dagen maxheap<integer> maxheap = new Max heap<integer> (100); Deposit for (int i = 0; i < i++) {maxheap.insert (int) (Math.random () * 100) to the heap; }//create array integer[] arr = new integer[100]; Take from the heap, put in the array for (int i = 0; i < i++) {Arr[i] = Maxheap.popmax (); System.out.print (Arr[i] + ""); } System.out.println (); }}
Maximum Heap (Java array Implementation)