数据结构-二叉树的基本操作

#include <iostream>

using namespace std;
struct binode
{
    char data;
    binode*lchild,*rchild;
};
class bitree
{
public:
    bitree(){root=creat(root);}
    ~bitree(){release(root);}
    void preorder(){preorder(root);}
    void inorder(){inorder(root);}
    void postorder(){postorder(root);}
    int num(){num(root);}
    int depth(){depth(root);}
    int yznum(){yznum(root);}
    void ex(){ex(root);}
private:
    binode*creat(binode *bt);
    void release(binode *bt);
    void preorder(binode *bt);
    void inorder(binode *bt);
    void postorder(binode *bt);
    int num(binode *bt);
    int depth(binode *bt);
    int yznum(binode *bt);
    void ex(binode*bt);
    binode *root;
};
void bitree::ex(binode *bt)
{

   if(bt==NULL)
   {
       return ;
   }
   else
{
        binode *temp=bt->lchild;
        bt->lchild=bt->rchild;
        bt->rchild=temp;
        ex(bt->lchild);
        ex(bt->rchild);
    }
}
int bitree::yznum(binode *bt)
{
    if(bt==NULL)
        return 0;
    else if(bt->lchild==NULL&&bt->rchild==NULL)
    {
        return 1;
    }
    else{
        return yznum(bt->lchild)+yznum(bt->rchild);
    }
}
int bitree::depth(binode*bt)
{
    if(bt==NULL)
        return 0;
    else{
        return max(depth(bt->lchild),depth(bt->rchild))+1;
    }

}
int bitree::num(binode *bt)
{
    if(bt==NULL)
        return 0;
    else{
        return num(bt->lchild)+num(bt->rchild)+1;
    }
}
binode *bitree::creat(binode *bt)
{
    char ch;
    cin>>ch;
    if(ch=='#')
    {
        bt=NULL;
    }
    else{
        bt=new binode;
        bt->data=ch;
        bt->lchild=creat(bt->lchild);
        bt->rchild=creat(bt->rchild);
    }
    return bt;
}
void bitree::release(binode *bt)
{
    if(bt==NULL){return ;}
    else
    {
        release(bt->lchild);
        release(bt->rchild);
        delete bt;
    }
}
void bitree::preorder(binode *bt)
{
    if(bt==NULL)
    {
        return ;
    }
    else{
        cout<<bt->data;
        preorder(bt->lchild);
        preorder(bt->rchild);
    }
}
void bitree::inorder(binode *bt)
{
    if(bt==NULL)
    {
        return ;
    }
    else{

        preorder(bt->lchild);
        cout<<bt->data;
        preorder(bt->rchild);
    }
}
void bitree::postorder(binode *bt)
{
    if(bt==NULL)
    {
        return ;
    }
    else{

        preorder(bt->lchild);
        preorder(bt->rchild);
        cout<<bt->data;
    }

}
int main()
{  ios::sync_with_stdio(0);
    bitree a;
    /*a.preorder();
    cout<<endl;
    a.inorder();
    cout<<endl;
    a.postorder();
    cout<<endl;*/
    cout<<a.num();
    cout<<endl;
    cout<<a.yznum();
    cout<<endl;
    cout<<a.depth();
    cout<<endl;
    a.ex();
    a.preorder();
    cout<<endl;
    char cc;
    while(cin>>cc)
{
    if(cc=='Y')
    {
        bitree b;
    cout<<b.num();
    cout<<endl;
    cout<<b.yznum();
    cout<<endl;
    cout<<b.depth();
    cout<<endl;
   b.ex();
    b.preorder();
    cout<<endl;
    }
    else{
        break;
    }
}
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉梦昂志️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值