[Java]
/**
* Maximum priority queue interface for Data Structure Learning
* @ Author Sking
*/
Package heap;
Public interface MaxPriorityQueue {
/**
* Determines whether the maximum priority queue is empty.
* @ Return if the maximum priority queue is null, true is returned; otherwise, false is returned.
*/
Public boolean isEmpty ();
/**
* Returns the number of elements in the maximum priority queue.
* @ Return the maximum number of elements in a priority queue
*/
Public int size ();
/**
* Returns the "maximum" element of the maximum priority queue.
* @ Return the "maximum" element of the maximum priority queue. If the queue is empty, null is returned.
*/
Public Comparable <?> GetMax ();
/**
* Insert the specified element to the maximum priority queue.
* @ Param theObject: Elements to be inserted
*/
Public void put (Comparable <?> TheObject );
/**
* Remove the "maximum" element of the maximum priority queue and return
* @ Return the "maximum" element of the maximum priority queue. If the queue is empty, null is returned.
*/
Public Comparable <?> RemoveMax ();
}
[Java]
/**
* Maximum priority queue for Data Structure Learning queues
* Use the array heap structure to implement the maximum priority queue
* @ Author Sking *
*/
Package heap;
Import java. lang. reflect. Array;
Public class MaxHeap {
@ SuppressWarnings ("rawtypes ")
Comparable [] heap; // heap array, index position 0 not used
Int size; // number of elements
/**
* Method for constructing the initial heap array capacity
*
* @ Param initialCapacity
* Initial heap array capacity
*/
Public MaxHeap (int initialCapacity ){
If (initialCapacity <1)
Throw new IllegalArgumentException ("initialCapacity must be> = 1 ");
Heap = new Comparable [initialCapacity + 1];
Size = 0;
}
/**
* No parameter constructor. The initial size of the heap array is 10 by default.
*/
Public MaxHeap (){
This (10 );
}
/**
* Determines whether the heap array is empty.
*/
Public boolean isEmpty (){
Return size = 0;
}
/**
* Obtain the number of elements in the heap array.
*/
Public int size (){
Return size;
}
/**
* Obtain the first element ("maximum element") in the heap array ')
*/
@ SuppressWarnings ({"rawtypes "})
Public Comparable getMax (){
Return (size = 0 )? Null: heap [1];
}
/**
* Insert the maximum heap of the element guide and re-adjust it to keep the array "heap feature"
*
* Implementation Method: place the inserted element at the end of the array and recursively compare the current element
* If the Father's Day element does not meet the heap characteristics, it is recursive to the "root" side.
* Bubble (move relatively) until the parent node element of the new element is greater than "it"
*/
@ SuppressWarnings ({"rawtypes", "unchecked "})
Public void put (Comparable theElement ){
If (size = heap. length-1) {// The heap array is full and new space is allocated.
/*
* Heap. getClass (). getComponentType ()
* Get the Class object corresponding to the array element type
* Array. newInstance (Class <?>, Int)
* Specify the element type and the number of leaders to create a new array instance.
*/
Comparable [] newArray = (Comparable []) Array. newInstance (heap
. GetClass (). getComponentType (), 2 * heap. length );
// Copy the heap element to the new array.
System. arraycopy (heap, 0, newArray, 0, heap. length );
Heap = newArray;
}
Int currentNode = ++ size; // insert index for new elements
// The new element bubbles up (compare and exchange) along the path to the root until the parent node element of the new element is "greater than" it
While (currentNode! = 1
& Heap [currentNode/2]. compareTo (theElement) <0 ){
// Move the parent node element down
Heap [currentNode] = heap [currentNode/2];
CurrentNode/= 2;
}
Heap [currentNode] = theElement;
}
/**
* Remove the first element ("maximum element") of the heap array from the Max heap.
*
* Implementation Method: first fill the root node with the last element of the heap array, and then
* The position begins to recursively adjust along the path that does not meet the heap characteristics, and finds the final
* The proper position of an element to make it the largest heap. Each iteration involves
* A subtree (which contains three elements) is used to find the maximum element in the subtree as
* The root node of the front subtree. If the maximum element is the element at the end of the array, It is recursively stopped,
* Otherwise, the positions of the elements are exchanged and adjusted recursively.
*
* Note: The maximum element is saved at the beginning and used for the final return value. Therefore
* The entire operation can safely use the first element. And the position at the end of the array will be blank after adjustment.
*/
@ SuppressWarnings ({"unchecked", "rawtypes "})
Public Comparable removeMax (){
If (size = 0) // The heap array is empty.
Return null;
Comparable maxElement = heap [1]; // "maximum" element
Comparable lastElement = heap [size --]; // the last element of the heap Array
Int currentNode = 1, child = 2; // root node index and left child Index of the current subtree
While (child <= size ){
// The maximum element of the subtree is filtered out in each iteration as the root node of the subtree, And the element position is exchanged.
If (child <size & heap [child]. compareTo (heap [child + 1]) <0)
Child ++;
If (lastElement. compareTo (heap [child])> = 0)
Break; // The maximum heap features have been met
Heap [currentNode] = heap [child];
CurrentNode = child;
Child * = 2;
} // CurrentNode indicates the position of the child root node currently under investigation
Heap [currentNode] = lastElement;
Return maxElement;
}
/**
* Initialize the input array (Length: n) to the maximum heap.
* Implementation Method: Starting from the element with a child node (index is n/2), if this position is the root subtree
* If the maximum heap feature is met, no adjustment is required. Otherwise, the swap location is adjusted. Adjusted in turn I, I-1, I-2 ..
* The position is the root subtree until the first element of the array.
* @ Param theHeap the input array may not meet the heap features
* @ Param theSize: Number of input array elements
*/
@ SuppressWarnings ({"rawtypes", "unchecked "})
Public void initialize (Comparable [] theHeap, int theSize ){
Heap = theHeap;
Size = theSize;
For (int root = size/2; root> = 1; root --){
Comparable rootElement = heap [root];
Int child = 2 * root;
While (child <= size ){
If (child <size & heap [child]. compareTo (heap [child + 1]) <0)
Child ++;
If (rootElement. compareTo (heap [child])> = 0)
Break;
Heap [child/2] = heap [child];
Child * = 2;
}
Heap [child/2] = rootElement;
}
}
/**
* Print the heap array in the specified format [element 1, element 2, element 3...]
*/
Public String toString (){
StringBuffer s = new StringBuffer ();
S. append ("The" + size + "elements are [");
If (size> 0 ){
S. append (heap [1]);
For (int I = 2; I <= size; I ++)
S. append ("," + heap [I]);
}
S. append ("]");
Return new String (s );
}
}