zoukankan      html  css  js  c++  java
  • 哈夫曼树

    template<class Elem>
    class HuffNode
    {
    public:
    virtual int weight()=0;
    virtual bool isLeaf()=0;
    virtual HuffNode* left() const=0;
    virtual void setLeft(HuffNode*)=0;
    virtual HuffNode* right() const=0;
    virtual void setRight(HuffNode*)=0;
    };

    template<class Elem>
    class LeafNode:public HuffNode<Elem>
    {
    private:
    FreqPair<Elem>* it;
    public:
    LeafNode(const Elem& val,int freq)
    {
    it=new FreqPair<Elem>(val,freq);
    }
    int weight()
    {
    return it->weight();
    }
    FrePair<Elem>* val()
    {
    return it;
    }
    bool isLeaf()
    {
    return true;
    }
    virtual HuffNode* left() const
    {
    return NULL;
    }
    virtual void setLeft(HuffNode*){}
    virtual HuffNode* right() const
    {
    return NULL;
    }
    virtual void setRight(HuffNode*);
    };

    template<class Elem>
    class IntlNode:public HuffNode<Elem>
    {
    private:
    HuffNode<Elem>* lc;
    HuffNode<Elem>* rc;
    int wgt;
    public:
    IntlNode(HuffNode<Elem> *l,HuffNode<Elem>* r)
    {
    wgt=l->weight()+r->weight();lc=l;rc=r;
    }
    int weight()
    {
    return wgt;
    }
    bool isLeaf()
    {
    return false;
    }
    HuffNode<Elem> *left() const
    {
    return lc;
    }
    void setLeft(HuffNode<Elem> *b)
    {
    lc=(HuffNode*)b;
    }
    HuffNode<Elem> *right() const
    {
    return rc;
    }
    void setRight(HuffNode<Elem> *b)
    {
    rc=(HuffNode*)b;
    }
    };

    template<class Elem>
    class FreqPair
    {
    private:
    Elem it;
    int freq;
    public:
    FreqPair(const Elem& e,int f)
    {
    it=e;freq=f;
    }
    ~FreqPair()
    {}
    int weight()
    {
    return freq;
    }
    Elem& val()
    {
    return it;
    }
    };


    //HuffTree is a template of two parameters:the element
    //type being coded and a comparator for two such elements
    template<class Elem>

    class HuffTree
    {
    private:
    HuffNode<Elem>* theRoot;
    public:
    HuffTree(Elem& val,int freq)
    {
    theRoot=new LeafNode<Elem>(val,freq);
    }
    HuffTree(HuffTree<Elem>* l,HuffTree<Elem>* r)
    {
    theRoot=new IntlNode<Elem>(l->root(),r->root());
    }
    ~HuffTree(){}
    HuffNode<Elem>* root() { return theRoot;}
    int weight()
    {
    return theRoot->weight();
    }
    };

    template<class Elem>
    class HHCompare
    {
    public:
    static bool lt(HuffTree<Elem>* x,HuffTree<Elem> *y)
    {
    return x->weight()<y->weight();
    }
    static bool eq(HuffTree<Elem>* x,HuffTree<Elem> *y)
    {
    return x->weight()==y->weight();
    }
    static bool gt(HuffTree<Elem>* x,Huffman<Elem>* y)
    {
    return x->weight()>y->weight();
    }
    };

    //Build a Huffman tree from list f1
    template <class Elem>HuffTree<Elem>*

    buildHuff(SLList<HuffTree<Elem>*,HHCompare<Elem>>*f1)
    {
    HuffTree<Elem> *temp1,*temp2,*temp3;
    for(f1->setStart();f1->leftLength()+f1->rightLength()>1;f1->setStart())
    {
    f1->remove(temp1);
    f1->remove(temp2);
    temp3=new HuffTree<Elem>(temp1,temp2);
    f1->insert(temp3);
    delete temp1;
    delete temp2;
    }
    return temp3;
    }
    Live together,or Die alone!
  • 相关阅读:
    线性代数复习向量空间线性相关向量组的秩
    证明:将n(n为2的幂)个点的位反转环划分为长为j(2的幂)的连续片段,这些片段都是次序等价的(分布式算法)
    vim命令小合集
    poj2240floyd算法
    visual studio打开文件所在目录
    5个0,4个1组成的字符串中,出现01或10的次数为4的不同字符串个数
    max spacing kclustering(类似kruskal最小生成树)并查集Debuging
    算法导论16.35huffman树的表示
    最优二叉查找树optimalBSTC++实现2
    算法导论16.37
  • 原文地址:https://www.cnblogs.com/hzhida/p/2354759.html
Copyright © 2011-2022 走看看