Data structure (Java language) -- simple implementation of BinaryHeap
The priority queue is a data structure that allows at least two types of operations: insert and deleteMin. It works to locate, return, and delete the smallest element in the priority queue. The insert operation is equivalent to the enqueue queuing operation, while the deleteMin operation is equivalent to the dequeue queuing operation in the priority queue.
One method to implement the priority queue is to use the binary heap of the binary stack, which is widely used for the implementation of the priority queue, so that when heap is used in the context of the priority queue without modification, it is generally an implementation of the exponential data structure. In this section, the binary heap is called a heap. Like a binary search tree, a heap has two properties: Structure and heap sequence. Like the AVL tree, a heap operation may destroy one of these two properties. Therefore, the heap operation can be terminated only when all properties of the heap are satisfied. In fact, this is not difficult to do.
A heap is a fully filled binary tree. The possible exception is that it is at the bottom layer, and the elements at the bottom layer are filled from left to right. Such a tree is called a Complete Binary Tree. It is easy to prove that a complete binary tree with a height of h has 2 ^ h to 2 ^ (h + 1)-1 nodes. This means that the height of the Complete Binary Tree is the log n rounded down. Obviously, it is O (logN ).
An important observation found that because the full binary tree is so regular, it can be represented with an array without the need to use a chain. For the element at any position I in the array, the left son is at the position 2i, And the right son is in the unit (2i + 1) behind the left son, its father is in position I/2. Therefore, not only does the link not need to be used, but also the operations required to traverse the tree are extremely simple, and it may be very fast to run on most computers. The only problem with this implementation is that the maximum heap size needs to be estimated in advance, but this is generally not a problem.
The implementation of a binary heap is as follows:
Import java. util. noSuchElementException; import java. util. random; public class BinaryHeap> {private static final int DEFAULT_CAPACITY = 10; // default capacity private int currentSize; // current heap size private AnyType [] array; // array public BinaryHeap () {this (DEFAULT_CAPACITY) ;}@ SuppressWarnings (unchecked) public BinaryHeap (int capacity) {currentSize = 0; array = (AnyType []) new Comparable [capacity + 1];} @ SuppressWarnings (unch Ecked) public BinaryHeap (AnyType [] items) {currentSize = items. length; array = (AnyType []) new Comparable [(currentSize + 2) * 11/10]; int I = 1; for (AnyType item: items) {array [I ++] = item;} buildHeap ();}/*** creates a heap from an arbitrarily arranged project and runs the heap linearly */private void buildHeap () {for (int I = currentSize/2; I> 0; I --) {percolateDown (I );}} /*** move the element in the heap down ** @ param hole * Start subscript of the move down */private void percolateDown (int hole ){ Int child; AnyType tmp = array [hole]; for (; hole * 2 <= currentSize; hole = child) {child = hole * 2; if (child! = CurrentSize & array [child + 1]. compareTo (array [child]) <0) {child ++;} if (array [child]. compareTo (tmp) <0) {array [hole] = array [child];} else {break;} array [hole] = tmp ;} /*** insert an element ** @ param x * insert element */public void insert (AnyType x) {if (isFull () {enlargeArray (array. length * 2 + 1);} int hole = + currentSize; for (; hole> 1 & x. compareTo (array [hole/2]) <0; hole/= 2) {array [hole] = array [hole/2];} array [hole] = x ;} /*** indicates whether the heap is full ** @ return is full */public boolean isFull () {return currentSize = array. length-1;}/*** empty heap? ** @ return: Empty heap? */public boolean isEmpty () {return currentSize = 0 ;} /*** clear heap */@ SuppressWarnings (unused) public void makeEmpay () {currentSize = 0; for (AnyType anyType: array) {anyType = null ;}} /*** find the minimum element in the heap * @ return minimum element */public AnyType findMin () {if (isEmpty () return null; return array [1];} /*** Delete the smallest element in the heap * @ return Delete element */public AnyType deleteMin () {if (isEmpty () {throw new NoSuchElementException ();} anyType minItem = findMin (); array [1] = array [currentSize]; array [currentSize --] = null; percolateDown (1); return minItem ;} /*** expand the array capacity * @ param newSize new capacity */@ SuppressWarnings (unchecked) private void enlargeArray (int newSize) {AnyType [] old = array; array = (AnyType []) new Comparable [newSize]; for (int I = 0; I <old. length; I ++) {array [I] = old [I] ;}/ *** elements in the output array */public void printHeap () {for (AnyType anyType: array) {System. out. print (anyType +) ;}} public static void main (String [] args) {BinaryHeap
Heap = new BinaryHeap
(); For (int I = 0; I <20; I ++) {heap. insert (I);} heap. deleteMin (); heap. deleteMin (); heap. deleteMin (); heap. printHeap ();}}
Execution result:
Null 3 4 5 7 9 11 6 15 8 17 10 12 13 14 19 16 null