哈夫曼树

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;
}
posted @ 2012-02-16 19:03  苍术厚朴  阅读(304)  评论(0编辑  收藏  举报