About Bloggers skywang123456 article - questioning the binomial heap (c) of the Java binary heap (c) implementation of Java

Garden blog blogger skywang123456 (hereinafter referred to as s blogger) is a large cattle-class people, I believe that many programmers have read his blog, I am no exception, and benefit. But for articles to achieve binary heap (c) of the Java I have some doubts, write here for a nice ring reference. For the inserted binary heap, it is a relatively simple method, there is no problem. But while the binary heap deletion is indeed operating a bit more complicated, in fact, I first read this blog when he felt a little trance unclear. In general, while the binary heap Delete to delete top of the heap is divided into type and search deleted. Top of the heap can be achieved by a lookup type delete delete name suggests Italy, delete data directly to the top of the heap, is widely used in practical applications in binary heap. The first thing to look for delete type found in the location of a given parameter, then remove the element. And I want to point out the problem is to use code to delete blogger s top of the heap instead of the deleted code lookup type, which I will illustrate two are different.

 

First look at bloggers s delete the code

public  int Remove (T data) {
     // If the "heap" is empty, return -1 
    IF (mHeap.isEmpty () == to true )
         return -1 ; 

    // Get index into the array of data 
    int index = mHeap. the indexOf (Data);
     IF (index == -. 1 )
         return -1 ; 

    int size = mHeap.size (); 
    mHeap.set (index, mHeap.get (size -1)); // padded with the last element 
    mHeap. the Remove (size - 1);                 // delete the last element 

    IF (mHeap.size ()> 1 ) 
        filterdown (index, mHeap.size ()-1);     // start index number from the top-down position is adjusted to the minimum heap 

    return 0 ; 
}
protected  void filterdown ( int Start, int End) {
     int C = Start;           // current position (Current) node 
    int L = 2 * C +. 1;      // left (left) child position 
    T tmp = mHeap.get ( C);     // current (current) size of the node 

    the while (L <= End) {
         int CMP = mHeap.get (L) .compareTo (mHeap.get (. 1 + L ));
         // "L" is the left child , "l + 1" is the right child of 
        IF (L <End && CMP <0 ) 
            L ++;         // about two children selected larger, i.e. mHeap [L +. 1] 
        CMP = tmp.compareTo(mHeap.get(l));
        if(cmp >= 0)
            break;        //调整结束
        else {
            mHeap.set(c, mHeap.get(l));
            c = l;
            l = 2*l + 1;   
        }       
    }   
    mHeap.set(c, tmp);
}

It will be appreciated, the found node heap data padding the tail, the end of the heap after deleting the data, then the adjustment of the node from the stack down to a minimum. (Visual typo, should be the biggest heap)

Bloggers s to delete two examples in the text. The first element is to remove the top of the heap, because there is no root node, so

 

 

 

Guess you like

Origin www.cnblogs.com/lbrs/p/11827422.html