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!
  • 相关阅读:
    WHU-1551-Pairs(莫队算法+分块实现)
    JS日历控件 灵活设置: 精确的时分秒.
    java集群优化——多线程下的单例模式
    课程设计——银行系统
    互联网金融,巨头天下还是创业者天堂?
    Android 使用图片异步载入框架Universal Image Loader的问题
    程序C++ to C#交互
    浅谈asp.net通过本机cookie仿百度(google)实现搜索input框自己主动弹出搜索提示
    全栈JavaScript之路(十六)HTML5 HTMLDocument 类型的变化
    推荐安卓开发神器(里面有各种UI特效和实例)
  • 原文地址:https://www.cnblogs.com/hzhida/p/2354759.html
Copyright © 2011-2022 走看看