二叉树基本操作及层次遍历

 #include <iostream>
 #include <queue>
 #include<iomanip>
 using namespace std;
 template<class T>
 struct Node{
     Node(const T &x=0,Node<T> *a=NULL,Node<T> *b=NULL)
     {
         data = x;
         lchild = a;
         rchild = b;
     }
     Node<T> *lchild;
     Node<T> *rchild;
     T data;
 };
 int hcount=1,height=1,leaf=0;      //height是树的高度,leaf是叶子结点数
 template<class T>
 void deletetree(Node<T> * root);       //删除一棵二叉树
 template<class T>
 void calheight(Node<T> * root);        //求一棵树的高度
 template<class T>
 void calleaf(Node<T> * root);     //求一棵二叉树的叶子节点树
 template<class T>
 void copytree(Node<T>  &a,Node<T>  &b);     //复制一棵二叉树,将a复制给b
 template<class T>
 void exchange(Node<T> * root);     //交换二叉树的左右孩子
 template<class T>
 void bianli(Node<T> *root);    //遍历
 template<class T>
 void callqueue(queue<Node<T>*> &q);    //调用队列
  template<class T>
  void maketree(Node<T> &root,const T&x,Node<T> &lc,Node<T> &rc)    //建树
  {
        root.data=x;
        if(lc.data==-1)
            root.lchild==NULL;
        else
            root.lchild=&lc;
        if(rc.data==-1)
            root.rchild==NULL;
        else
            root.rchild=&rc;
  }
 int main()
 {
     Node<int> t[100],a(-1),b(-1),*root;
     maketree(t[7],4,a,b);
     maketree(t[6],5,a,b);
     maketree(t[5],6,a,b);
     maketree(t[4],7,a,b);
     maketree(t[2],2,t[7],t[6]);
     maketree(t[3],3,t[5],t[4]);
     maketree(t[1],1,t[2],t[3]);
     root=&t[1];
     bianli(root);
     calheight(root);
     cout<<"高度"<<height<<endl;
     calleaf(root);
     cout<<"叶子结点数"<<leaf<<endl;  //虚假ab孩子除以2消去
     copytree(t[1],t[0]);
     exchange(&t[0]);
     bianli(&t[0]);
     deletetree(&t[0]);
     bianli(&t[0]);
    return 0;
 }
 template<class T>
 void deletetree(Node<T> * root)    //删除一棵二叉树
 {
     if(!root->lchild&&!root->rchild)
     {
         root=NULL;
         delete root;
         return ;
     }
     if(root->lchild)
     {
         deletetree(root->lchild);
         root->lchild=NULL;
     }
     if(root->rchild)
     {
         deletetree(root->rchild);
          root->rchild=NULL;
     }
     root->data=-1;
 }
 template<class T>
 void calheight(Node<T> * root)     //求一棵树的高度
 {
     if(!root->lchild&&!root->rchild)
     {
         if(hcount>height)
             height=hcount;
         return ;
     }
     if(root->lchild)
     {
         hcount++;
         calheight(root->lchild);
         hcount--;
     }
     if(root->rchild)
     {
         hcount++;
         calheight(root->rchild);
         hcount--;
     }
}
 template<class T>
 void calleaf(Node<T> *root)    //求一棵二叉树的叶子节点树
 {
     if(!root->lchild&&!root->rchild)
     {
         leaf++;
         return ;
     }
     if(root->lchild)
         calleaf(root->lchild);
     if(root->rchild)
         calleaf(root->rchild);
 }
 template<class T>
 void copytree(Node<T>  &a,Node<T>  &b)     //复制一棵二叉树,将a复制给b
 {
     if(NULL==&b)
     {
        Node<T> *m=new Node<T>;
        b=*m;
     }
     b.data=a.data;
     b.lchild=a.lchild;
     b.rchild=a.rchild;
     if(!a.lchild&&!a.rchild)
         return;
     if(a.lchild)
     {
         copytree(*a.lchild,*b.lchild);
     }
     if(a.rchild)
     {
         copytree(*a.rchild,*b.rchild);
     }
 }
 template<class T>
 void exchange(Node<T> * root)
 {
     if(root)
     {
         Node<T> * temp = root->lchild;
         root->lchild=root->rchild;
         root->rchild=temp;
         exchange(root->lchild);
         exchange(root->rchild);
     }
 }
template<class T>
void bianli(Node<T> * root) //遍历树
{
    if(root->data==-1)
    {
        cout<<"nothing!"<<endl;
        return ;
    }
    queue<Node<T>*> q;
    q.push(root);   //push元素入队
    callqueue(q);   //队列递归
    cout<<endl;
}
template<class T>
void callqueue(queue<Node<T>*> &q)//队列递归
{
    if(!q.empty())
    {
        Node<T>* temp=q.front();             //front()返回队头元素back()返回队尾元素
        if(temp->lchild)
            q.push(temp->lchild);          //pop()队头元素出队
        if(temp->rchild)
            q.push(temp->rchild);
        cout<<setw(3)<<temp->data;
        q.pop();
        callqueue(q);
    }
}


存一下自己写过的代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值