2014-12-06 15 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로 처리하기 위해 이진 검색 트리를 작성하려고합니다. 루트 출력을 올바르게 표시 할 수는 있지만 다른 것은 표시 할 수 없습니다. 그것은 약간의 쓰레기를 뱉어 낸다. 그리고 나는 확실하지 않다 왜. printf 문을 설정하고 실제 문자열을 삽입하고 있음을 보여줍니다. 내가 뭐 잘못하고 있니? 이것은 모든 코드가 아니지만 사람들이 내가하는 일을 이해할 수있을 정도로 충분하다고 생각합니다. 제안?C 바이너리 검색 트리 삽입

+1

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

+0

@BLUEPIXY 글자 그대로 말한 다음 비명을지면서 의견을 읽었습니다. . 하아! 감사!!!!! – jgabb

답변

0

return node; ->return root; BLUEPIXY의 의견에 따르면, 정답입니다 .- BLUEPIXY 2 분 전