最大堆的创建、插入和删除

最大堆所需的头文件“treeNode.h”

#ifndef TREENODE_H_INCLUDED
#define TREENODE_H_INCLUDED

class Node
{
public:
    char name;
    int data;
   // int degree;
   // int height;
   // int freq;
    Node* leftchild;
    Node* rightchild;
    Node();
    Node* GetLeftChild();
    Node* GetRightChild();
    void setLeftChild(Node* l);
    void setRightChild(Node* r);
   // void addDegree();
    int GetData();
    void setData(int d);
    bool isLeaf();
};

Node::Node()
{
    data=0;
   // degree=0;
   // height=0;
   // freq=0;
    leftchild=NULL;
    rightchild=NULL;
}

Node* Node::GetLeftChild()
{
    return leftchild;
}

Node* Node::GetRightChild()
{
    return rightchild;
}

void Node::setLeftChild(Node* l)
{
    leftchild=l;
}

void Node::setRightChild(Node* r)
{
    rightchild=r;
}

int Node::GetData()
{
    return data;
}

void Node::setData(int d)
{
    data=d;
}

bool Node::isLeaf()
{
    if(rightchild==NULL&&leftchild==NULL)
        return true;
    else
        return false;
}

/*void Node::addDegree()
{
    degree++;
}*/

#endif // TREENODE_H_INCLUDED


最大堆的创建、插入、删除

#include<iostream>
#include<queue>
#include"treeNode.h"
using namespace std;

class MaxHeap
{
private:
    int* heapArray;
    int CurrentSize;
    int MaxSize;
public:
    MaxHeap(int a[],int n);
    ~MaxHeap();
    void BuildHeap();//创建最大堆
    bool IsLeaf(int pos);
    int leftchild(int pos);
    int rightchild(int pos);
    int parent(int pos);
    bool Remove(int pos);//按给定位置删除元素
    void SiftDown(int left);//向上调整
    bool SiftUp(int position);
    bool Insert(int x);//插入元素
    void show();
    void RemoveMax();//删除最大元素
};

MaxHeap::MaxHeap(int a[],int n)
{
    heapArray=new int[2*n];
    for(int i=0;i<n;i++)
        heapArray[i]=a[i];
    CurrentSize=n;
    MaxSize=n;
}

MaxHeap::~MaxHeap()
{
    delete [] heapArray;
}

bool MaxHeap::IsLeaf(int pos)
{
    int j=pos*2+1;
    if(j<CurrentSize||j+1<CurrentSize)
        return true;
    else
        return false;
}

void MaxHeap::BuildHeap()
{
    for(int i=CurrentSize/2-1;i>=0;i--)
        SiftDown(i);
}

void MaxHeap::SiftDown(int left)
{
    int i=left;
    int j=2*i+1;
    int temp=heapArray[i];
    while(j<CurrentSize){
        if((j<CurrentSize-1)&&(heapArray[j]<heapArray[j+1]))
            j++;
        if(temp<heapArray[j]){
            heapArray[i]=heapArray[j];
            heapArray[j]=temp;
            i=j;
            j=j*2+1;
        }
        else
            break;
    }
   // heapArray[i]=temp;
}

bool MaxHeap::Remove(int pos)
{
    if(pos>=CurrentSize)
        return false;
    else {
        int temp=heapArray[pos];
        heapArray[pos]=heapArray[CurrentSize-1];
        heapArray[CurrentSize-1]=temp;
        CurrentSize--;
        if(CurrentSize>1)
            SiftDown(0);
    }
}

void MaxHeap::RemoveMax()
{
    if(CurrentSize>=1){
        Remove(0);
    }
    else return;
}


bool MaxHeap::Insert(int x)
{
    int i=0;
    while(i<CurrentSize&&heapArray[i]!=x){
        i++;
    }
    //cout<<"i="<<i<<endl;
    if(i==CurrentSize){
        CurrentSize++;
        heapArray[CurrentSize-1]=x;
        //cout<<heapArray[CurrentSize]<<endl;
        for(int i=CurrentSize/2-1;i>=0;i--)
            SiftDown(i);
    }
    else
        cout<<"插入元素重复!"<<endl;
}

bool MaxHeap::SiftUp(int position)
{
    int j;
    int temp=0;
    if(position>=CurrentSize)
        return false;
    else{
        while(position){
            if(position%2==0){
                int lson=j-1;
                int rson=j;
                int pos=(position-1)/2;
                if(lson>heapArray[pos]){
                    temp=heapArray[pos];
                    heapArray[pos]=lson;
                    heapArray[j-1]=temp;
                }
                if(rson>heapArray[pos]){
                    temp=heapArray[pos];
                    heapArray[pos]=lson;
                    heapArray[j]=temp;
                }
                SiftUp((j-1)/2);
            }
            else{
                int lson=j;
                int rson=j+1;
                int pos=(position-1)/2;
                if(lson>heapArray[pos]){
                    temp=heapArray[pos];
                    heapArray[pos]=lson;
                    heapArray[j]=temp;
                }
                if(rson>heapArray[pos]){
                    temp=heapArray[pos];
                    heapArray[pos]=lson;
                    heapArray[j+1]=temp;
                }
                SiftUp((j-1)/2);
            }
        }
    }
}

void MaxHeap::show()
{
    if(CurrentSize)
        for(int i=0;i<CurrentSize;i++)
            cout<<heapArray[i]<<" ";
    else
        cout<<"堆空!"<<endl;
}

int main()
{
    cout<<"请输入最大堆的长度:";
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    MaxHeap heap(a,n);
    heap.BuildHeap();
    cout<<"构建堆:";
    heap.show();
    cout<<endl<<endl;
    cout<<"输入插入的数字:";
    int in;
    cin>>in;
    heap.Insert(in);
    heap.show();
    cout<<endl<<endl;
    cout<<"输入要删除的节点位置(从0开始):";
    int de;
    cin>>de;
    if(heap.Remove(de))
        heap.show();
    else
        cout<<"删除失败!"<<endl;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值