二叉树02

为了加强记忆我又写了一遍二叉树的基本代码。比上一次多了一点点功能。

  1 #include<stdio.h>
  2 #include<iostream>
  3 using namespace std;
  4 struct Bitree{//二叉树的结构 
  5     int val;
  6     Bitree *lchild,*rchild;
  7 };
  8 void creat(Bitree * &T)//创建树 
  9 {
 10     int ch;
 11     scanf("%d",&ch);
 12     if(ch==0)
 13     T=NULL;
 14     else
 15     {
 16         T=new Bitree;
 17         T->val=ch;
 18         creat(T->lchild);
 19         creat(T->rchild);
 20     }
 21 }
 22 void qshow(Bitree *T)//前序遍历一棵树 
 23 {
 24     if(T!=NULL)
 25     {
 26         printf("%d\t",T->val);
 27         qshow(T->lchild);
 28         qshow(T->rchild);
 29     }
 30 }
 31 void zshow(Bitree *T)//中序遍历一棵树 
 32 {
 33     if(T!=NULL)
 34     {
 35         zshow(T->lchild);
 36         printf("%d\t",T->val);
 37         zshow(T->rchild);
 38     }
 39 }
 40 void hshow(Bitree *T)//后序遍历一棵树 
 41 {
 42     if(T!=NULL)
 43     {
 44         hshow(T->lchild);
 45         hshow(T->rchild);
 46         printf("%d\t",T->val);
 47     }
 48 }
 49 void copy(Bitree *T,Bitree * &T1)//复制一棵树 
 50 {
 51     if(T==NULL)
 52     T1=NULL;
 53     else
 54     {
 55         T1=new Bitree;
 56         T1->val=T->val;
 57         copy(T->lchild,T1->lchild);
 58         copy(T->rchild,T1->rchild);
 59     }
 60 }
 61 void delete_(Bitree * &T,int e)//删除一个值为e的结点及其所有孩子 
 62 {
 63     if(T==NULL)
 64     return;
 65     else
 66     {    
 67         if(T->val==e)
 68         T=NULL;
 69         else
 70         {
 71             delete_(T->lchild,e);
 72             delete_(T->rchild,e);
 73         }
 74      }
 75 } 
 76 int countyz(Bitree *T)//统计叶子结点数目 
 77 {
 78     int count1,count2;
 79     if(T==NULL)
 80     return 0;
 81     if(T->lchild==NULL&&T->rchild==NULL)
 82     return 1;
 83     else
 84     {
 85         count1=countyz(T->lchild);
 86         count2=countyz(T->rchild);
 87     } 
 88     return count1+count2;
 89 } 
 90 int maxdeep(Bitree *T)//树的最大深度 
 91 {
 92     if(T==NULL)
 93     return 0;
 94     else
 95     {
 96         int rlength,llength;
 97         rlength=maxdeep(T->rchild)+1;
 98         llength=maxdeep(T->lchild)+1;
 99         return llength>rlength?llength:rlength;    
100     }    
101 } 
102 int  mindeep(Bitree *T)//二叉树的最小深度 
103 {
104     if(T==NULL)
105     return 0;
106     if(T->lchild==NULL&&T->rchild==NULL)
107     return 1;
108     else if(T->lchild!=NULL)
109     return mindeep(T->lchild)+1;
110     else if(T->rchild!=NULL)
111     return mindeep(T->rchild)+1;
112     else
113     {
114         int rl=mindeep(T->rchild)+1;
115         int ll=mindeep(T->lchild)+1;
116         return rl>ll?ll:rl;
117     }
118 }
119 /*int main()
120 {
121     Bitree *T,*T1;
122     creat(T);
123     qshow(T);
124     printf("\n");
125     copy(T,T1);
126     qshow(T1);
127     printf("\n");
128     delete_(T,3);
129     qshow(T);
130     printf("\n");
131     printf("%d\n",countyz(T1));
132     printf("%d\n",maxdeep(T1));
133     printf("%d\n",mindeep(T1));
134     return 0;
135 } */

小白在成长

posted @ 2017-05-11 11:32  小花同学  阅读(128)  评论(0编辑  收藏  举报