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!
  • 相关阅读:
    dd——留言板再加验证码功能
    怎样去除织梦版权信息中的Power by DedeCms
    数据结构和算法的选择
    数据结构和算法9——哈希表
    数据结构与算法8——二叉树
    数据结构与算法7——高级排序
    数据结构与算法6——递归
    数据结构和算法5——链表
    数据结构与算法4——栈和队列
    数据结构与算法3——简单排序(冒泡、选择、插入排序)
  • 原文地址:https://www.cnblogs.com/hzhida/p/2354759.html
Copyright © 2011-2022 走看看