码迷,mamicode.com
首页 > 其他好文 > 详细

课设4---二叉树的操作

时间:2018-07-02 20:14:55      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:菜单   info   typedef   div   初始化   init   tno   ued   http   

二叉树

建立二叉树,先序、中序、后序、层次的遍历此二叉树,并求叶子结点个数、树的高度( 用递归或非递归的方法都可以)。

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include<conio.h>
  4 #define ERROR 0
  5 #define OK 1
  6 typedef char TElemType;
  7 typedef int Status;
  8 
  9 //二叉树的二叉链表存储表示
 10 typedef struct BiTNode
 11  {
 12   TElemType data;
 13   struct BiTNode *lchild, *rchild; 
 14 }BiTNode, *BiTree;
 15 
 16 //定义链式队列结点
 17 typedef struct QNode
 18 {
 19  BiTree Queuedata;
 20  struct QNode * next;
 21 }QNode,* QueuePtr;
 22 //定义链式队列
 23 typedef struct
 24 {
 25  QueuePtr front; 
 26  QueuePtr rear;
 27 }LinkQueue;
 28 
 29 //按照先序次序构造二叉树
 30 Status CreatBiTree(BiTree &T)
 31 {char ch;
 32 scanf("%c",&ch);getchar();
 33 if(ch==#) T=NULL;
 34 
 35 else
 36 {
 37     T=(BiTNode*)malloc(sizeof(BiTNode));
 38         T->data=ch;
 39         printf(" 请输入 %c 结点的左孩子:",T->data);
 40         CreatBiTree(T->lchild);
 41         printf(" 请输入 %c 结点的右孩子:",T->data);
 42         CreatBiTree(T->rchild);
 43 }
 44 return OK;
 45 }
 46 
 47 //递归先序遍历二叉树
 48 Status PreOrder ( BiTree T )
 49 {
 50  if ( T!=NULL)
 51  {
 52   printf("%c",T->data);  
 53   PreOrder ( T->lchild ); 
 54   PreOrder ( T->rchild ); 
 55  }
 56  return OK;
 57 }
 58 
 59 //递归中序遍历二叉树
 60 Status InOrder ( BiTree T )
 61 {
 62  if ( T!=NULL)
 63  {
 64   InOrder ( T->lchild ); 
 65   printf("%c",T->data);
 66   InOrder ( T->rchild );
 67  }
 68  return OK;
 69 }
 70 
 71 //递归后序遍历二叉树
 72 Status LastOrder ( BiTree T )
 73 {
 74  if (T!=NULL)
 75  {
 76   LastOrder( T->lchild ); 
 77   LastOrder( T->rchild );
 78   printf("%c",T->data);
 79  }
 80  return OK;
 81 }
 82 //初始化一个带头结点的队列
 83 Status InitQueue(LinkQueue &Q)
 84 {
 85  Q.front=(QNode*)malloc(sizeof(QNode));
 86  if (!Q.front)
 87   return ERROR;
 88  Q.rear=Q.front;
 89   Q.front->next=NULL;
 90  return OK;
 91 }
 92 
 93 //入队列
 94 Status EnQueue(LinkQueue &Q,BiTree e)
 95 {
 96  QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
 97  if (!s)
 98 return ERROR;
 99  s->Queuedata=e;
100  s->next=NULL;
101  Q.rear->next=s;
102  Q.rear=s;
103  return OK;
104 
105 }
106 //出队
107 int DelQueue(LinkQueue &Q, BiTree &e)
108 {
109  char data1;
110  QueuePtr s;
111  s=Q.front->next;
112  e=s->Queuedata;
113     data1=e->data;
114  Q.front->next=s->next;
115  if(Q.rear==s)
116   Q.rear=Q.front;
117  free(s);
118  
119  return OK;
120 }
121 //队列的判断空操作
122 Status QueueEmpty(LinkQueue Q)
123 {
124  
125  if (Q.front->next==NULL)
126     return OK;
127  else return ERROR;
128  
129 }
130 //按层次遍历
131 Status LevelBiTree(BiTree T)
132 {
133  LinkQueue Q; 
134  
135  InitQueue(Q); 
136  BiTree p = T;         
137  if (T == NULL) return ERROR; 
138  
139  EnQueue(Q,p); 
140  
141  while (!QueueEmpty(Q))      
142  {   DelQueue(Q, p); 
143   printf("%C",p->data);
144  
145  if (p->lchild)  
146   EnQueue(Q, p->lchild); 
147  if (p->rchild)   
148   EnQueue(Q, p->rchild); 
149  }
150  return OK;
151 }
152 
153 //树的深度
154 Status DepthTree(BiTree T){
155 int llength=0,rlength=0;
156 if(T==NULL) return 0;
157 else{
158 llength=DepthTree(T->lchild);
159 rlength=DepthTree(T->rchild);
160 return(llength>rlength)?(llength+1):(rlength+1);
161 }
162 }
163 
164 //叶子节点的个数
165 Status Leafnumber(BiTree T){
166 int rl=0,rh=0;
167 if(T==NULL)
168 return 0;
169 else if (T->lchild==NULL&&T->rchild==NULL) return 1;
170 else
171 {
172 rl=Leafnumber(T->lchild);
173 rh=Leafnumber(T->rchild);
174 return(rl+rh);
175 }
176 }
177 int main()
178 {
179     BiTree T;
180     printf("按照先序次序输入二叉树中结点的值.\n");
181     CreatBiTree(T);
182     int k;    
183     do
184     {  
185     printf("\n\n\n\n");
186     printf("\t\t\t  树 子系统\n");
187     printf("\t\t******************************\n");
188     printf("\t\t*        1----前序遍历    *\n");
189     printf("\t\t*        2----中序遍历    *\n");
190     printf("\t\t*        3----后序遍历    *\n");
191     printf("\t\t*        4----层次遍历     *\n");
192     printf("\t\t*        5----求树高度      *\n");
193     printf("\t\t*        6----叶子个数      *\n");
194     printf("\t\t*        0----返  回    *\n");
195     printf("\t\t******************************\n");
196     printf("\t\t 请选择菜单项(0-6):");
197     scanf("%d",&k);getchar();
198 
199     if (k==1)          
200     {
201         printf("\n   此树前序遍历的顺序:");
202         PreOrder(T);
203         printf("\n");
204                             
205     }
206     else if (k==2)
207     {    printf("\n   此树中序遍历的顺序:");
208         InOrder(T);printf("\n");
209                                 
210     }
211     else if (k==3)       //查找线性表中元素值为x的位置
212     {    
213         printf("\n   此树后序遍历的顺序:");
214          LastOrder(T);printf("\n");
215                             
216     }
217     else if (k==4)        //输出链表
218      {    
219         printf("\n   此树层次遍历的顺序:");
220                             
221         LevelBiTree(T);printf("\n");
222     }
223     else if (k==5)        //输出链表
224      {    
225                             
226         printf("\n此树的高度是:%d",DepthTree(T)); 
227     }
228      else if (k==6)        //输出链表
229      {    
230                             
231          printf("\n此树叶子结点个数是:%d",Leafnumber(T));
232     }
233     }while(k!=0);

部分运行结果:

技术分享图片

 

课设4---二叉树的操作

标签:菜单   info   typedef   div   初始化   init   tno   ued   http   

原文地址:https://www.cnblogs.com/zongyao/p/9255370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!