2014-12-06 2 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 Binary Search Вставка дерева

+1

'return node;' -> 'return root;' – BLUEPIXY

+0

@BLUEPIXY Я буквально сделал это, а затем закричал, а затем прочитал ваш комментарий , ХА! БЛАГОДАРЯ!!!!! – jgabb

ответ

0

return node; ->return root; Согласно комментарию BLUEPIXY, это был правильный ответ. BLUEPIXY 2 мин назад