递归实现二叉树的基本操作

  1 #include<iostream>
  2 #include<cstdlib>
  3 using namespace std;
  4 #define OK 1
  5 #define OVERFLOW -2
  6 typedef int status;
  7 typedef struct BiTNode{  //创建链表
  8     char data;   //节点的值
  9     struct BiTNode *lchild,*rchild;  //左右孩子节点
 10 }BiTNode,*BiTree;
 11 
 12 status CreateBiTree(BiTree &T){            //1. 以先序方式构造二叉树
 13    char ch;
 14    cin>>ch;
 15    if (ch=='#') T=NULL;
 16    else{
 17         T=new BiTNode;
 18         if(!T) exit(OVERFLOW);  //表示创建节点失败
 19         T->data = ch;
 20         CreateBiTree(T->lchild);  //递归建立左子树
 21         CreateBiTree(T->rchild);  //递归建立右子树
 22    }
 23    return OK;
 24 }//CreateBiTree
 25 
 26 void Preorder(BiTree T){ // 2. 先序遍历打印二叉树各节点的值
 27    if (T) {
 28       cout<<T->data;       // 访问根结点
 29       Preorder(T->lchild);        //先序遍历左子树
 30       Preorder(T->rchild);        //先序遍历右子树
 31    }// if
 32 } // Preorder
 33 
 34 void Printnode(BiTree T){  //3. 先序遍历打印二叉树各节点的度
 35     if(T){
 36         if(!T->lchild && !T->rchild)
 37             cout<<'0';
 38         else if(T->lchild && T->rchild)
 39             cout<<'2';
 40         else cout<<'1';
 41         Printnode(T->lchild);
 42         Printnode(T->rchild);
 43     }
 44 }
 45 
 46 void Midorder(BiTree T){  //4. 中序遍历打印二叉树各节点的值
 47     if(T){
 48         Midorder(T->lchild);
 49         cout<<T->data;
 50         Midorder(T->rchild);
 51     }
 52 }
 53 
 54 void Lastorder(BiTree T){  //5. 后序遍历打印二叉树各节点的值
 55     if(T){
 56         Lastorder(T->lchild);  //遍历左子树
 57         Lastorder(T->rchild);  //遍历右子树
 58         cout<<T->data;
 59     }
 60 }
 61 
 62 int Nodes(BiTree T){      //6. 计算二叉树所有结点数
 63    if (T==NULL) return 0;
 64    else  return Nodes(T->lchild)+Nodes(T->rchild)+1;
 65 }// Nodes
 66 
 67 int Countlastnode(BiTree T){ // 7. 计算二叉树的叶节点数
 68     if(T==NULL) return 0;
 69     if(!T->lchild && !T->rchild)return 1;
 70     return Countlastnode(T->lchild)+Countlastnode(T->rchild);
 71 }
 72 
 73 int Depth(BiTree T){  //8. 计算二叉树的深度
 74     int m,n;
 75     if(T==NULL)return 0;
 76     else{
 77         m=Depth(T->lchild);  //递归得到左子树的深度
 78         n=Depth(T->rchild);  //递归得到右子树的深度
 79         if(m>n)return m+1;
 80         else return n+1;
 81     }
 82 }
 83 void Destorytree(BiTree &T){  //9. 销毁二叉树
 84     if(T){
 85         Destorytree(T->lchild); //递归找到叶节点,从叶节点开始往前删除
 86         Destorytree(T->rchild);
 87         delete T;
 88         T = NULL;
 89     }
 90 }
 91 
 92 int main()
 93 {
 94     BiTree T;
 95     cout<<"1、请输入形如\"ABC##D###\"这样的字符串,\'#\'表示该节点为空,先序建立一棵二叉树:";
 96     CreateBiTree(T);
 97     cout<<"2、先序递归遍历二叉树,打印各节点的值:";Preorder(T);cout<<endl;
 98     cout<<"3、先序递归遍历二叉树,打印各节点的度:";Printnode(T);cout<<endl;
 99     cout<<"4、中序递归遍历二叉树,打印各节点的值:";Midorder(T);cout<<endl;
100     cout<<"5、后序递归遍历二叉树,打印各节点的值:";Lastorder(T);cout<<endl;
101     cout<<"6、该二叉树的所有节点数为:"<<Nodes(T)<<endl;
102     cout<<"7、该二叉树的叶子节点数为:"<<Countlastnode(T)<<endl;
103     cout<<"8、该二叉树的深度为:"<<Depth(T)<<endl;
104     Destorytree(T);cout<<"9、已经销毁了二叉树"<<endl;
105 
106     return 0;
107 }

 

posted @ 2018-04-26 15:07  霜雪千年  阅读(934)  评论(0编辑  收藏  举报