鱼C论坛

 找回密码
 立即注册
查看: 3253|回复: 9

[已解决]二叉树转化为森林 父子关系的递归遍历

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

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

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

x
本帖最后由 Martine 于 2018-6-25 18:08 编辑

题:将如图所示的深林转化为二叉树(易)
pic2.png pic1.png
将父子关系表示为 父(子,子......)
则该森林的数据结构可以表示为 A(B(EF)C(G)D)

  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 in(btree root)
  23. {
  24.         if(root)
  25.         {
  26.                 in(root->lchild);
  27.                 printf("[%c ] ", root->data);
  28.                 in(root->rchild);
  29.         }
  30. }


  31. void herit(btree root)
  32. {
  33.         printf("%c", root->data);
  34.         if(root->lchild || root->rchild){
  35.                 printf("(");
  36.                 if(root->lchild)
  37.                         herit(root->lchild);
  38.                 if(root->rchild)
  39.                         herit(root->rchild);
  40.                 printf(")");
  41.         }
  42. }


  43. int main(void)
  44. {
  45.         btree root;
  46.         CreateBtree(&root);
  47.         in(root);
  48.         putchar('\n');

  49.         herit(root);
  50.         putchar('\n');
  51.         printf(" \n######Wors*/k done!#######\n");

  52.         return 0;
  53. }
复制代码

output.png

如何增加边界检测才能去掉多余的括号 ?求解!!
最佳答案
2018-6-25 20:15:19
Martine 发表于 2018-6-25 19:17
错了, 答案是A(B(EF)C(G)D) 不是A(B(E(F)C(GD)));
D的父节点是A 不是C

1.png
貌似是对了?
我也不确定,这个结果是调试出来的,再换几组数据测试一下

  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. char data[] = "ABE#F##CG##D###";
  10. char *p = data;
  11. void CreateBtree(btree *root)
  12. {
  13.         ElemType c;
  14.         //scanf("%c", &c);
  15.         c = *p++;                // 为 debug 方便

  16.         if('#' == c){
  17.                 *root = NULL;
  18.         }
  19.         else{
  20.                 *root = (btree)malloc(sizeof(node));
  21.                 (*root)->data = c;

  22.                 CreateBtree(&(*root)->lchild);
  23.                 CreateBtree(&(*root)->rchild);
  24.         }
  25. }

  26. void in(btree root)
  27. {
  28.         if(root)
  29.         {
  30.                 in(root->lchild);
  31.                 printf("[%c ] ", root->data);
  32.                 in(root->rchild);
  33.         }
  34. }


  35. void herit(btree root)
  36. {
  37.         printf("%c", root->data);
  38.         if(root->lchild || root->rchild){
  39.                 if(root->lchild)
  40.                         printf("(");

  41.                 if(root->lchild)
  42.                         herit(root->lchild);
  43.                 if(root->rchild)
  44.                         herit(root->rchild);
  45.                
  46.                 //if(root->rchild)
  47.                 //        printf(")");
  48.         }
  49.         else
  50.                 printf(")");
  51. }


  52. int main(void)
  53. {
  54.         btree root;
  55.         CreateBtree(&root);
  56.         in(root);
  57.         putchar('\n');

  58.         herit(root);
  59.         putchar('\n');
  60.         printf(" \n######Wors*/k done!#######\n");

  61.         return 0;
  62. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-25 18:02:15 | 显示全部楼层
如果可以,把代码发完整,我研究研究
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-25 18:10:45 | 显示全部楼层
人造人 发表于 2018-6-25 18:02
如果可以,把代码发完整,我研究研究

测试数据 ABE#F##CG##D###
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-25 18:38:23 | 显示全部楼层
1.png

图片不对吧?

也就是说要输入 ABE#F##CG##D### ,输出 A(B(EF)C(G)D) 就行了?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-25 18:51:05 | 显示全部楼层
人造人 发表于 2018-6-25 18:38
图片不对吧?

也就是说要输入 ABE#F##CG##D### ,输出 A(B(EF)C(G)D) 就行了?

是!
要的就是你那种结果
你是怎么做到的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-25 19:01:31 | 显示全部楼层
Martine 发表于 2018-6-25 18:51
是!
要的就是你那种结果
你是怎么做到的?

我复制了你的代码,就是那个截图
不是输入 ABE#F##CG##D###,输出 A(B(EF)C(G)D) 吗?
现在是输入 ABE#F##CG##D###,输出 A(B(E(F)C(GD)))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-25 19:17:44 | 显示全部楼层
人造人 发表于 2018-6-25 19:01
我复制了你的代码,就是那个截图
不是输入 ABE#F##CG##D###,输出 A(B(EF)C(G)D) 吗?
现在是输入 ABE# ...

错了, 答案是A(B(EF)C(G)D) 不是A(B(E(F)C(GD)));
D的父节点是A 不是C
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-25 20:15:19 | 显示全部楼层    本楼为最佳答案   
Martine 发表于 2018-6-25 19:17
错了, 答案是A(B(EF)C(G)D) 不是A(B(E(F)C(GD)));
D的父节点是A 不是C

1.png
貌似是对了?
我也不确定,这个结果是调试出来的,再换几组数据测试一下

  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. char data[] = "ABE#F##CG##D###";
  10. char *p = data;
  11. void CreateBtree(btree *root)
  12. {
  13.         ElemType c;
  14.         //scanf("%c", &c);
  15.         c = *p++;                // 为 debug 方便

  16.         if('#' == c){
  17.                 *root = NULL;
  18.         }
  19.         else{
  20.                 *root = (btree)malloc(sizeof(node));
  21.                 (*root)->data = c;

  22.                 CreateBtree(&(*root)->lchild);
  23.                 CreateBtree(&(*root)->rchild);
  24.         }
  25. }

  26. void in(btree root)
  27. {
  28.         if(root)
  29.         {
  30.                 in(root->lchild);
  31.                 printf("[%c ] ", root->data);
  32.                 in(root->rchild);
  33.         }
  34. }


  35. void herit(btree root)
  36. {
  37.         printf("%c", root->data);
  38.         if(root->lchild || root->rchild){
  39.                 if(root->lchild)
  40.                         printf("(");

  41.                 if(root->lchild)
  42.                         herit(root->lchild);
  43.                 if(root->rchild)
  44.                         herit(root->rchild);
  45.                
  46.                 //if(root->rchild)
  47.                 //        printf(")");
  48.         }
  49.         else
  50.                 printf(")");
  51. }


  52. int main(void)
  53. {
  54.         btree root;
  55.         CreateBtree(&root);
  56.         in(root);
  57.         putchar('\n');

  58.         herit(root);
  59.         putchar('\n');
  60.         printf(" \n######Wors*/k done!#######\n");

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

使用道具 举报

 楼主| 发表于 2018-6-26 09:12:33 | 显示全部楼层
人造人 发表于 2018-6-25 20:15
貌似是对了?
我也不确定,这个结果是调试出来的,再换几组数据测试一下

感谢!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-6-26 09:13:03 | 显示全部楼层
人造人 发表于 2018-6-25 20:15
貌似是对了?
我也不确定,这个结果是调试出来的,再换几组数据测试一下

感谢!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-18 10:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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