树的基本操作

#include<iostream>
using namespace std;
template<typename t>
struct binode
{
    t data;
    binode<t>*lchild,*rchild;
};
template<typename t>
class bitree
{
private:
    binode<t>*creat(binode<t>*bt)
    {
        char ch;
        cin>>ch;
        if(ch=='#')
            bt=NULL;
        else{
            bt=new binode<t>;
            bt->data=ch;
            bt->lchild=creat(bt->lchild);
            bt->rchild=creat(bt->rchild);
        }
        return bt;
    }
    void release(binode<t>*bt)
    {
        if(bt==NULL)
            return;
        else
        {
            release(bt->lchild);
            release(bt->rchild);
            delete bt;
        }
    }
    void preorder(binode<t>*bt)
    {
        if(bt==NULL)
            return;
        else
        {
            cout<<bt->data;
            preorder(bt->lchild);
            preorder(bt->rchild);
        }
    }
    void inorder(binode<t>*bt)
    {
        if(bt==NULL)
            return;
        else{
            inorder(bt->lchild);
            cout<<bt->data;
            inorder(bt->rchild);
        }
    }
    void postorder(binode<t>*bt)
    {
        if(bt==NULL)
            return;
        else{
            inorder(bt->lchild);
            inorder(bt->rchild);
            cout<<bt->data;
        }
    }
    binode<t>*root;
public:
    bitree()
    {
        root=creat(root);
    }
    ~bitree()
    {
        release(root);
    }
    void preorder()
    {
        preorder(root);
    }
    void inorder()
    {
        inorder(root);
    }
    void postorder()
    {
        postorder(root);
    }

};
int main()
{
    bitree<char> f;
    cout<<"该二叉树的前序遍历的序列是:"<<endl;
    f.preorder();
    cout<<"\n该二叉树的中序遍历序列是:"<<endl;
    f.inorder();
    cout<<"\n该二叉树的后序遍历序列是:"<<endl;
    f.postorder();
    return 0;

}

运行结果:

123##4##5#6##
该二叉树的前序遍历的序列是:
123456
该二叉树的中序遍历序列是:
324156
该二叉树的后序遍历序列是:
324561
Process returned 0 (0x0)   execution time : 19.111 s
Press any key to continue.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值