Data structure and algorithm (Java version) _ Heap

Source: Internet
Author: User

A complete binary tree is called a heap.

A complete binary tree is the last node before the node is not allowed to be dissatisfied, that is, the hole is not allowed.

You can use an array to do a complete binary tree (heap).

The heap is divided into large top piles and small top piles. The Big top heap is the largest number on the root node, and the small top heap is the smallest heap of numbers on the root node.

The operations inside the heap include two types: inserting new nodes and deleting the root node.

Penetrates the operation of the new node as it is inserted. The action to remove the root node is to infiltrate down.

When inserting a new node, insert the new node into the last position and slowly penetrate upward (swap with your father). When the root node is deleted, the last node is placed on the root node and then slowly penetrates down (and the descendants are exchanged).

Use Java to write a large top heap below.

 PackageHeap; Public classMaxheap {Private int[] heaparray; Private intmaxSize; Private intcurrentsize; /*constructor Function*/     PublicMaxheap (intmxthrowsException {if(MX < 1)Throw NewException ("max size must be >=1"); MaxSize=MX; CurrentSize= 0; Heaparray=New int[MaxSize]; }    /*penetrate upward, index is subscript*/    Private voidTrickleup (intindex) {        intParent = (index-1)/2; intBottom =Heaparray[index];  while(Index>0 && heaparray[parent]<bottom) {Heaparray[index]=Heaparray[parent]; Index=parent; Parent= (parent-1)/2; } Heaparray[index]=Bottom; }    /*downward penetration*/    Private voidTrickledown (intindex) {        intLargerchild; inttop =Heaparray[index];  while(Index < CURRENTSIZE/2){            intLeftchild = 2 * index + 1; intRightchild = 2 * index + 2; if(Rightchild<currentsize && heaparray[leftchild]<Heaparray[rightchild]) largerchild=Rightchild; ElseLargerchild=Leftchild; if(Top >=Heaparray[largerchild]) Break; Heaparray[index]=Heaparray[largerchild]; Index=Largerchild; } Heaparray[index]=top; }     Public BooleanIsEmpty () {returnCurrentSize = = 0; }     Public voidPush (intNumthrowsexception{if(currentsize = = maxSize)Throw NewException ("Maxheap ID full"); Heaparray[currentsize]=num;        Trickleup (currentsize); CurrentSize++; }     Public intTop () {returnHeaparray[0]; }     Public voidPop () {heaparray[0]=heaparray[--CurrentSize]; Trickledown (0); }     Public Static voidMain (string[] args)throwsException {System.out.println ("Test the Big Top pile"); Maxheap Maxheap=NewMaxheap (100);        System.out.println (Maxheap.isempty ()); Maxheap.push (20); Maxheap.push (30); Maxheap.push (40);        System.out.println (Maxheap.top ()); Maxheap.push (90); Maxheap.push (80); Maxheap.push (70);        System.out.println (Maxheap.top ());        Maxheap.pop ();        System.out.println (Maxheap.top ());        Maxheap.pop ();        System.out.println (Maxheap.top ());        Maxheap.pop ();    System.out.println (Maxheap.top ()); }}

A heap sort is an iteration of the top element of a pop-up stack.

The time complexity of heap sequencing is O (NLOGN).

Data structure and algorithm (Java version) _ Heap

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.