二叉树和哈夫曼树

二叉树:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <iomanip>
    using namespace std;
template<class T>
struct BTNode
{
    T element;
    BTNode<T> *lChild,*rChild,*parent;
    char value;
    int v;
    vector<int> code;
    BTNode() {lChild=rChild=NULL;}
    BTNode(const T &x,const char &v)
    {
        element=x;
        value=v;
        lChild=rChild=NULL;
        parent=NULL;
    }
    BTNode(const T &x,BTNode<T> *l,BTNode<T> *r)
    {
        element=x;
        lChild=l;
        rChild=r;
        parent=NULL;
        value=NULL;
    }
    BTNode(const T &x,const char &v,BTNode<T> *l,BTNode<T> *r)
    {
        element=x;
        lChild=l;
        rChild=r;
        parent=NULL;
        value=v;
    }
};
template<class T>
class BinaryTree
{
private:
    void PreOrder(void (*Visit)(T& x),BTNode<T>* t);
    void InOrder(void (*Visit)(T &x),BTNode<T>* t);
    void PostOrder(void (*Visit)(T& x),BTNode<T>* t);
    int Size(BTNode<T> *t);
public:
    BTNode<T> *root;
    BinaryTree() {root=NULL;}
    ~BinaryTree(){}
    bool IsEmpty() const;
    void Clear(BTNode<T> *t);
    bool Root(T &x) const;
    void MakeTree(const T& x,const char &value,BinaryTree<T> &left,BinaryTree<T> &right);
    void BreakTree(T& x,BinaryTree<T> &left,BinaryTree<T> &right);
    void PreOrder(void (*Visit)(T& x));
    void InOrder(void (*Visit)(T &x));
    void PostOrder(void (*Visit)(T& x));
    int Size();
};
template<class T>
bool BinaryTree<T>::IsEmpty() const
{
    return root==NULL;
}
template<class T>
void BinaryTree<T>::Clear(BTNode<T> *t)
{
    if(!t)
        return;
    else
    {
        Clear(t->lChild);
        Clear(t->rChild);
        delete t;
    }
}
template<class T>
bool BinaryTree<T>::Root(T &x) const
{
    if(root)
    {
        x=root->element;
        return true;
    }
    else return false;
}
template<class T>
void BinaryTree<T>::MakeTree(const T& x,const char &value,BinaryTree<T> &left,BinaryTree<T> &right)
{
    if(root||&left==&right)
        return;
    root=new BTNode<T>(x,value,left.root,right.root);
    if(left.root!=right.root)
    {
        left.root->parent=root;
        right.root->parent=root;
        left.root->v=0;
        right.root->v=1;
    }
    left.root=right.root=NULL;
}
template<class T>
void BinaryTree<T>::BreakTree(T &x,BinaryTree<T> &left,BinaryTree<T> &right)
{
    if(!root||&left==&right||left.root||right.root)
        return;
    x=root->element;
    left.root=root->lChild;
    right.root=root->rChild;
    delete root;
    root=NULL;
}
template<class T>
void Visit(T &x)
{
    cout<<x<<" ";
}
template<class T>
void BinaryTree<T>::PreOrder(void (*Visit)(T& x))
{
    PreOrder(Visit,root);
}
template<class T>
void BinaryTree<T>::PreOrder(void (*Visit)(T& x),BTNode<T>* t)
{
    if(t)
    {
        Visit(t->element);
        PreOrder(Visit,t->lChild);
        PreOrder(Visit,t->rChild);
    }
}
template<class T>
void BinaryTree<T>::InOrder(void (*Visit)(T& x))
{
    InOrder(Visit,root);
}
template<class T>
void BinaryTree<T>::InOrder(void (*Visit)(T& x),BTNode<T>* t)
{
    if(t)
    {
        InOrder(Visit,t->lChild);
        Visit(t->element);
        InOrder(Visit,t->rChild);
    }
}
template<class T>
void BinaryTree<T>::PostOrder(void (*Visit)(T& x))
{
    PostOrder(Visit,root);
}
template<class T>
void BinaryTree<T>::PostOrder(void (*Visit)(T& x),BTNode<T>* t)
{
    if(t)
    {
        PostOrder(Visit,t->lChild);
        PostOrder(Visit,t->rChild);
        Visit(t->element);
    }
}
template<class T>
int BinaryTree<T>::Size()
{
    Size(root);
}
template<class T>
int BinaryTree<T>::Size(BTNode<T> *t)
{
    if(!t)
        return 0;
    else
        return Size(t->lChild)+Size(t->rChild)+1;
}
优先权队列:
#include "BinaryTree.h"
template<class T>
class PriQueue
{
private:
    T *q;
    int n,maxSize;
    void AdjustDown(int r,int j);
    void AdjustUp(int j);
public:
    PriQueue(int mSize=20);
    ~PriQueue() {delete []q;}
    bool IsEmpty() {return n==0;}
    bool IsFull() {return n==maxSize;}
    void Append(const T &x);
    void Serve(T &x);
    void show();

};
template<class T>
PriQueue<T>::PriQueue(int mSize)
{
    maxSize=mSize;
    n=0;
    q=new T[maxSize];
}
template<class T>
void PriQueue<T>::AdjustDown(int r,int j)
{
    int child=2*r+1;
    T temp=q[r];
    while(child<=j)
    {
        if((child<j)&&(q[child]>q[child+1]))
            child++;
        if(temp<=q[child])
            break;
        q[(child-1)/2]=q[child];
        child=2*child+1;
    }
    q[(child-1)/2]=temp;
}
template<class T>
void PriQueue<T>::AdjustUp(int j)
{
    int i=j;
    T temp=q[i];
    while(i>0&&temp<q[(i-1)/2])
    {
        q[i]=q[(i-1)/2];
        i=(i-1)/2;
    }
    q[i]=temp;
}
template<class T>
void PriQueue<T>::Append(const T &x)
{
    if(IsFull())
    {
        cout<<"OverFlow";
        return;
    }
    q[n++]=x;
    AdjustUp(n-1);
}
template<class T>
void PriQueue<T>::Serve(T &x)
{
    if(IsEmpty())
    {
        cout<<"UnderFlow";
        return;
    }
    x=q[0];
    q[0]=q[--n];
    AdjustDown(0,n-1);
}
template<class T>
void PriQueue<T>::show()
{
    for(int i=0;i<n;i++)
        cout<<q[i]<<" ";
    cout<<endl;
}

