鱼C论坛

 找回密码
 立即注册
查看: 2118|回复: 3

[已解决]二叉树 遍历问题

[复制链接]
发表于 2018-7-9 17:25:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef char ElemType;
  4. typedef struct node
  5. {
  6.         ElemType data;
  7.         struct node *lchild, *rchild;
  8. }node, *btree;

  9. void CreateBtree(btree *root)
  10. {
  11.         ElemType c;
  12.         scanf("%c", &c);
  13.         if(c == '#'){
  14.                 *root = NULL;
  15.         }else{
  16.                 *root = (btree)malloc(sizeof(node));
  17.                 (*root)->data = c;

  18.                 CreateBtree(&(*root)->lchild);
  19.                 CreateBtree(&(*root)->rchild);
  20.         }
  21. }

  22. void CreateBtree2(node head[], int n, ElemType c[])
  23. {
  24.         int i;
  25.         btree ptr;
  26.         for(i=1; i<n; i++)
  27.         {
  28.                 ptr = &head[i];
  29.                 ptr->data = c[i-1];
  30.                 ptr->lchild = NULL;
  31.                 ptr->rchild = NULL;
  32.         }
  33.        
  34.         for(i=1; i<n; i++)
  35.         {
  36.                 ptr = &head[i];
  37.                 //printf("==== %c\n", ptr->data);
  38.                 if(2*i+1 < n)
  39.                 {
  40.                         btree l = ptr->lchild = &head[2*i];
  41.                         //printf("L  %c  \n", l->data);
  42.                         btree r = ptr->rchild = &head[2*i+1];
  43.                         //printf("R  %c  \n", r->data);
  44.                 }
  45.         }
  46. }



  47. void in(btree root)
  48. {
  49.         if(root)
  50.         {
  51.                 in(root->lchild);
  52.                 printf("[%c] ", root->data);
  53.                 in(root->rchild);
  54.         }
  55. }


  56. void pos(btree root)
  57. {
  58.         if(root)
  59.         {
  60.                 in(root->lchild);
  61.                 in(root->rchild);
  62.                 printf("[%c] ", root->data);
  63.         }

  64. }
  65. int main(void)
  66. {
  67.         node head[8];
  68.         btree ptr, newnode;
  69.         int i;
  70.         ElemType c[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'};
  71.         CreateBtree2(head, 8, c);

  72.         in(&head[1]);
  73.         putchar('\n');

  74.         pos(&head[1]);

  75.         putchar('\n');

  76.         return 0;
  77. }
复制代码



最佳答案
2018-7-10 16:50:04
我相信你不相信这个结果
调用错函数了 ^_^
1.png
2.png

  1. void pos(btree root)
  2. {
  3.         if(root)
  4.         {
  5.                 /*in(root->lchild);
  6.                 in(root->rchild);*/
  7.                 pos(root->lchild);
  8.                 pos(root->rchild);
  9.                 printf("[%c] ", root->data);
  10.         }

  11. }
复制代码

后序 无法递归遍历

后序 无法递归遍历
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-10 16:50:04 | 显示全部楼层    本楼为最佳答案   
我相信你不相信这个结果
调用错函数了 ^_^
1.png
2.png

  1. void pos(btree root)
  2. {
  3.         if(root)
  4.         {
  5.                 /*in(root->lchild);
  6.                 in(root->rchild);*/
  7.                 pos(root->lchild);
  8.                 pos(root->rchild);
  9.                 printf("[%c] ", root->data);
  10.         }

  11. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-10 16:56:00 | 显示全部楼层
可以看得出,你在写 pos 函数时是复制的 in 函数,然后忘记改了
^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-11 19:26:30 | 显示全部楼层
人造人 发表于 2018-7-10 16:50
我相信你不相信这个结果
调用错函数了 ^_^

自己太粗心!!
谢谢! ^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 04:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表