Max heap (implemented by Java array) and Max heap java Array

Source: Internet
Author: User
Tags comparable

Max heap (implemented by Java array) and Max heap java Array
Max heap

Data [1] starts to be stored, and data [0] is empty. You can also use data [0] As the size.

 

Public class MaxHeap <T extends Comparable <? Super T> {private T [] data; private int size; private 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 the maximum root (only do not delete, compared 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 returns the maximum root (this parameter indicates deletion, compared with seekMax) */public T popMax () {swap (1, size --); shiftDown (1); return data [size + 1];} /*** @ param child node the parent node is child, and the parent node's parent table is child/2 */public void shiftUp (int child) {while (child> 1 & data [child]. compareTo (data [child/2])> 0) {swap (child, child/2); child = child/2 ;}} /*** @ param a cursor mark of an element in the data array * @ param B the cursor mark of an element in the data array * @ return the cursor mark of an element if it is large * /private int max (int, int B) {if (data [a]. compareTo (data [B]) <0) {// If data [B] Big return B; // return B} else {// If data [a] Big return; // return a}/*** @ param a data array. The cursor mark of an element * @ param B data array * @ param c data array * @ return indicates the maximum value of an element. */private int max (int, int B, int c) {int biggest = max (a, B); biggest = max (biggest, c); return biggest ;} /*** @ param father: the parent node is named father. The parent tables of the left and right child nodes are: father * 2 and father * 2 + 1 */public void shiftDown (int father) {while (true) {int lchild = father * 2; // The left child int rchild = father * 2 + 1; // The right child int newFather = father; // newFather is about to be updated, newFather is the if (lchild> size) {// if the father node has neither the left child nor the right child return;} else if (rchild> size) {// if the father node has only the left child and no right child newFather = max (father, lchild);} else {// if the father node has both the left child, another right child, newFather = max (father, lchild, rchild);} if (newFather = father) {// It indicates that father is larger than both child nodes, the table name is already a big root heap, so you do not need to adjust the return;} else {// otherwise, you still need to adjust the heap until the big root heap conditions are met until swap (father, newFather ); // values are exchanged for father = newFather; // update the father value, which is equivalent to continuing to adjust shiftDown (newFather) }}public static void main (String [] args) {// create a large root heap MaxHeap <Integer> maxHeap = new MaxHeap <Integer> (100); // store for (int I = 0; I <100; I ++) {maxHeap. insert (int) (Math. random () * 100);} // create an array Integer [] arr = new Integer [100]; // retrieve from the heap, put in the array for (int I = 0; I <100; I ++) {arr [I] = maxHeap. popMax (); System. out. print (arr [I] + "");} System. out. println ();}}
Max heap: The shiftDown () function is different from the above.
Public class MaxHeap <T extends Comparable <? Super T> {private T [] data; private int size; private 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: The maximum root is displayed. (This parameter indicates deletion. It is compared with seekMax) */public T popMax () {T ret = data [1]; swap (1, size); size --; shiftDown (1); return ret ;} /*** @ return view the maximum root (only do not delete, compared 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] The newFather = newFather + 1 ;} if (data [father]. compareTo (data [newFather])> = 0) {break;} else {swap (father, newFather); // values are exchanged for father = newFather; // newFather is (2 * father) or (2 * father + 1), that is, continue shiftDown (newFather) ;}} public static void main (String [] args) {// create a large root heap MaxHeap <Integer> maxHeap = new MaxHeap <Integer> (100); // store for (int I = 0; I <100; I ++) {maxHeap. insert (int) (Math. random () * 100);} // create an array Integer [] arr = new Integer [100]; // retrieve from the heap, put in the array for (int I = 0; I <100; I ++) {arr [I] = maxHeap. popMax (); System. out. print (arr [I] + "");} System. out. println ();}}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.