插入二叉树时的错误?

时间:2015-03-02 01:34:22

标签: c pointers tree binary-search-tree

我有这个代码,但无法弄清楚它中的错误,或者有一些我遗漏的边缘情况。它是使用字典插入到二叉树中。我不是指针专家,可能存在内存问题或其他问题。

/* just.h 
 typedef struct node  *tree_ptr;
  typedef struct table *Table;
*/

#include <stdio.h>
#include <stdlib.h>
#include "just.h"
#include <string.h>

int comp(int x, int y ){
  if (x>y) return 1;
  else if (x<y) return -1;
 return 0;
}
struct node {
int age;
tree_ptr left;
tree_ptr right;
tree_ptr next;
};

struct table {
  tree_ptr head;
};

tree_ptr create(int age ) {
 tree_ptr t = malloc(sizeof(*t));
 t->age = age;
 t->left = t->right = NULL;
 return t;
 }

Table init() {
Table tt = malloc(sizeof(tree_ptr));
tt->head = NULL;
return tt;
}
Table insert(Table temp ,int age) {
//    Table node = init();
//    node->head = create(age);
//    node->head->next = temp->head;
//    return node;
Table node = temp;
if(node == NULL || temp==NULL || temp->head == NULL || node- >head==NULL) {
   //node = init();
   tree_ptr t = create(age);
   node->head = t;
   temp->head = t ;
   printf("wow %d \n",node->head->age);
   return node;
 }
 else {
   //  if(node!=NULL) {
   int cmp =  comp(node->head->age,age);
   printf("%d \n ", cmp);
   if(cmp < 0) {
      if(node->head->left==NULL) {
         tree_ptr t  = create(age);
         //node->head = malloc(sizeof(tree_ptr));
         node->head->left = t;
         printf(" left null %d \n",node->head->left->age);
         return node;
      }
      else {
         printf("wow added to left branch %d \n",node->head->left->age);
         return insert((Table)node->head->left,age);
      }
   }
   else if(cmp > 0) {
      if(node->head->right==NULL) {
         tree_ptr t  = create(age);
         //node = malloc(sizeof(tree_ptr));
         node->head->right = t;
         printf(" right null %d \n",node->head->right->age); 
         return node;
      }
      else {
         printf("wow added to right branch %d \n",node->head->right->age);
         return insert((Table)node->head->right,age);
      }
   }
   else return NULL;
  }    
  }

 void print_table(tree_ptr temp ) {
 if(temp==NULL)  
 return;
 print_table(temp->left);
 printf("%d \n ", temp->age);
 print_table(temp->right); 
 }

 int main(int argc,  char * argv[]) {  
 Table tb;
 tb = init();
 //tb->head = malloc(sizeof(tree_ptr));
 //if(tb->head == NULL) {
 //   printf("Why you are null??");
 //   tb->head = create(5); 
 //}
 for(int i=0;i<6;i++)
    tb = insert(tb,i);
 //  print_table(tb->head);
 // while(tb->head!=NULL) {
 //     printf("%d  \n", tb->head->age);
 //     tb->head = tb->head->next;
 // }
 return 0;
 }

1 个答案:

答案 0 :(得分:1)

删除隐藏的ptr并修复插入

#include <stdio.h>                                                             
#include <stdlib.h>                                                            
#include <string.h>                                                            

int comp(int x, int y ){                                                       
    if (x>y) return 1;                                                         
    else if (x<y) return -1;                                                   
    return 0;                                                                  
}                                                                              
typedef struct node node;                                                      

struct node {                                                                  
    int age;                                                                   
    node *left;                                                                
    node *right;                                                               
};                                                                             

typedef struct table {                                                         
    node *head;                                                                
} table;                                                                       

node *create(int age) {                                                        
    node *t = malloc(sizeof(*t));                                              
    t->age = age;                                                              
    t->left = t->right = NULL;                                                 
    return t;                                                                  
}                                                                              

table *init(table *tt) {                                                       
    tt->head = NULL;                                                           
    return tt;                                                                 
}
node *insert(node **_node ,int age) {                                          
    //    Table node = init();                                                 
    //    node->head = create(age);                                            
    //    node->head->next = temp->head;                                       
    //    return node;                                                         
    node *node = *_node;                                                       
    if(!node) {                                                                
        //node = init();                                                       
        node = create(age);                                                    
        printf("wow %d \n",node->age);                                         
        *_node = node;                                                         
        return node;                                                           
    }                                                                          
    else {                                                                     
        //  if(node!=NULL) {                                                   
        int cmp =  comp(node->age,age);                                        
        printf("%d \n ", cmp);                                                 
        if(cmp < 0) {                                                          
            if(node->left==NULL) {                                             
                node->left = create(age);                                      
                //node->head = malloc(sizeof(tree_ptr));                       
                printf(" left null %d \n",node->left->age);                    
                return node;                                                   
            }                                                                  
            else {                                                             
                printf("wow added to left branch %d \n",node->left->age);      
                return insert(&node->left,age);                                
            }                                                                  
        }                                                                      
        else if(cmp > 0) {                                                     
            if(node->right==NULL) {                                            
                node->right = create(age);                                     
                printf(" right null %d \n",node->right->age);                  
                return node;                                                   
            }                                                                  
            else {                                                             
                printf("wow added to right branch %d \n",node->right->age);    
                return insert(&node->right,age);                               
            }                                                                  
        }                                                                      
        else return NULL;                                                      
    }                                                                          
    }                                                                          

    void print_table(node *temp) {                                             
        if(temp==NULL)                                                         
            return;                                                            
        print_table(temp->left);                                               
        printf("%d \n ", temp->age);                                           
        print_table(temp->right);                                              
    }
 int main(int argc,  char * argv[]) {                                       
        table tb;                                                              
        init(&tb);                                                             
        //tb->head = malloc(sizeof(tree_ptr));                                 
        //if(tb->head == NULL) {                                               
        //   printf("Why you are null??");                                     
        //   tb->head = create(5);                                             
        //}                                                                    
        for(int i=0;i<6;i++) {                                                 
            insert(&tb.head,i * 7);                                            
        }                                                                      
        for(int i=0;i<6;i++) {                                                 
            insert(&tb.head,i * 3);                                            
        }                                                                      
        print_table(tb.head);                                                  
        // while(tb->head!=NULL) {                                             
        //     printf("%d  \n", tb->head->age);                                
        //     tb->head = tb->head->next;                                      
        // }                                                                   
        return 0;                                                              
    }