哈夫曼树:

#include "PriQueue.h"
template<class T>
class HfmTree:public BinaryTree<T>
{
public:
    operator T()const{return weight;}
    T getW()const {return weight;}
    void putW(const T &x){weight=x;}
    void SetNull() {this->root=NULL;}
    void Create_Code();
    void Get_Code();
    BTNode<T>* FindV(char value);
    void CodeToArticle();
    void GetParents();
private:
    T weight;
    void Create_Code(BTNode<T>*t);
    void Get_Code(BTNode<T>* t);
    BTNode<T>* FindV(char value,BTNode<T> *t);
    void CodeToArticle(BTNode<T> *p);
    void GetParents(BTNode<T> * t);
};
template<class T>
BTNode<T>* HfmTree<T>::FindV(char value)
{
    return FindV(value,this->root);
}
template<class T>
BTNode<T>* HfmTree<T>::FindV(char value,BTNode<T> *t)
{
    if(!t)
        return NULL;
    if(t->value==value) return t;
    BTNode<T>* p;
    if(p=FindV(value,t->lChild))
        return p;
    if(p=FindV(value,t->rChild))
        return p;
    return NULL;
}
template<class T>
void HfmTree<T>::GetParents()
{
    if(this->root)
        GetParents(this->root);
}
template<class T>
void HfmTree<T>::GetParents(BTNode<T> * t)
{
    if(!t) return;
    if(t->lChild!=NULL)
    {
        t->lChild->parent=t;
    }
    if(t->rChild!=NULL)
    {
        t->rChild->parent=t;
    }
    GetParents(t->lChild);
    GetParents(t->rChild);
}

template<class T>
void HfmTree<T>::Create_Code()
{
    Create_Code(this->root);
}
template<class T>
void HfmTree<T>::Create_Code(BTNode<T>* t)
{
    if(t==NULL) return ;
    if(t->parent)
    {
        for(int i=0;i<t->parent->code.size();i++)
        {
            t->code.push_back(t->parent->code[i]);
        }
        t->code.push_back(t->v);
    }
    Create_Code(t->lChild);
    Create_Code(t->rChild);
}

template<class T>
void HfmTree<T>::Get_Code()
{
    cout<<"CODE:"<<endl;
    Get_Code(this->root);
}
template<class T>
void HfmTree<T>::Get_Code(BTNode<T>* t)
{
    if(t==NULL) return;
    if((t->lChild==NULL)&&(t->rChild==NULL))
    {
        cout<<t->value<<":";
        for(int i=0;i<t->code.size();i++)
        {
            cout<<t->code[i];
        }
        cout<<endl;
    }
    Get_Code(t->lChild);
    Get_Code(t->rChild);
}

HfmTree<int> Hfm;
int num;
int *w;
char *s;

