欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1957|回復(fù): 4
收起左側(cè)

用C語(yǔ)言這么構(gòu)建一個(gè)二叉樹(shù)不成功原因?想求助一下

[復(fù)制鏈接]
回帖獎(jiǎng)勵(lì) 1 黑幣 回復(fù)本帖可獲得 1 黑幣獎(jiǎng)勵(lì)! 每人限 1 次(中獎(jiǎng)概率 50%)
ID:379586 發(fā)表于 2018-8-11 13:55 | 顯示全部樓層 |閱讀模式
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);
這三個(gè)輸出都是亂碼,我不知道二叉樹(shù)的建立過(guò)程中問(wèn)題在哪里,我應(yīng)該在每個(gè)節(jié)點(diǎn)都分配了內(nèi)存,不過(guò)結(jié)果好像有問(wèn)題

}



回復(fù)

使用道具 舉報(bào)

ID:155507 發(fā)表于 2018-8-11 17:23 | 顯示全部樓層
給你一個(gè)試試

  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. }

復(fù)制代碼
回復(fù)

使用道具 舉報(bào)

ID:379586 發(fā)表于 2018-8-11 20:37 | 顯示全部樓層
很感謝給代碼,我主要是問(wèn)問(wèn)這么寫到底錯(cuò)在哪里,我知道別的寫法可以只是這種為什么不行
回復(fù)

使用道具 舉報(bào)

ID:155507 發(fā)表于 2018-8-11 23:05 | 顯示全部樓層
再好好看看書,看看別人的程序吧。
回復(fù)

使用道具 舉報(bào)

ID:390775 發(fā)表于 2018-9-3 12:44 | 顯示全部樓層
再看看二叉樹(shù)的知識(shí)點(diǎn)吧   看看它的基本操作
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表