Java implementation heap Encapsulation, inserts, adjusts, deletes heap top to complete heap sort instance __java

Source: Internet
Author: User

Brief introduction

Heap for sorting algorithm is a more common data structure, I use the Java language to implement this algorithm

First, we need to know the structure of the heap of the form, in fact, is a special two-fork tree. But this binary tree has certain characteristics, in addition to the complete binary tree, for the maximum heap, the heap top element value is the largest, and for each subtree of the heap is also a small number of the largest heap; similarly for the smallest heap, the nature of the opposite is OK.

I take the largest heap as an example:
To implement the initialization of the heap, you first create a complete binary tree according to the given element, and then adjust the process continuously from the end node. The principle of adjustment is: to compare the size of the current node and its parent node to be placed, and to place the current node with a value less than its parent node, the current node is in the same location as the maximum heap, and the current node to place is more appropriate. If the value of the current node being placed is greater than the value of its parent node, then it is not appropriate to place the current node, then the value of the current node is exchanged with the value of its parent node, and the original parent node becomes the new current node to be placed. The end condition is that the current node does not have a parent node, but the adjustment may not be over, and we just need to get the top element of the heap to be the value to be inserted. At this point, the maximum heap insertion and adjustment process is complete.
The code is as follows:

public boolean insert (int x) {
        if (currentsize==maxsize) {
            System.out.println ("Sorry,this heap is full!");
            return false;
        }
        If a heap of dissatisfied words
        currentsize++;
        int flag=currentsize-1;
        while (flag>0) {
            int parent= (flag-1)/2;
            if (heap[parent]>x) {
                heap[flag]=x;
                return true;
            } else{
                heap[flag]=heap[parent];
                Flag=parent
            }
        }
        heap[0]=x;
        return true;
    }

Siftdown process: Given the position of a node, adjust it to fit the maximum heap definition, this process is the process we want to implement. The principle of adjustment is as follows:
For the current node I, the subscript for the child node satisfies the left node as 2*i+1 and the right node is 2*i+2; During the adjustment process, you only need to compare the current node with the largest node in its child nodes to adjust it. The specific code logic can be seen in the code:

public void Siftdown (int flag) {
        int want=flag;
        int X=heap[flag];

        while (want<currentsize) {
            int lchild=2*want+1;
            int rchild=2*want+2;
            int maxchildnumber;
            if (lchild>currentsize) {  //No child node
                heap[want]=x;
            } else{                   //has two children node
                if (lchild<currentsize) {
                    maxchildnumber=heap[lchild]>heap[rchild]?lchild: Rchild;
                } else{
                    maxchildnumber=lchild;
                }
                if (heap[maxchildnumber]<x) {
                    heap[want]=x;return;
                } else{
                    Heap[want]=heap[maxchildnumber];
                    Want=maxchildnumber}}}}

    

The deletion of the top of the heap, our operation of the heap basic Mulberry is to obtain the maximum value of this heap, then there is no doubt that the top of the heap is the object we want to study. The following is the code logic:

public int deletetop () {
        if (currentsize<0) {
            System.out.println ("Sorry, this heap is empty!");
            return-1;
        }
        int target=heap[0];
        int substitute=heap[currentsize-1];
        this.currentsize--;
        Heap[0]=substitute;
        Siftdown (0);
        return target;
    }

The following is a detailed code

Package test.maxheap;
    public class Maxheap {private int []heap;
    private int currentsize;

    private static int MAXSIZE;
        public maxheap (int n) {heap=new int[n];
        currentsize=0;
    Maxsize=n; public boolean insert (int x) {if (currentsize==maxsize) {System.out.println ("Sorry,this heap
            Full! ");
        return false;
        //If a heap of discontent currentsize++;
        int flag=currentsize-1;
            while (flag>0) {int parent= (flag-1)/2;
                if (heap[parent]>x) {heap[flag]=x;
            return true;
                }else{Heap[flag]=heap[parent];
            Flag=parent;
        }} heap[0]=x;
    return true;
        public void Siftdown (int flag) {int want=flag;

        int X=heap[flag];
            while (want<currentsize) {int lchild=2*want+1;
            int rchild=2*want+2; int Maxchildnumber;
            if (lchild>currentsize) {//No child node heap[want]=x; }else{//has two children node if (lchild<currentsize) {Maxchildnumber=heap[lchi
                ld]>heap[rchild]?lchild:rchild;
                }else{Maxchildnumber=lchild;
                } if (heap[maxchildnumber]<x) {Heap[want]=x;return;
                    }else{Heap[want]=heap[maxchildnumber];
                Want=maxchildnumber; public int Deletetop () {if (currentsize<0) {System.out.println}}}}
            ("Sorry, this heap is empty!");
        return-1;
        int target=heap[0];
        int substitute=heap[currentsize-1];
        this.currentsize--;
        Heap[0]=substitute;
        Siftdown (0);
    return target;
 }

}

Well, the code is complete. Let's check to see if we're right.

public class Maxheaptest {public

    static void main (String []args) {
        maxheap maxheap=new maxheap (7);
        for (int i=1;i<=7;i++) {
            maxheap.insert (i);
        }
        for (int i=0;i<7;i++) {
            System.out.print (maxheap.deletetop () + "   ");
        }
        System.out.println ("\ n");
    }

Next is the result of the program's operation:

7   6   5   4   3 2 1   
//visible, for the largest heap, the removal of the heap top operation actually completes the sort task of the heap and proves that our code is correct.

Summarize:
The operation of the heap is very important, we should learn more about the application of the heap, such data structure can make the program run more efficient and smooth. For the smallest heap, we only need to insert the method, the Sift method within a little modification can be (that is, the value of the distribution of the distribution of the relationship to adjust). This will also enable minimal heap creation and related operations.
The code may exist in the wrong place, I hope you will be criticized and look forward to working with you to progress together.

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.