二叉树 数据结构

 实验四  二叉树子系统

1.实验目的

(1)掌握二叉树的特点及其存储方式。

(2)掌握二叉树的创建和显示方法。

(3)复习二叉树遍历的概念,掌握二叉树遍历的基本方法。

2.实验内容

(1)按屏幕提示用前序方法建立一棵二叉树。

(2)编写各个函数:前序遍历、中序遍历、后序遍历、层次遍历、求二叉树的叶子结点数、求二叉树的总结点数、求二叉树的深度。

(3)设计一个选择式菜单,以菜单方式选择下列操作。

二叉树子系统

                *******************************************

                *   1——建二叉树                         *

             *   2——凹入显示                         *

                *   3——先序遍历                         *

                *   4——中序遍历                         *

*   5——后序遍历                         *

                *   6——层次遍历                         *

                *   7——求叶子数                         *

                *   8——求结点数                         *

                *   9——求树深度                         *

                *   0——返    回                         *

             *******************************************

请选择菜单号(0–9):

3.实验步骤

(1)按下图建立二叉树。

(2)输入并调试程序。

(3)验证程序运行结果。

 

附:程序清单如下:

  1 #include<stdio.h>
  2 #define TREEMAX 100
  3 typedef struct BT
  4 { char data;
  5   struct BT* lchild;
  6   struct BT*rchild;
  7 }BT;
  8 
  9 BT *CreateTree();
 10 void ShowTree(BT *T);
 11 void Preorder(BT *T);
 12 void Postorder(BT *T);
 13 void Levelorder(BT *T);
 14 void Inorder(BT *T);
 15 void Nodenum(BT *T);
 16 void Leafnum(BT *T);
 17 int TreeDepth(BT *T);
 18 int count=0;
 19 
 20 void main()
 21 {BT*T=NULL;
 22  char ch1,ch2,a;
 23  ch1='y';
 24  while(ch1=='y'||ch1=='Y')
 25     {printf("\n");
 26     printf("\n\t\t            二叉树子系统");
 27     printf("\n\t\t*****************************************");
 28     printf("\n\t\t*          1----建二叉树                *");
 29     printf("\n\t\t*          2----凹入显示                *");
 30     printf("\n\t\t*          3----先序遍历                *");
 31     printf("\n\t\t*          4----中序遍历                *");
 32     printf("\n\t\t*          5----后序遍历                *");
 33     printf("\n\t\t*          6----层次遍历                *");
 34     printf("\n\t\t*          7----求叶子数                *");
 35     printf("\n\t\t*          8----求结点数                *");
 36     printf("\n\t\t*          9----求树深度                *");
 37     printf("\n\t\t*          0----返回                    *");
 38     printf("\n\t\t*****************************************");
 39     printf("\n\t\t   选择菜单号(0---9):");
 40 
 41     scanf("%c",&ch2);
 42     getchar();
 43     printf("\n");
 44     switch(ch2)
 45         { case '1':
 46         printf("\n\t\t请按先序序列输入二叉树的结点: \n");
 47         printf("\n\t\t说明:输入结点('0'表示后继结点为空)后按回车。 \n");
 48         printf("\n\t\t请输入结点:");
 49         T=CreateTree();
 50         printf("\n\t\t 二叉树成功建立!\n");break;
 51         case '2':
 52             ShowTree(T);break;
 53         case '3':
 54             printf("\n\t\t 该二叉树的先序遍历数列为:");
 55             Preorder(T);break;
 56         case '4':
 57             printf("\n\t\t 该二叉树的中序遍历数列为:");
 58             Inorder(T);break;
 59         case '5':
 60             printf("\n\t\t 该二叉树的后序遍历数列为:");
 61             Postorder(T);break;
 62         case '6':
 63             printf("\n\t\t 该二叉树的层次遍历数列为:");
 64             Levelorder(T);break;
 65         case '7':
 66             count=0;Leafnum(T);
 67             printf("\n\t\t 该二叉树总共有%d个叶子。\n",count);break;
 68         case '8':
 69             count=0;Nodenum(T);
 70             printf("\n\t\t 该二叉树总共有%d个结点。\n",count);break;
 71         case '9':
 72             printf("\n\t\t 该树的高度是:%d",TreeDepth(T));break;
 73         case '0':
 74             ch1='n';break;
 75         default:
 76             printf("\n\t\t*** 请注意:输入有误!***");
 77         }
 78 
 79     if(ch2!='0')
 80     { printf("\n\n\t\t 按ENTER键继续,按任意键返回主菜单!\n");
 81      a=getchar();
 82      if(a!='\xA')
 83      {getchar();ch1='n';}
 84     }
 85  }
 86 }
 87 
 88 
 89 
 90 BT *CreateTree()
 91 { BT *t;
 92   char x;
 93   scanf("%c",&x);
 94   getchar();
 95   if(x=='0')
 96     t=NULL;
 97   else
 98     {t=(BT*)malloc(sizeof(BT));
 99     t->data=x;
100     printf("\n\t\t请输入%c结点的左子结点:",t->data);
101     t->lchild=CreateTree();
102     printf("\n\t\t请输入%c结点的右子结点:",t->data);
103     t->rchild=CreateTree();
104     }
105   return t;
106 }
107 
108 void Preorder(BT *T)
109 { if(T)
110     { printf("%3c",T->data);
111       Preorder(T->lchild);
112       Preorder(T->rchild);
113     }
114 }
115 
116 void Inorder(BT *T)
117 { if(T)
118     { 
119       Preorder(T->lchild);
120       printf("%3c",T->data);
121       Preorder(T->rchild);
122     }
123 }
124 
125 void Postorder(BT *T)
126 { if(T)
127     { 
128       Preorder(T->lchild);
129       Preorder(T->rchild);
130       printf("%3c",T->data);
131     }
132 }
133 
134 void Levelorder(BT *T)
135 { int i,j;
136   BT *q[100], *p;
137   p=T;
138   if(p!=NULL)
139     {i=1;q[i]=p;j=2;}
140      while(i!=j)
141          {p=q[i];printf("%3c",p->data);
142          if(p->lchild!=NULL)
143         {q[j]=p->lchild;j++;}
144         if(p->rchild!=NULL)
145         {q[j]=p->rchild;j++;}
146          i++;
147     }
148 }
149 
150 void Leafnum(BT *T)
151 {if(T)
152     {if(T->lchild==NULL&&T->rchild==NULL)
153     count++;
154     Leafnum(T->lchild);
155     Leafnum(T->rchild);
156     }
157 }
158 
159 void Nodenum(BT *T)
160 {if(T)
161     {count++;
162      Nodenum(T->lchild);
163       Nodenum(T->rchild);
164     }
165 }
166 int TreeDepth(BT *T)
167 {int ldep,rdep;
168  if(T==NULL)
169  return 0;
170 else
171  {ldep=TreeDepth(T->lchild);
172   rdep=TreeDepth(T->rchild);
173   if(ldep>rdep)
174     return ldep+1;
175     else 
176     return rdep+1;
177 }
178 }
179 void ShowTree(BT *T)
180 {BT *stack[TREEMAX],*p;
181  int level[TREEMAX][2],top,n,i,width=4;
182  if(T!=NULL)
183     {printf("\n\t\t 凹入表示法:\n\t\t");
184      top=1;
185      stack[top]=T;
186      level[top][0]=width;
187      while(top>0)
188         {p=stack[top];
189         n=level[top][0];
190         for(i=1;i<=n;i++)
191         printf(" ");
192         printf("%c",p->data);
193         for(i=n+1;i<30;i+=2)
194         printf("-");
195         printf("\n\t\t");
196         top--;
197         if(p->rchild!=NULL)
198             {top++;
199             stack[top]=p->rchild;
200             level[top][0]=n+width;
201             level[top][1]=2;
202             }
203         if(p->lchild!=NULL)
204             {top++;
205             stack[top]=p->lchild;
206             level[top][0]=n+width;
207             level[top][1]=1;
208             }
209         }
210     }
211 }

 

运行过程及结果:

1—-建二叉树 

《二叉树 数据结构》

 

 

2—-凹入显示

《二叉树 数据结构》

 

3—-先序遍历

 

《二叉树 数据结构》

 

4—-中序遍历

《二叉树 数据结构》

 

5—-后序遍历

 

《二叉树 数据结构》

 

6—-层次遍历

《二叉树 数据结构》

 

7—-求叶子数

 

《二叉树 数据结构》

 

8—-求结点数

《二叉树 数据结构》

 

9—-求树深度

 

《二叉树 数据结构》

 

0—-返回

《二叉树 数据结构》

    原文作者:竹立荷塘
    原文地址: https://www.cnblogs.com/daipianpian/p/4492860.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