zoukankan      html  css  js  c++  java
  • 课设4---二叉树的操作

    二叉树

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

      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("按照先序次序输入二叉树中结点的值.
    ");
    181     CreatBiTree(T);
    182     int k;    
    183     do
    184     {  
    185     printf("
    
    
    
    ");
    186     printf("			  树 子系统
    ");
    187     printf("		******************************
    ");
    188     printf("		*        1----前序遍历    *
    ");
    189     printf("		*        2----中序遍历    *
    ");
    190     printf("		*        3----后序遍历    *
    ");
    191     printf("		*        4----层次遍历     *
    ");
    192     printf("		*        5----求树高度      *
    ");
    193     printf("		*        6----叶子个数      *
    ");
    194     printf("		*        0----返  回    *
    ");
    195     printf("		******************************
    ");
    196     printf("		 请选择菜单项(0-6):");
    197     scanf("%d",&k);getchar();
    198 
    199     if (k==1)          
    200     {
    201         printf("
       此树前序遍历的顺序:");
    202         PreOrder(T);
    203         printf("
    ");
    204                             
    205     }
    206     else if (k==2)
    207     {    printf("
       此树中序遍历的顺序:");
    208         InOrder(T);printf("
    ");
    209                                 
    210     }
    211     else if (k==3)       //查找线性表中元素值为x的位置
    212     {    
    213         printf("
       此树后序遍历的顺序:");
    214          LastOrder(T);printf("
    ");
    215                             
    216     }
    217     else if (k==4)        //输出链表
    218      {    
    219         printf("
       此树层次遍历的顺序:");
    220                             
    221         LevelBiTree(T);printf("
    ");
    222     }
    223     else if (k==5)        //输出链表
    224      {    
    225                             
    226         printf("
    此树的高度是:%d",DepthTree(T)); 
    227     }
    228      else if (k==6)        //输出链表
    229      {    
    230                             
    231          printf("
    此树叶子结点个数是:%d",Leafnumber(T));
    232     }
    233     }while(k!=0);

    部分运行结果:

    不经一番彻骨寒,哪有梅花扑鼻香?
  • 相关阅读:
    linux常用小技巧(持续更新中)
    【CodeBase】PHP将数组键名转成变量名
    【Mysql】给mysql配置远程登录
    【Codebase】JQuery获取表单部分数据提交方法
    【shopex】添加网页挂件widgets
    【shopex】真正可用的app开发机制
    【转】Mysql查询语句优化策略
    【Ecshop】修改处理用户购物车的行为
    【Ecshop】商品数据采集扩展
    supervisor 使用
  • 原文地址:https://www.cnblogs.com/zongyao/p/9255370.html
Copyright © 2011-2022 走看看