鱼C论坛

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

输入一个数组, 生成一个二叉树排序树

[复制链接]
发表于 2018-7-16 14:35:36 | 显示全部楼层 |阅读模式

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

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

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

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


  8. btree createNode(int num)
  9. {
  10.         btree newnode = (btree)malloc(sizeof(node));
  11.         newnode->data = num;
  12.         return newnode;
  13. }

  14. void CreateBtreeFromArray(btree *root, int array[] , int begin, int end)
  15. {
  16.         if((end - begin)==1){
  17.                 *root = createNode(array[0]);
  18.         }else{
  19.                 int mid = begin + (end-begin)/2;
  20.                 *root = createNode(array[1]);
  21.                 CreateBtreeFromArray(&(*root)->lchild, array, begin, mid);
  22.                 CreateBtreeFromArray(&(*root)->rchild, array, mid+1, end);
  23.         }
  24. }


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


  34. int main(void)
  35. {
  36.         int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  37.         int len = sizeof(a)/sizeof(int);
  38.        
  39.         btree root;
  40.         CreateBtreeFromArray(&root, a, 0, len);
  41.         in(root);
  42.         putchar('\n');

  43.         return 0;
  44. }       
复制代码




结果总是 segmentation default 这是为什么啊?@人造人 @风扫地
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-16 14:57:49 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>

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


  8. btree createNode(int num)
  9. {
  10.         btree newnode = (btree)malloc(sizeof(node));
  11.         newnode->data = num;
  12.         return newnode;
  13. }

  14. void CreateBtreeFromArray(btree *root, int array[], int begin, int end)
  15. {
  16.         if((end - begin) == 1)
  17.         {
  18.                 *root = createNode(array[0]);
  19.                 (*root)->lchild = NULL;
  20.                 (*root)->rchild = NULL;
  21.         }
  22.         else
  23.         {
  24.                 int mid = begin + (end - begin) / 2;
  25.                 *root = createNode(array[1]);
  26.                 CreateBtreeFromArray(&(*root)->lchild, array, begin, mid);
  27.                 //CreateBtreeFromArray(&(*root)->rchild, array, mid + 1, end);
  28.                 CreateBtreeFromArray(&(*root)->rchild, array, mid, end);
  29.         }
  30. }


  31. void in(btree root)
  32. {
  33.         if(root)
  34.         {
  35.                 in(root->lchild);
  36.                 printf("[%d  ] ", root->data);
  37.                 in(root->rchild);
  38.         }
  39. }


  40. int main(void)
  41. {
  42.         int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  43.         int len = sizeof(a) / sizeof(int);

  44.         btree root;
  45.         CreateBtreeFromArray(&root, a, 0, len);
  46.         in(root);
  47.         putchar('\n');

  48.         return 0;
  49. }
复制代码


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

使用道具 举报

 楼主| 发表于 2018-7-18 17:08:05 | 显示全部楼层

后来发现是 少了递归出口
在第28行 加上 if(begin < end) 就好啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-18 17:54:45 | 显示全部楼层
Martine 发表于 2018-7-18 17:08
后来发现是 少了递归出口
在第28行 加上 if(begin < end) 就好啦

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-18 08:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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