Data structure Java version of Heap & heap sequencing (ix)

Source: Internet
Author: User

Heaps are divided into large top piles, and small top piles. What is a heap? A heap can be thought of as a binary tree, and the element of a binary tree is an array of constant rotation placed from left to right. If the heap is large, then the large number is placed on the upper layer, and the small number is placed below the layer. The number of the previous layer must be greater than the number of the next layer. The small top pile is the opposite.

So, how to implement a big top heap? Here I use a linked list to implement.

The implementation of the heap is simple, just keep in mind his principles.

Add a new element: Add to the end of the array. It then bubbles up to the end of the node in a continuous upward layer. Until a suitable node placement is found.

Delete element: Remove an element from the end of the array to overwrite the element that is currently being deleted, and then delete the element at the end. It then bubbles down from the current node continuously. You can find a suitable location for placement.

Relationship between the upper and lower elements: the upper-level element (indexed as: index) and the underlying element index exist 2 * index + 1 (left child node) 2 * index + 2 (right child node) relationship.

Realize the source code:

 PackageHeap;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;Importorg.junit.Test; Public classHeap {PrivateList<integer>Heaparray;  PublicHeap () {Super();  This. Heaparray =NewArrayList (); }    //adding elements into the heap     Public voidPush (Integer x) {heaparray.add (x); Trickleup (Heaparray.size ()-1); }    //Delete Element     Public voidPop () {Heaparray.set (0, Heaparray.get (heaparray.size ()-1)); Heaparray.remove (Heaparray.size ()-1); Trickledown (0); }    //Remove Root Data     PublicInteger Top () {returnHeaparray.get (0); }    //determines whether the empty     Public BooleanIsEmpty () {if(Top () = =NULL) {            return true; }        return false; }    //penetrate Upward     Public voidTrickleup (intindex) {        intParent = (index-1)/2; Integer Bottom=Heaparray.get (index);  while(Index > 0 && heaparray.get (parent) <bottom)            {Heaparray.set (index, Heaparray.get (parent)); Index=parent; Parent= (parent-1)/2;    } heaparray.set (index, bottom); }    //downward penetration     Public voidTrickledown (intindex) {Integer Top= Heaparray.get (0); intLagerchild;  while(Index < Heaparray.size ()/2) {            intLeftchild = Index * 2 + 1; intRightchild = Index * 2 + 2; if(Rightchild < Heaparray.size () && heaparray.get (Leftchild) <Heaparray.get (Rightchild)) {Lagerchild=Rightchild; }Else{lagerchild=Leftchild; }            if(Top >=Heaparray.get (Lagerchild)) {                 Break;            } heaparray.set (Index, Heaparray.get (Lagerchild)); Index=Lagerchild;    } heaparray.set (index, top); }}

Test procedure:

@Test  Public void Fun () {    new  Heap ();    P.push (a);    P.push (+);    P.push (a);    System.out.println (P.top ());    P.push (+);    P.push (+);    System.out.println (P.top ());    P.pop ();    System.out.println (P.top ());}

Test results:

309035

Heap Sort:

Heap ordering is very simple, using the top of the top heap must be the maximum value inside the heap element. Then we can put a series of data in, and then constantly take out the top element. The removed element is a well-ordered element.

Source:

  

     Public Static void Main (string[] args) {        new  Heap ();        H.push (2);        H.push (1);        H.push (4);        H.push (6);        System.out.println (H.top ());        H.pop ();        System.out.println (H.top ());        H.pop ();        System.out.println (H.top ());        H.pop ();        System.out.println (H.top ());    }

Results:

6421

At this point, the heap sort is ready.

Data structure Java version of Heap & heap sequencing (ix)

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.