2017-11-20 4 views
-1

이유를 모르겠는데 도움이 필요합니다. 아래 코드는 입니다. 루트 인덱스 만 쓰면됩니다. 루트의 왼쪽 및 오른쪽 포인터는 여전히 비어 있습니다. 하지만 메모리에 할당하고 색인을 복사 한 문자열.CST의 BST - 루트에만 씁니다.

typedef struct bst { 
    char index[128]; 
    struct bst *left; 
    struct bst *right; 
}bst; 

int main(void) 
{ 
    bst *root = malloc (sizeof(bst)); 
    root->left = NULL; 
    root->right = NULL; 
    strcpy(root->index, "indexisnull"); 

    bst *temp; 
    temp = root; 

    FILE *fp, *fp2;       //file pointers 
    char word[128]; 

    fp = fopen("Input1.txt", "r");   //opening text files in read mode 
    fp2 = fopen("Input2.txt", "r"); 

    while(fscanf(fp,"%s", word) == 1) 
    {  
     //getting strings from file to string 'word'   

     temp = root; 

     if(strcmp(root->index, "indexisnull") == 0) 
     { 
      strcpy(root->index, word); 
     } 

     if(strcmp(word, root->index) < 0) 
     { 
      //temp = temp->left; 
      while(temp != NULL) 
      { 
       if(strcmp(word, temp->index) < 0) 
       { 
        printf("lefttoleft\n"); 
        temp = temp->left; 
       } 
       else if(strcmp(word, temp->index) > 0) 
       { 
        temp = temp->right; 
        printf("lefttoright\n"); 
       } 
      } 
      temp = malloc(sizeof(bst)); 
      strcpy(temp->index, word); 
      temp->left = NULL; 
      temp->right = NULL;   
     } 
     else if(strcmp(word, root->index) > 0) 
     { 
      temp = temp->right; 
      while(temp != NULL) 
      { 
       if(strcmp(word, temp->index) < 0) 
       { 
        temp = temp->left; 
        printf("righttoleft\n"); 
       } 
       else if(strcmp(word, temp->index) > 0) 
       { 
        temp = temp->right; 
        printf("righttoright\n"); 
       } 
      } 
      temp = malloc(sizeof(bst)); 
      strcpy(temp->index, word); 
      temp->left = NULL; 
      temp->right = NULL; 
     } 
    } 

    temp = root; 
    printf("%s\n", temp->index); 
    printf("%s\n", temp->right->index); 
    printf("%s\n", temp->left->index); 

    fclose(fp);  //closing files 
    fclose(fp2); 
    return 0; 
} 
+1

* 항상 게시물에 세부 정보를 추가 할 수 있습니다. [도움말 페이지] (http://stackoverflow.com/help), 특히 [여기서 어떤 주제에 관해서 물어볼 수 있습니까?] (http://stackoverflow.com/help/) 섹션을 읽어보십시오. on-topic) 및 [ "어떤 유형의 질문을하지 않아야합니까?"] (http://stackoverflow.com/help/dont-ask). 또한 [둘러보기] (http://stackoverflow.com/tour)와 [좋은 질문을하는 방법에 대해 읽어보십시오.] (http://stackoverflow.com/help/how-to-ask). 마지막으로 [** Minimal **, Complete, Verifiable Example] (http://stackoverflow.com/help/mcve)을 만드는 방법을 배우십시오. –

+0

이번에는 들여 쓰기가 있습니다. 다음에 나는 네 엄마 한테 그것에 대해 말할거야! – WedaPashi

+0

'fopen'이 필요한 것을 반환하는지 확인 했습니까? 'fp'와'fp2'를 확인하십시오. 시작하기 좋은 곳입니다. – WedaPashi

답변

0

gdb를 사용하여 접근 방식이 트리를 통과하는 방식을 이해할 것을 제안합니다. 프로그램을 디버깅하기위한 온라인 정보가 풍부합니다. temp의 메모리 주소를 인쇄 할 수 있도록 기본 및 단일 스테핑에서 중단 점을 설정하십시오.

내가 원하는 새 bst 노드에 메모리를 할당하지 않는다고 생각합니다. 루트 노드 만 가질 때 임시 동등 물은 무엇입니까? 루트 노드는 새롭게 할당 된 노드에 대한 기준이 필요 : temp-에 할당함으로써

if(strcmp(word, temp->index) < 0) { 
    printf("lefttoleft\n"); 
    temp->left = malloc(sizeof(bst)); 
    temp = temp->left; 
    strcpy(temp->index, word); 
    temp->left = NULL; 
    temp->right = NULL; 
} 
else if(strcmp(word, temp->index) > 0) { 
    temp->right = malloc(sizeof(bst)); 
    temp = temp->right; 
    strcpy(temp->index, word); 
    temp->left = NULL; 
    temp->right = NULL; 
    printf("lefttoright\n"); 
} 

> 좌 temp-> 권리를 부모 노드는 참조를 저장한다.

노드를 배치 할 적절한 위치를 찾는 것만 큼 (내가 준 노드는 하위 노드를 찾을 수 없으며 왼쪽 또는 오른쪽 자식으로 저장됩니다) 부모 노드를 개별적으로 추적하거나 왼쪽/삽입하기 전에 null에 대한 올바른 참조.