Chapter 6 _ implementing priority queue

Source: Internet
Author: User
Public class priority_queue {// storage array private int A []; // heap size private int pile_size = 0; // If a specified value is found, return-1 private int not_find =-1; // Private int increat_capcity = 20; // heap container size Private Static int int_num = 20; // The constructor 1 _ does not specify the heap container size. The container size is set to the default size. Value: int_num: 20; Public priority_queue () {This (int_num );} // constructor 2__ specifies the size of the heap container public priority_queue (INT capcity) {A = new int [capcity];} // inserts a value into the container. Considering multithreading, so join synchronizedpublic sync Hronized void insert (INT value) {If (getheap_size () =. length) {increate_capcity ();} A [pile_size] = value; query_suitable_index (pile_size ++);} // modify the value of public synchronized Boolean modify (INT index, int value) {If (index <0 | index> pile_size) {return false;} If (value <A [Index]) {return false ;} A [Index] = value; query_suitable_index (INDEX); return true;} // modify or add elements. As a result, the container does not meet the heap nature, so it needs to be initialized ", make the container meet the public void query_s of the heap nature Uitable_index (INT index) {While (parent (INDEX )! = Not_find & index> = 1 & A [Parent (INDEX)] <A [Index]) {int exchange = A [Index]; A [Index] = A [Parent (INDEX)]; A [Parent (INDEX)] = exchange; Index = parent (INDEX );}} // increase the container size public void increate_capcity () {int [] B = new int [. length + increat_capcity]; for (INT I = 0; I <. length; I ++) {B [I] = A [I] ;}a = B ;}// obtain the number of container elements. Public int getheap_size () {return pile_size ;} // get the left child public int left_chiren (int I) {int left = I * 2 + 1; retu RN (left <pile_size )? Left: not_find;} // get the right child public int right_chiren (int I) {int right = I * 2 + 2; Return (right <pile_size )? Right: not_find;} // get the Parent and Child Nodes public int parent (int I) {int parent = (I-1)/2; Return (parent> = 0 )? Parent: not_find;} // initialize the heap container from the specified node "! Public void max_pile (int I) {int left_chiren = left_chiren (I); int right_chiren = right_chiren (I); int largest; If (left_chiren! =-1 & A [I] <A [left_chiren]) {largest = left_chiren;} else {largest = I;} If (right_chiren! =-1 & A [Largest] <A [right_chiren]) {largest = right_chiren;} If (largest! = I) {int largetvalue = A [Largest]; A [Largest] = A [I]; A [I] = largetvalue; max_pile (largest );}} // sort the heap public void heap_sort () {// sort from small to large for (INT I = pile_Size-1; I> = 1; I --) {int exchange = A [I]; A [I] = A [0]; A [0] = exchange; pile_size --; max_pile (0 );}} // obtain the largest public int getmax () {return a [0];} // obtain the largest element in the heap and delete the largest public int extract_max () {If (pile_size <1) {return not_find;} int max = A [0]; A [0] = A [pile_size --]; max_pile (0); Return Max ;} // print @ overridepublic string tostring () {stringbuilder sb = new stringbuilder (); For (INT I = 0; I <pile_size; I ++) Sb. append (A [I] + ""); return sb. tostring () ;}// test public static void main (string [] ARGs) {priority_queue pile = new priority_queue (); system. out. println ("--------------- test the insert () method ------------------------"); For (INT I = 1; I <5; I ++) {pile. insert (I); system. out. println ("Add new element:" + I); system. out. println (pile);} system. out. println ("--------------- test the extract_max () method -------------------"); system. out. println ("Maximum number of heap containers:" + pile. extract_max (); system. out. println ("Delete the maximum number in the heap, and the container changes to:"); system. out. println (pile );}}


Output:

--------------- Test the insert () method --------------------------
Add new elements: 1
1
Add new elements: 2
2 1
Add new elements: 3
3 1 2
Add new elements: 4
4 3 2 1
--------------- Test the extract_max () method ---------------------
Maximum number of heap containers: 4
Delete the maximum number in the heap. Then, the container changes:
3 0 2

Chapter 6 _ implementing priority queue

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.