首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

二叉树的建立并输出解决方案

2012-03-29 
二叉树的建立并输出#includestdio.h#includestdlib.hstructnode{intvaluestructnode*child_lstructn

二叉树的建立并输出
#include   <stdio.h>
#include   <stdlib.h>

struct   node{
      int   value;
      struct   node   *child_l;
      struct   node   *child_r;
  };

int   add_node(struct   node   **pointer,   struct   node   *new_node);

struct   node   *create_node(int   data)
{
    struct   node   *new_node   =   NULL;
    struct   node   **pointer   =   NULL;

    new_node   =   (struct   node   *)malloc(sizeof(struct   node));
    new_node   ->   value   =   data;
    new_node   ->   child_l   =   NULL;
    new_node   ->   child_r   =   NULL;
    add_node(pointer,   new_node);

    return   new_node;
}

int   add_node(struct   node   **pointer,   struct   node   *new_node)
{
      if(*pointer   ==   NULL)
          {
            *pointer   =   new_node;
          }
      else
          {
              if((*pointer)   -> value   >   new_node-> value)
                        add_node(&(*pointer)   -> child_l,   new_node);
              else
                        add_node(&(*pointer)   -> child_r,   new_node);    
          }
    return   0;
}

int   main()
{
    struct   node   *new_node;
    int   i;
    int   A[12]   =   {3,31,45,47,51,58,67,72,78,85,99,105};    

    for(i=0;   i <12;   i++){
        new_node   =   create_node(A[i]);
        printf( "the   tree:   %d   ",   new_node   -> value);
    }
return   0;
}
=======================================================================
编译通过了,   就是一运行会出错,哪位高手指点下,谢谢了!!                                                          



[解决办法]
struct node *create_node(int data
+PtNode root
)
{
struct node *new_node = NULL;
struct node **pointer = NULL;

new_node = (struct node *)malloc(sizeof(struct node));
+ if(new_node==NULL){
destoryTree(root);
exit(-1);
}
new_node -> value = data;
new_node -> child_l = NULL;
new_node -> child_r = NULL;
add_node(pointer, new_node);

return new_node;
}

[解决办法]
struct node{
int value;
struct node *child_l;
struct node *child_r;
};



int add_node(struct node **pointer, struct node *new_node);

struct node *create_node(struct node **head, int data)
{
struct node *new_node = NULL;

new_node = (struct node *)malloc(sizeof(struct node));
new_node -> value = data;
new_node -> child_l = NULL;
new_node -> child_r = NULL;
add_node(head, new_node);

return new_node;
}

int add_node(struct node **head, struct node *new_node)
{
if(*head == NULL)
{
*head = new_node;
}
else
{
if((*head)-> value > new_node-> value)
add_node(&(*head)-> child_l, new_node);
else
add_node(&(*head)-> child_r, new_node);
}
return 0;
}


int main(int argc, char *argv[])
{
struct node *new_node, *head=NULL;
int i;
int A[12] = {3,31,45,47,51,58,67,72,78,85,99,105};

for(i=0; i <12; i++){
new_node = create_node(&head, A[i]);
printf( "the tree: %d ", new_node-> value);
}
return 0;
}
[解决办法]
if(*pointer == NULL)//程序运行到这里报错,原因在于下面的赋值语句
     struct node **pointer = NULL;//这里说明二重指针指向一个NULL,那么上面语句*pointer=*NULL,所以出错了
struct node **pointer = NULL;//如果把这个改为struct node *pointer = NULL;
调用add_node(pointer, new_node);时,这样调用add_node(&pointer, new_node);
整理一下也就是把add_node()函数修改如下,就解决问题了:
struct node *create_node(int data)
{
struct node *new_node = NULL;
struct node *pointer = NULL;

new_node = (struct node *)malloc(sizeof(struct node));
new_node -> value = data;
new_node -> child_l = NULL;
new_node -> child_r = NULL;
add_node(&pointer, new_node);

return new_node;
}

热点排行