(Data structure) stack, binary heap (java implementation)

1. heap is a complete binary tree:

2. There are two important heap formula:

Known parent index, left = 2 * parent + 1 right = 2 * parent + 2

Known child subscript (either right or left) parent = (child-1) / 2;

3. The stack 1) is a complete binary tree logic 2) is an array of physical

4. meet anywhere value> = the value of its left and right children piles

Conversely <= small reactor

5.

About heap has the following important actions:

Downward adjustment (stack of) piles:

Premise: In the entire stack, in addition to adjust the position of the remaining positions have been met, the nature of the heap

0) If you want to adjust the position of a leaf node, the adjustment is completed

1) found two children in one of the largest child

2) To adjust the position and the value of comparing the maximum child

a. If the nature of the heap satisfied, the adjustment is completed

b. Otherwise, the two exchange values, continue to adjust the original oldest child subscript position

Only left child = "left child

Around there, left her children = "left child

Around there, right child Ta = "right child


 /**
     * 前提:除了index和它的孩子外,其他位置已经满足堆的性质了,调整堆,调整为大堆,向下调整
     * @param array 被看作堆的数组
     * @param size  数组中被看作堆的值的个数
     * @param index 要调整位置的下标
     */
    public static void heapify(int [] array,int size,int index){
        while(true){
            int left=2*index+1;
            if(left>=size){
                return;
            }
            int max=left;
            if(left+1<size&&array[left+1]>array[left]){
                max=left+1;
            }
            if(array[index]>=array[max]){
                return;
            }
            swap(array,index,max);
            index=max;
        }
    }

//调整为小堆,向下调整
    public static void heapifyMin(int [] array,int size,int index){
        while(true){
            int left=2*index+1;
            if(left>=size){
                return;
            }
            int min=left;
            if(left+1<size&&array[left+1]<array[left]){
                min=left+1;
            }
            if(array[index]<=array[min]){
                return;
            }
            swap(array,index,min);
            index=min;
        }
    }

Built heap: piles turn any array

1) From [last non-leaf node, 0], downward adjustment

2) the last non-leaf node: parent index of the last node is: (size-2) / 2

3) a final index node: size-1

//建堆,构建为大堆
    //复杂度为O(n),粗略为0(n*log(n))
    public static void createHeap(int [] array,int size){
       for(int i=(size-2)/2; i>=0; i--){
           heapify(array,size,i);
        }
    }


    //建堆,构建为小堆
    public static void createHeapMin(int [] array,int size){
        for(int i=(size-2)/2;i>=0;i--){
            heapifyMin(array,size,i);
        }
    }

Upward adjustment piles:

Premise: In the entire stack, in addition to adjust the position of the remaining positions have been met, the nature of the heap

1) find the index using Equation parent node

2) max provided parent node,

3) If the nature of the stack to meet the direct return

4) If not, the two exchange array index value is index ,, make max

  //向上调整大堆,除了index和它的孩子外,其余位置已经满足堆的性质了
    public static void adjustUp(int [] array,int index) {
        while (true) {
            if(index<=0){
                return;
            }
            int parent = (index - 1) / 2;
            int max = parent;
            if (array[max] >= array[index]) {
                return;
            }
            swap(array, index, max);
            index = max;
        }
    }
    //向上调整小堆,前提:除了index和它的孩子外,其余位置已经满足堆的性质了
    public static void adjustUpMin(int [] array,int index){
        while(index >0){
            int parent=(index-1)/2;
            int min=parent;
            if(array[min]<=array[index]){
                return;
            }
            swap(array,index,min);
            index=min;
        }
    }

 

 

 

 

 

Published 62 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43669007/article/details/100541000