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!
  • 相关阅读:
    ASP.NET使用Coolite.Ext.Web.dll,显示ext"未定义"的解决方法
    浏览器引擎模式与DOCTYPE
    MVC 参数如何自动绑定数组对象
    IIS7.5 配置 PHP
    TFSDeleteProject:删除团队项目
    自动生成存储过程一
    如何更新ntext字段信息
    清除所有默认样式的CSS代码
    自定义动作过滤器属性
    IE9点击别的网页弹出空白页
  • 原文地址:https://www.cnblogs.com/hzhida/p/2354759.html
Copyright © 2011-2022 走看看