二叉树的建立与基本操作

编写程序实现二叉树的如下操作:
1) 建立二叉链表
2) 二叉树的先序、中序、后序遍历
3) 求解二叉树的叶子结点个
4) 将二叉树中所有结点的左、右子树相互交换

输入:
  扩展二叉树先序序列:ab#d##ce###。其中#代表空指针。

输出:
       二叉树的凹入表示
  二叉树的先序序列、中序序列、后序序列
  二叉树叶子结点个数
  左、右子树相互交换后的二叉树的凹入表示
  左、右子树相互交换后的二叉树的先序序列、中序序列、后序序列。

说明:
  在输出凹入表示的二叉树时,先输出根结点,然后依次输出左右子树,上下层结点之间相隔 3 个空格。

测试用例 1 以文本方式显示
  1. ab#d##ce###↵
以文本方式显示
  1. BiTree↵
  2. a↵
  3.     b↵
  4.         d↵
  5.     c↵
  6.         e↵
  7. pre_sequence  : abdce↵
  8. in_sequence   : bdaec↵
  9. post_sequence : dbeca↵
  10. Number of leaf: 2↵
  11. BiTree swapped↵
  12. a↵
  13.     c↵
  14.         e↵
  15.     b↵
  16.         d↵
  17. pre_sequence  : acebd↵
  18. in_sequence   : ceadb↵
  19. post_sequence : ecdba↵
1秒 64M 0
测试用例 2 以文本方式显示
  1. abd##e##cf##g##↵
以文本方式显示
  1. BiTree↵
  2. a↵
  3.     b↵
  4.         d↵
  5.         e↵
  6.     c↵
  7.         f↵
  8.         g↵
  9. pre_sequence  : abdecfg↵
  10. in_sequence   : dbeafcg↵
  11. post_sequence : debfgca↵
  12. Number of leaf: 4↵
  13. BiTree swapped↵
  14. a↵
  15.     c↵
  16.         g↵
  17.         f↵
  18.     b↵
  19.         e↵
  20.         d↵
  21. pre_sequence  : acgfbed↵
  22. in_sequence   : gcfaebd↵
  23. post_sequence : gfcedba↵
1秒 64M 0

#include<stdio.h> 
#include<stdlib.h> 
 
struct Btreenode 
{ 
   char element; 
  struct Btreenode *left; 
    struct Btreenode *right; 
}; 
typedef struct Btreenode *PtrNode; 
int numlef; 
void Creat_Btree(PtrNode*proot)     //用二级指针创建二叉树 
{ 
  char c; 
    c = getchar(); 
 if (c == '#') 
      *proot = NULL; 
 else 
   { 
      (*proot) = (PtrNode)malloc(sizeof (struct Btreenode)); 
     (*proot)->element = c; 
      Creat_Btree(&(*proot)->left); 
       Creat_Btree(&(*proot)->right); 
      if (!(*proot)->left&&!(*proot)->right) 
           numlef++; 
  } 
} 
 
PtrNode creat_BTree(PtrNode proot)  //用返回值创建二叉树(没用二级指针) 
{ 
 
    char c; 
    c = getchar(); 
 if (proot == NULL&&c!='#') 
 { 
      proot = (PtrNode)malloc(sizeof (struct Btreenode)); 
        proot->element = c; proot->left = proot->right = NULL; 
        proot->left = creat_BTree(proot->left); 
      proot->right = creat_BTree(proot->right); 
        if (!(proot)->left&&!(proot)->right) 
         numlef++; 
  } 
  return proot; 
} 
 
void inorder(PtrNode rt) 
{ 
    if (rt) 
    { 
      inorder(rt->left); 
      printf("%c", rt->element); 
      inorder(rt->right); 
 } 
} 
 
void preorder(PtrNode rt) 
{ 
   if (rt) 
    { 
      printf("%c", rt->element); 
      preorder(rt->left); 
     preorder(rt->right); 
    } 
} 
 
void postorder(PtrNode rt) 
{ 
  if (rt) 
    { 
 
     postorder(rt->left); 
        postorder(rt->right); 
       printf("%c", rt->element); 
  } 
} 
 
void Printline(char c, int num) 
{ 
 for (int i = 0; i < 4*num; i++) 
 { 
      printf(" "); 
   } 
  printf("%c\n", c); 
} 
 
void listtree(PtrNode rt, int depth) 
{ 
   if (rt) 
    { 
      Printline(rt->element, depth); 
      listtree(rt->left, depth + 1); 
      listtree(rt->right, depth + 1); 
 } 
} 
 
void Swap(PtrNode rt) 
{ 
   if (rt) 
    { 
 
     if (rt->left||rt->right) 
     { 
          PtrNode tmp = rt->left; 
         rt->left = rt->right; 
            rt->right = tmp; 
            if (rt->left) 
               Swap(rt->left); 
         if (rt->right) 
              Swap(rt->right); 
        } 
  } 
} 
int main() 
{ 
   //freopen("1.txt","r", stdin); 
 PtrNode root=NULL; 
 printf("BiTree\n"); 
    //Creat_Btree(&root); 
  root=creat_BTree(root); 
    listtree(root, 0); 
 
 
   printf("pre_sequence  : "); 
    preorder(root); printf("\n"); 
  printf("in_sequence   : "); 
    inorder(root); printf("\n"); 
   printf("post_sequence : "); 
    postorder(root); printf("\n"); 
 
 
   printf("Number of leaf: %d\n", numlef); 
    printf("BiTree swapped\n"); 
    Swap(root); 
    listtree(root, 0); 
 
    printf("pre_sequence  : "); 
    preorder(root); printf("\n"); 
  printf("in_sequence   : "); 
    inorder(root); printf("\n"); 
   printf("post_sequence : "); 
    postorder(root); printf("\n"); 
 
 
   return 0; 
} 


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水之积也不厚,则其负大舟也无力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值