Heap_sort(沙老师版本)

Posted 南瓜小屋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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\n");continue;}
            printf("%d\n",maxHeap.top());
            maxHeap.pop();
        }
        else if(n==3)
        {
            if(minHeap.empty()) {printf("0\n");continue;}
            printf("%d\n",minHeap.top());
            minHeap.pop();
        }
    }
    return 0;
}

 

以上是关于Heap_sort(沙老师版本)的主要内容,如果未能解决你的问题,请参考以下文章

堆排序Heap_Sort

c_cpp Heap_Sort

stl--heap_sort

heap_sort

空间坐标转换: 布尔沙七参数(C++版本)

iOS开发沙盒管理工具swift版