The maximum heap is characterized by a parent element larger than the child element and a complete binary tree.
Heap Interface:
/**
* Create by ZXB on 2017/9/10/public
interface Iheap<t extends comparable<t>> {
void di Splay ();
void Initoriginlist (list<t> orginlist);
void Makeheap (INT-A, last);
void Popheap (INT-A, last);
void Pushheap (INT-A, last);
List<t> getheap ();
}
Heap implementation, where a dynamic array is used to store
/** * Create by ZXB on 2017/9/10/public class Heapimpl<t extends comparable<t>> implements Iheap<t>
{Private list<t> heap;
Private list<t> orginlist;
public void Initoriginlist (list<t> orginlist) {this.orginlist = orginlist;
Public list<t> Getheap () {return heap;
Public Heapimpl () {this.heap = new arraylist<> (); /** * Insert corresponds to float * * @param start */protected void Adjustup (int start) {int Currentin
Dex = start;
int parentindex = (currentIndex-1)/2;
T-tmp = Heap.get (currentindex);
while (Currentindex > 0) {int cmp = Heap.get (parentindex). CompareTo (TMP);
if (CMP >= 0) {break;
else {heap.set (Currentindex, Heap.get (Parentindex));
Currentindex = Parentindex;
Parentindex = (parentIndex-1)/2; } heap.set (Currentindex, TMP);
public void Insert (T data) {int size = Heap.size (); Heap.add (data); Inserts an "array" at the footer Adjustup (size);
Adjust heap} public void remove (int index) {int size = Heap.size ();
Heap.set (Index, Heap.get (size-1));
Heap.remove (size-1);
Adjustdown (index); /** * Delete corresponds to sink * * @param index */private void Adjustdown (int index) {int Currentin
Dex = index;
int leftchildindex = Index * 2 + 1;
int rightchildindex = index * 2 + 2;
T-tmp = Heap.get (currentindex);
int size = Heap.size ();
while (Leftchildindex < size) {T left = null;
T right = null;
if (Leftchildindex < size) {left = Heap.get (Leftchildindex);
} if (Rightchildindex < size) {right = Heap.get (Rightchildindex);
} int maxindex = right = null?
Leftchildindex: (Left.compareto (right) >= 0? leftchildindex:rightchildindex);
T max = Heap.get (Maxindex);
if (Tmp.compareto (max) >=0) {break;
}else{Heap.set (Currentindex, Max);
Heap.set (Maxindex, TMP);
Leftchildindex = Maxindex * 2 +1;
Rightchildindex = Maxindex +1;
}} public void Makeheap (INT-A, last) {for (int i =-i < last; i++) {
Insert (Orginlist.get (i));
} public void Popheap (INT-A, last) {remove (a);
public void Pushheap (INT-A, last) {Adjustup (last-1);
public void display () {System.out.println (heap);
public static void Main (string[] args) {iheap<integer> heap = new heapimpl<> (); Heap.initoriginlist (Arrays.aslist (10, 20, 30, 5, 15));
SYSTEM.OUT.PRINTLN ("Initial build heap:");
Heap.makeheap (0, 5);
Heap.display ();
System.out.println ("Pop-up heap Top:");
Heap.popheap (0, 5);
Heap.display ();
System.out.println ("Pop-up heap Top:");
Heap.popheap (0, 4);
Heap.display ();
System.out.println ("Insert heap Tail:");
Heap.getheap (). Add (90);
Heap.display ();
System.out.println ("rearrange:");
Heap.pushheap (0, 4);
Heap.display (); }
}Run Result: