zoukankan      html  css  js  c++  java
  • Heap_delete(A,i)操作将结点i中的想从堆A中删去。对含n个元素的最大堆,请给出时间为O(lgn)的HEAPDELETE的实现。

    代码:

    #include "iostream"
    using namespace std;
    int heap_size;
    
    int left(int i){
        return 2*i;
    }
    
    int right(int i){
        return 2*i+1;
    }
    
    void max_Heapify(int A[],int i){
        int l,r,largest;
        l=left(i);
        r=right(i);
        int temp;
        if(l<=heap_size&&A[i]<A[l])
            largest=l;
        else
            largest=i;
        if(r<=heap_size&&A[largest]<A[r])
            largest=r;
        if(largest!=i){
            temp=A[i];
            A[i]=A[largest];
            A[largest]=temp;
            max_Heapify(A,largest);
        }
    
    }
    
    void build_MaxHeap(int A[]){
        heap_size=5;
        for(int i=heap_size/2;i>=1;i--){
            max_Heapify(A,i);
        }
    }
    
    void heap_Delete(int A[],int i){
        A[i]=A[heap_size];
        heap_size--;
        max_Heapify(A,i);
    }
    
    void main(){
        int A[20]={-1,12,2,43,23,3};
        cout<<A[2]<<endl;
        build_MaxHeap(A);
        cout<<A[2]<<endl;
        heap_Delete(A,2);
        cout<<A[2]<<endl;
        getchar();
        getchar();
    }

      

    贯彻自己的思想
  • 相关阅读:
    THUWC2020 游记
    USACO14MAR The Lazy Cow(Gold)
    luogu P3768 简单的数学题
    2017/9/22模拟赛
    2017/9/20模拟赛
    2017/9/15模拟赛
    刷屏代码·稳 from林凯
    2017/9/13模拟赛
    【9018:1368】八数码
    2017/9/10模拟赛
  • 原文地址:https://www.cnblogs.com/593213556wuyubao/p/2822467.html
Copyright © 2011-2022 走看看