标题: 用C语言这么构建一个二叉树不成功原因?想求助一下 [打印本页]

作者: chao超    时间: 2018-8-11 13:55
标题: 用C语言这么构建一个二叉树不成功原因?想求助一下
typedef struct node Binary_Tree;
        typedef struct node
        {
                int  data;  
                struct node *leftnode;
                struct node *rightnode;

        }Binary_Tree,*Tree;

u8 Make_BinaryTree( Binary_Tree *tree , int *p )
{
        static int *q;    q=p;         
   if( *q==0)
   {
   tree=NULL;
   q++;
  return 0;  
   }
   else
   {
   tree=(Binary_Tree *)malloc(sizeof(Binary_Tree));
   tree->data=*q;
   printf("%d \n", tree->data);           
   q++;
   Make_BinaryTree( tree->leftnode , q );           
   Make_BinaryTree( tree->rightnode ,q );
   }

   return 0;
}

void Initialization_BinaryTree(void)
{
         
int array[]={1,2,3,0,0,4,0,0,5,6,0,0,7,0,0 };
Binary_Tree tree;

Make_BinaryTree( &tree , &array[0] );
printf("%d \n" ,tree.data);         
printf("%d \n" ,tree .rightnode->data);
printf("%d \n" ,tree.leftnode->data);
这三个输出都是乱码,我不知道二叉树的建立过程中问题在哪里,我应该在每个节点都分配了内存,不过结果好像有问题

}




作者: angmall    时间: 2018-8-11 17:23
给你一个试试

  1. /*-------------------------------------------------*/
  2. #include <stdio.h>
  3. #include <stdlib.h>

  4. typedef struct node
  5. {
  6.   int val;
  7.   struct node * left;
  8.   struct node * right;
  9. } node_t;

  10. void insert(node_t * tree,int val);
  11. void print_tree(node_t * current);
  12. void printDFS(node_t * current);

  13. int main()
  14. {
  15.   node_t * test_list = malloc(sizeof(node_t));
  16.   /* set values explicitly, alternative would be calloc() */
  17.   test_list->val = 0;
  18.   test_list->left = NULL;
  19.   test_list->right = NULL;

  20.   insert(test_list,5);
  21.   insert(test_list,8);
  22.   insert(test_list,4);
  23.   insert(test_list,3);

  24.   printDFS(test_list);
  25.   printf("\n");
  26. }

  27. void insert(node_t * tree, int val)
  28. {
  29.   if (tree->val == 0)
  30.   {
  31.     /* insert on current (empty) position */
  32.     tree->val=val;
  33.   }
  34.   else
  35.   {
  36.     if (val < tree->val)
  37.     {
  38.       /* insert left */
  39.       if (tree->left != NULL)
  40.       {
  41.         insert(tree->left, val);
  42.       }
  43.       else
  44.       {
  45.         tree->left = malloc(sizeof(node_t));
  46.         /* set values explicitly, alternative would be calloc() */
  47.         tree->left->val = val;
  48.         tree->left->left = NULL;
  49.         tree->left->right = NULL;
  50.       }
  51.     }
  52.     else
  53.     {
  54.       if (val >= tree->val)
  55.       {
  56.         /* insert right */
  57.         if (tree->right != NULL)
  58.         {
  59.           insert(tree->right,val);
  60.         }
  61.         else
  62.         {
  63.           tree->right=malloc(sizeof(node_t));
  64.           /* set values explicitly, alternative would be calloc() */
  65.           tree->right->val=val;
  66.           tree->right->left = NULL;
  67.           tree->right->right = NULL;
  68.         }
  69.       }
  70.     }
  71.   }
  72. }

  73. /* depth-first search */
  74. void printDFS(node_t * current)
  75. {
  76.   /* change the code here */
  77.   if (current == NULL)         return;   /* security measure */
  78.   if (current->left != NULL)   printDFS(current->left);
  79.   if (current != NULL)         printf("%d ", current->val);
  80.   if (current->right != NULL)  printDFS(current->right);
  81. }

复制代码

作者: chao超    时间: 2018-8-11 20:37
很感谢给代码,我主要是问问这么写到底错在哪里,我知道别的写法可以只是这种为什么不行
作者: angmall    时间: 2018-8-11 23:05
再好好看看书,看看别人的程序吧。

作者: zhaoyinlo    时间: 2018-9-3 12:44
再看看二叉树的知识点吧   看看它的基本操作




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1