Java Data Structure-heap

Source: Internet
Author: User

1. It is convenient to store the heap data by using arrays, And the array position 0 is used as the Sentinel.

Package com. jzm. heapmap; public class maxheap <t extends comparable <? Super T> {private T [] heap; // Private int lastindex that stores the array of heap elements; // Private Static final int default_capacity = 20 for the index of the last element; public maxheap () {This (default_capacity);} public maxheap (INT size) {heap = (T []) New comparable [size]; lastindex = 0 ;} // end constructor public Boolean isempty () {return lastindex <1;} // determines whether it is empty public void doublearray () {int enlarge = heap. length * 2; t [] A = (T []) New comp Arable [enlarge]; for (INT I = 1; I <= lastindex; I ++) {A [I] = heap [I];} heap = ;} // expand the array space public void add (T newentry) {lastindex ++; If (lastindex> = heap. length) {doublearray ();} int newindex = lastindex; int parentindex = newindex/2; while (newindex> 1 & newentry. compareto (heap [parentindex])> 0) {heap [newindex] = heap [parentindex]; newindex = parentindex; parentindex = newindex/2;} // end while heap [n Ewindex] = newentry;} // end add private void reheap (INT rootindex) {Boolean done = false; t orphant = heap [rootindex]; int largerchildindex = 2 * rootindex; while (! Done & largerchildindex <= lastindex) {int leftchildindex = largerchildindex; int rightchildindex = leftchildindex + 1; if (rightchildindex <= lastindex & heap [rightchildindex]. compareto (heap [largerchildindex])> 0) {largerchildindex = rightchildindex;} // find the largest child if (orphant. compareto (heap [largerchildindex]) <0) {heap [rootindex] = heap [largerchildindex]; rootindex = largerchildindex; largerchildindex = 2 * rootindex;} else {done = true;} // end while heap [rootindex] = orphant;} // end reheap public t removemax () {t root = NULL; if (! Isempty () {root = heap [1]; // return value heap [1] = heap [lastindex]; // form a half-heap lastindex --; // The heap size minus 1 reheap (1); // convert to heap} // end if return root;} // end removemax public t getmax () {t root = NULL; If (! Isempty () {root = heap [1];} return root;} // end getmax to get the top heap number public int getsize () {return lastindex ;} // obtain the public void clear () {for (; lastindex> = 0; lastindex --) {heap [lastindex] = NULL; lastindex = 0 ;}} of the heap size ;}} // end clear the public void display () {for (INT I = 1; I <= lastindex; I ++) {system. out. println ("" + heap [I]) ;}} public static void main (string [] ARGs) {maxheap <integer> maxheap = new maxheap <integer> (); maxheap. add (1); maxheap. add (2); maxheap. add (5); maxheap. add (6); maxheap. add (3); maxheap. add (4); maxheap. display (); maxheap. removemax (); system. out. println ("---------------"); maxheap. display ();}}

 

2. Heap sorting

Package com. jzm. heapmap; public class heapsort {Private Static <t extends comparable <? Super T> void reheap (T [] heap, int rootindex, int lastindex) {Boolean done = false; t orphant = heap [rootindex]; int largerchildindex = 2 * rootindex; while (! Done & largerchildindex <= lastindex) {int leftchildindex = largerchildindex; int rightchildindex = leftchildindex + 1; if (rightchildindex <= lastindex & heap [rightchildindex]. compareto (heap [largerchildindex])> 0) {largerchildindex = rightchildindex;} // find the largest child if (orphant. compareto (heap [largerchildindex]) <0) {heap [rootindex] = heap [largerchildindex]; rootindex = largerchildindex; largerchildindex = 2 * rootindex;} else {done = true;} // end while heap [rootindex] = orphant;} // end reheap Private Static <t extends comparable <? Super T> void swap (T [] A, int I, Int J) {T = A [I]; A [I] = A [J]; A [J] = T;} public static <t extends comparable <? Super T> void heapsort (T [] array, int N) {// create an initial heap for (INT rootindex = n/2-1; rootindex> = 0; rootindex --) {reheap (array, rootindex, n-1); For (INT I = 0; I <array. length; I ++) {system. out. print ("" + array [I]);} system. out. println ("\ n -------------------------");} swap (array, 0, n-1); For (INT lastindex = n-2; lastindex> 0; lastindex --) {reheap (array, 0, lastindex); swap (array, 0, lastindex);} // end for} // end heapsort public static void main (string [] ARGs) {INTEGER [] A = {20, 40, 30, 10, 90,70}; heapsort (A,. length); For (INT I = 0; I <. length; I ++) {system. out. print ("" + A [I]) ;}}

 

 

 

 

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.