void Menu()
{
    cout<<"--------------------Hfm Coding System-----------------"<<endl;
    cout<<"                 B------Build up tree"<<endl;
    cout<<"                 T------Trace the tree"<<endl;
    cout<<"                 C------Encode"<<endl;
    cout<<"                 D------Translate"<<endl;
    cout<<"                 Q------Exit"<<endl;
    cout<<"                 C------Delete"<<endl<<endl;
    cout<<"\ninput your choice :"<<endl;
}

template<class T>
HfmTree<T> CreateHfmTree(T *w,char *s,int n)               //构造哈夫曼树
{
    int i;
    PriQueue<HfmTree<T> > pq(n);
    HfmTree<T> x, y, z, zero;
    for(i = 0; i < n; i++)
    {
        z.MakeTree(w[i],s[i],x,y);
        z.putW(w[i]);
        pq.Append(z);
        z.SetNull();
    }
    for(i = 1; i < n; i++)
    {
        pq.Serve(x);
        pq.Serve(y);
        z.MakeTree(x.getW()+y.getW(),'0',x,y);
        z.putW(x.getW() + y.getW());
        pq.Append(z);
        z.SetNull();
    }
    pq.Serve(z);
    return z;
}
void BuildTree()
{
    char c;
    int i=0;
    cout<<"Please input the number of the code:";
    cin>>num;

    w=new int[num+1];
    s=new char[num+1];
    cout<<"Please input the codes:"<<endl;
    while(i<num)
    {
        cin>>c;
        s[i++]=c;
    }
    cout<<"Please input the weight:";
    for(i=0;i<num;i++)
        cin>>w[i];
    Hfm=CreateHfmTree(w,s,num);
    Hfm.GetParents();
    Hfm.Create_Code();
    cout<<"Finished!"<<endl;
}
void clear()
{
    if(s)
    {
        delete []s;
        s=NULL;
    }
    if(w)
    {
        delete []w;
        w=NULL;
    }
}

void ArticleToCode()
{
    cout<<"Please input the string you want to code:"<<endl;
    getchar();
    string str;
    getline(cin,str);
    for(int i=0;i<str.size();i++)
    {
        BTNode<int> *p=Hfm.FindV(str[i]);
        if(p==NULL)
        {
            cout<<"Cann't find "<<str[i]<<endl;
            continue;
        }
        for(int j=0;j<p->code.size();j++)
        {
            printf("%d",p->code[j]);
            char cc=p->code[j]+'0';
        }
    }
    cout<<endl;
    cout<<"Encoding complete!"<<endl<<endl;
}
template<class T>
void HfmTree<T>::CodeToArticle()
{
    CodeToArticle(this->root);
}
template<class T>
void HfmTree<T>::CodeToArticle(BTNode<T>* t)
{
    string str;
    cout<<"Please input strings:"<<endl;
    getchar();
    cin>>str;
    int i=0;
    while(i<str.size())
    {
        BTNode<int> *p=t;
        while(p->lChild!=NULL||p->rChild!=NULL)
        {
            if(str[i]=='0')
            {
                p=p->lChild;
            }
            else
            {
                p=p->rChild;
            }
            i++;
        }
        printf("%c ",p->value);
    }
    cout<<endl<<endl;
}

主函数
#include <iostream>
#include "HfmTree.h"
    using namespace std;
//int main()
//{
//    char aa;
//    BinaryTree<char> a,b,x,y,z;
//    y.MakeTree('E','e',a,b);
//    z.MakeTree('F','f',a,b);
//    x.MakeTree('C','c',y,z);
//    y.MakeTree('D','d',a,b);
//    z.MakeTree('B','b',y,x);
//    z.BreakTree(aa,''y,x);
//    z.PreOrder(Visit);
//    cout<<endl;
//    z.InOrder(Visit);
//    cout<<endl;
//    z.PostOrder(Visit);
//    cout<<endl;
//    cout<<z.Size();
//    cout<<endl;
//
//    PriQueue<int> q;
//    q.Append(2);
//    q.Append(3);
//    q.Append(1);
//
//    q.show();
//
//}
int main()
{
   // freopen("in.txt","r",stdin);
    char ch;
    Menu();
    cin >> ch;
    while(ch != 'Q'&&ch!='q')
    {
        switch(ch)
        {
        case 'B':
        case 'b':
            BuildTree();
            break;
        case 'T':
        case 't':
            Hfm.Get_Code();
            break;
        case 'C':
        case 'c':
            ArticleToCode();
            break;
        case 'D':
        case 'd':
            Hfm.CodeToArticle();
            break;
        case 'Q':
        case 'q':
            return 0;
        }
        system("PAUSE");
        system("CLS");
        Menu();
        cin >> ch;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值