Java-based heap operation methods (heap creation, insertion, and deletion) and java operation methods

Source: Internet
Author: User

Java-based heap operation methods (heap creation, insertion, and deletion) and java operation methods

As follows:

Import java. util. arrays; // the code of the small top Heap implements public class Heap {// downward adjustment. The top Heap is mainly used to delete and create a Heap. I indicates the node index to be adjusted, n indicates that the heap has one element index at most. // when the index is deleted, the value I is 0, during Heap creation, I adjusted public static void fixDown (int [] data, int I, int n) from the parent node of the last node in sequence {int num = data [I]; int son = I * 2 + 1; while (son <= n) {if (son + 1 <= n & data [son + 1] <data [son]) son ++; if (num <data [son]) break; data [I] = data [son]; I = son; son = I * 2 + 1 ;} data [I] = num;} // adjust the value upwards, and the small value goes up, It is used to add and adjust upwards without the need to create the top index. It must be 0 public static void fixUp (int [] data, int n) {int num = data [n]; int father = (n-1)/2; // data [father]> num is the basic condition for entering the loop, if father is reduced to 0, it will not be reduced. // when n is equal to 0, father = 0; enters the endless loop. Therefore, when n = 0, you need to jump out of the loop while (data [father]> num & n! = 0) {data [n] = data [father]; n = father; father = (n-1)/2;} data [n] = num ;} // delete. n indicates that the index of the last element of the heap is public static void delete (int [] data, int n) {data [0] = data [n]; data [n] =-1; fixDown (data, 0, n-1);} // Add, I indicates the number to be added, and n indicates the index of the added position, is the last element of the heap public static void insert (int [] data, int num, int n) {data [n] = num; fixUp (data, n );} // create a heap. n indicates the index public static void creat (int [] data, int n) {for (int I = (n-1) of the last element to be created) /2; I> = 0; I --) fixDown (data, I, n);} public static void main (String [] args) {int [] data = {15, 13, 1, 5, 20, 12, 8, 9, 11}; // test the heap creation creat (data, data. length-1); System. out. println (Arrays. toString (data); // test the delete (data, data. length-1); delete (data, data. length-2); System. out. println (Arrays. toString (data); // test insert (data, 3, data. length-2); System. out. println (Arrays. toString (data ));}}

The above java Implementation heap operation method (heap creation, insertion, and deletion) is all the content shared by the editor. I hope to give you a reference, and I hope you can also support the help house.

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.