2014-12-06 133 views
0
typedef struct word { 
    char *str;    
    int freq;    
    struct word *right; 
    struct word *left; 
    } Word; 



Word *root = NULL; //global 

while(pCounter != NULL){ 
      if(root == NULL){ 
       Word *x = (Word *)malloc(sizeof(Word)); 
       x->str = (char*)malloc(strlen(pCounter->str)+1); 
       //printf("%s", node->str); 
       strcpy(x->str,pCounter->str); 
       x->freq = pCounter->freq; 
       x->left = NULL; 
       x->right = NULL; 
       root = x; 
       } 
       else { 
        Insert(pCounter, root); 
       } 
      pCounter = pCounter ->pNext; 
     } 


void * Insert(Word *node, Word *root) 
     { 
      printf("inserted%s\n", node->str); 
      if(root==NULL) 
      { 
       Word *x = (Word *)malloc(sizeof(Word)); 
       x->str = (char*)malloc(strlen(node->str)+1); 
       //printf("%s", node->str); 
       strcpy(x->str,node->str); 
       x->freq = node->freq; 
       x->left = NULL; 
       x->right = NULL; 
       return x; 
       //node = root; 
      } 
      else if (strcmp(node->str, root->str)==0){ 

       root -> freq = root->freq+1; 
      } 
      else if (strcmp(node->str, root->str)<1){ 

       root->left = Insert(node,root->left); 
      } 
      else { 
       root->right = Insert(node, root->right);  
      } 

      return node; 


     } 

void ordered(Word *n){ 
      //printf("ordered"); 
    if(n != NULL){ 
     ordered(n->left); 
     printf("%-30s %5d\n", n->str, n->freq); 
     ordered(n->right); 
      } 
     } 

我试图建立一个二叉搜索树来处理链接列表成一个有序的bst。我可以让root的输出显示正确,但没有其他的东西。它吐出一些垃圾,我不知道为什么。我设置了一个printf语句,它显示它正在插入实际的字符串。难道我做错了什么?这不是所有的代码,但我认为这足以让人们明白我在做什么。建议?C二进制搜索树插入

+1

'返回节点;' - >'返回根;' – BLUEPIXY 2014-12-06 22:54:59

+0

@BLUEPIXY我从字面上做到了,然后尖叫然后阅读您的评论。哈!谢谢!!!!! – jgabb 2014-12-06 22:57:08

回答

0

return node; - >return root;根据BLUEPIXY的评论,这是正确的答案.- BLUEPIXY 2分钟前