最小堆与huffman树

#include <iostream>
#include <vector>
template <class T>
class MinHeap {
private:
    T * heapArray;
    int currentSize;
    int MaxSize;
    void siftUp(int position){
        int par=parent(position);
        while (position!=0&&heapArray[position]<heapArray[par]) {
            swap(position, par);
            position=par;
            if((par=parent(position))==-1){
                return;
            }
        }
    }
    void swap(int x,int y){
        T tmp = heapArray[x];
        heapArray[x]=heapArray[y];
        heapArray[y]=tmp;
    }
    void BuildHeap(){
        for (int i=currentSize/2-1; i<currentSize; i++) {
            siftUp(i);
        }
    }
    void siftDown(int left){
        int i = left;
        int j = 2*i+1;
        T temp = heapArray[i];
        while (j<currentSize) {
            if ((j<currentSize-1)&&(heapArray[j]>heapArray[j+1])) {
                j++;
            }
            if (temp>heapArray[j]) {
                heapArray[i]=heapArray[j];
                i = j;
                j = 2*i +1;
            }else{
                break;
            }
        }
        heapArray[i]=temp;
    }
public:
    MinHeap(T * array, int sum, int Min):heapArray(array),currentSize(sum),MaxSize(Min){
        heapArray=new T(Min);
        for (int i=0; i<sum; i++) {
            heapArray[i]= array[i];
        }
        BuildHeap();
    }
    ~MinHeap(){
        delete heapArray;
    };
    long size(){
        return currentSize;
    }
    bool isLeaf(int pos) const{
        if (pos>MaxSize/2-1) {
            return true;
        }
        return false;
    }
    ///返回左孩子位置
    int leftChild(int pos) const{
        int x=2*pos+1;
        if (x>currentSize) {
            throw 1;
        }
        return x;
    }
    ///返回右孩子位置
    int rightChild(int pos) const{
        
        int x=2*pos+2;
        if (x>currentSize) {
            throw 1;
        }
        return x;
    }
    ///返回父节点位置
    int parent(int pos) const{
        if (pos==0) {
            return -1;
        }
        return (pos-1)/2;
    }
    ///删除给定下标的元素
    bool remove(int pos,T& node){
        if (pos>=currentSize||pos<0) {
            return false;
        }
        node = heapArray[pos];
        swap(pos, currentSize-1);
        currentSize-=1;
        siftDown(pos);
        return true;
    }
    ///插入元素newNode
    bool insert(const T& newNode){
        if(currentSize==MaxSize)
            return false;
        heapArray[currentSize]=newNode;
        siftUp(currentSize);
        currentSize+=1;
        return true;
    }
    T& removeMin(){
        swap(0, currentSize-1);
        T& t=heapArray[currentSize-1];
        currentSize-=1;
        siftDown(0);
        return t;
    }
    void print(){
        for (int i=0; i<currentSize; i++) {
            std::cout<<heapArray[i]<<"-";
        }
        std::cout<<std::endl;
    }
};

template <typename E>
class HuffNode {
protected:
public:
    virtual ~HuffNode(){}
    virtual int weight()=0;
    virtual bool isLeaf()=0;
    
};
template <typename E>
class LeafNode:public HuffNode<E> {
    E _value;
    int _weight;
public:
    LeafNode(const E& val,int wei):_value(val),_weight(wei){}
    int weight(){ return weight; }
    bool isLeaf(){ return true; }
};
template <typename E>
class IntlNode:public HuffNode<E> {
    HuffNode<E> * leftChild;
    HuffNode<E> * rightChild;
    int _weight;
    
public:
    IntlNode(HuffNode<E> * l,HuffNode<E> * r):leftChild(l),rightChild(r){
        _weight=leftChild->weight()+rightChild->weight();
    }
    int weight(){ return _weight;}
    bool isLeaf(){ return false;}
    HuffNode<E> * left(){ return leftChild;}
    void setLeft(HuffNode<E> * node){ leftChild=node;}
    HuffNode<E> * right(){ return rightChild;}
    void setRight(HuffNode<E> * node){ rightChild=node;}
};

template <typename E>
class HuffTree {
    HuffNode<E> * _root;
    
public:
    HuffTree(E &val,int freq){
        _root=new LeafNode<E>(val,freq);
    }
    HuffTree(HuffTree<E>* l,HuffTree<E>* r){
        _root=new IntlNode<E>(l->_root,r->_root);
    }
    HuffTree * root(){return _root;}
    int weight(){ return _root->weight();}
};
template <typename E>
HuffTree<E> * buildHuff( HuffTree<E> **TreeArray,int count) {
    MinHeap<HuffTree<E *>> * forest = new MinHeap<HuffTree<E *>>(TreeArray,count,count);
    HuffTree<char> * temp1,*temp2,*temp3=NULL;
    while (forest->size()>1) {
        temp1=forest->removeMin();
        temp2=forest->removeMin();
        temp3=new HuffTree<E>(temp1,temp2);
        forest->insert(temp3);
        delete temp1;
        delete temp2;
    }
    return temp3;
}
int main(int argc, const char * argv[]) {
    int a[10]={1,2,3,4,5,6,7,8,9,0};
    std::vector<HuffTree<int> *> treeArray;
    for (int i=0; i<10; i++) {
        new HuffTree<int>(a[0],1);
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zerlina98/article/details/79073422