Heap_sort(沙老师版本)

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
    int id;
    int pri;
    struct node *left;
    struct node *right;
    struct node *parent;
}heapDataType;  

typedef struct 
{
    bool s[20];
    int top;
}stackType;

class myHeap
{
private:
    heapDataType *heap;
    //True min False max
    bool mMinMax; 
    int maxHeapSize;
    int mheapSize;
    heapDataType defaultValue;
    stackType stack;
public:
    myHeap(bool minMax)
    {
        heap = NULL;
        mheapSize = 0;
        mMinMax = minMax;
        stack.top = -1;
    }
    ~myHeap()
    {
        delete heap;
    }
    void up(heapDataType *cur)
    {
        if(cur == NULL) return;
        if(cur == heap) return;
        heapDataType *change = cur; 
        if(mMinMax) // Min heap
        {
            if(cur->parent->pri > change->pri)
            {
                change = cur->parent;
            }
        }
        else // Max heap
        {
            if(cur->parent->pri < change->pri)
            {
                change = cur->parent;
            }
        }
        if(change!=cur)
        {
            heapDataType temp;
            temp.pri = change->pri;
            temp.id = change->id;
            change->pri = cur->pri;
            change->id = cur->id;
            cur->pri = temp.pri;
            cur->id = temp.id;
            up(change);
        }
    }
    void down(heapDataType *cur)
    {
        if(cur == NULL) return;
        heapDataType *left_p = cur->left;
        if(left_p == NULL) left_p=cur;
        heapDataType *right_p = cur->right;
        if(right_p == NULL) right_p=cur;
        heapDataType *change = cur; 
        if(mMinMax) // Min heap
        {
            if(change->pri > right_p->pri)
            {
                change = right_p;
                if(change->pri > left_p->pri) 
                {
                    change = left_p;
                }
            }
            else
            {
                if(change->pri > left_p->pri) 
                {
                    change = left_p;
                }
            }
        }
        else // Max heap
        {
            if(change->pri < right_p->pri)
            {
                change = right_p;
                if(change->pri < left_p->pri) 
                {
                    change = left_p;
                }
            }
            else
            {
                if(change->pri < left_p->pri) 
                {
                    change = left_p;
                }
            }
        }
        if(change!=cur)
        {
            heapDataType temp;
            temp.pri = change->pri;
            temp.id = change->id;
            change->pri = cur->pri;
            change->id = cur->id;
            cur->pri = temp.pri;
            cur->id = temp.id;
            down(change);
        }
    }
    void makeHeap()
    {

    }
    void push(heapDataType key)
    {
        mheapSize++;
        int path=mheapSize/2; 
        if(heap == NULL)
        {
            heap = new heapDataType;
            heap->parent=NULL;
            heap->left=NULL;
            heap->right=NULL;
            heap->id = key.id;
            heap->pri = key.pri;
        }
        else
        {
            heapDataType *p= heap;
            stack.top=-1;
            while(path>1)
            {
                if(path%2==0)
                {
                    stack.s[++(stack.top)]=true;
                }
                else
                {
                    stack.s[++(stack.top)]=false;
                }
                path/=2;
            }
            while(stack.top>=0)
            {
                if(stack.s[stack.top])
                {
                    p=p->left;
                }
                else
                {
                    p=p->right;
                }
                stack.top--;
            }
            heapDataType *node = new heapDataType;
            node->parent = p;
            node->id=key.id;
            node->pri=key.pri;
            node->left=NULL;
            node->right=NULL;
            if(p->left==NULL)
            {
                p->left = node;
            }
            else
            {
                p->right = node;
            }
            up(node);
        }
    }
    void pop()
    {
        if(heap == NULL) return;
        int path=mheapSize/2;
        heapDataType *p= heap;
        stack.top=-1;
        while(path>1)
        {
            if(path%2==0)
            {
                stack.s[++(stack.top)]=true;
            }
            else
            {
                stack.s[++(stack.top)]=false;
            }
            path/=2;
        }
        while(stack.top>=0)
        {
            if(stack.s[stack.top])
            {
                p=p->left;
            }
            else
            {
                p=p->right;
            }
            stack.top--;
        }

        heapDataType *node = NULL;

        if(p->right!=NULL)
        {
            node=p->right;
            p->right = NULL;
        }
        else if(p->left!=NULL)
        {
            node=p->left;
            p->left = NULL;
        }
        else
        {
            node=p;
        }
        heap->id = node->id;
        heap->pri = node->pri;
        mheapSize--;
        down(heap);
        //delete node;
    }
    heapDataType top()
    {
        return *heap;
    }
    bool empty()
    {
        return mheapSize == 0;
    }

};

int main (void)
{
    myHeap maxHeap = myHeap(false);
    myHeap minHeap = myHeap(true);
    heapDataType temp;
    int n;
    while(true) {
        scanf("%d",&n);
    
        if(n==0)
        {
            return 0;
        }
        else if(n==1)
        {
            scanf("%d %d", &temp.id, &temp.pri);
            maxHeap.push(temp);
            minHeap.push(temp);
        }
        else if(n==2)
        {
            if(maxHeap.empty()) {printf("0
");continue;}
            printf("%d
",maxHeap.top());
            maxHeap.pop();
        }
        else if(n==3)
        {
            if(minHeap.empty()) {printf("0
");continue;}
            printf("%d
",minHeap.top());
            minHeap.pop();
        }
    }
    return 0;
}
大多数想法要么平庸,要么更糟糕,这很大程度上因为绝妙的想法难得一见,而且他们还要在我们身边这个充斥了各种恶俗的所谓常识的环境中孕育生长。
原文地址:https://www.cnblogs.com/linux0537/p/7531191.html