에서 이진 트리 구현 C 질문에서 K & RC 책을 통해 읽었으며 질문이 있습니다. 140-141 페이지의 6 장 구조체에 다음과 같은 코드가 있습니다 (나는 더 관련이없는 부분의 일부를했다)K & R
/*
the program loops through a tree looking for some word
if it finds the word itll incremenet the count by 1
if it doesnt itll add a new node
*/
struct node {
char *word;
int count;
struct node *left;
struct node *right;
}
main() {
struct node *root;
char word[1000];
root = NULL;
while(getword(word, MAXWORD) != EOF) /* getword just grabs 1 word at a time from a file of words */
if(isalpha(word[0])) /* isalpha checks to see if it is a valid word */
root = addNode(root, word);
treeprint(root); /* prints the tree */
return 0;
}
struct node *addNode(struct node *p, char *w) {
int cond;
if(p == NULL) {
p = malloc(sizeof(struct node)); /* allocates memory for the new node */
p -> word = strdup(w);
p -> count = 1;
p -> left = p -> right = NULL;
}
else if ((cond = strcmp(w, p -> word)) == 0)
p -> count++;
else if(cond < 0)
p -> left = addNode(p -> left, w);
else
p -> right = addNode(p -> right, w);
return p;
}
그리고 내 혼란이 루트 = addNode 명 (루트, 워드)
는 addNode 명 새로 추가에 대한 포인터를 반환하는 경우의 main() 함수에 노드 (또는 그 단어가 이미 int 인 경우 해당 노드에 있음)를 선택하면 트리 위의 모든 데이터가 "손실"되지 않습니까? 나무 뿌리로 머물러 있어야하지 않습니까?
감사합니다.
여기에서 재귀를 설명했습니다. http://stackoverflow.com/questions/6420309/how-can-i-analyze-a-recursive-source-codes-by-hand/6420521#6420521 – stacker