打印二叉树期间的无限循环

Arp*_*pit 0 c binary-tree

这是我的代码,除了无限循环之外几乎正常工作 printInTree()

struct node{
    char text[100];
    int count;
    struct node* left;
    struct node* right;
};

struct node* addNode(struct node* n,char w[]){
    int cond=0;
    if(n == NULL){
        n=malloc(sizeof(struct node));
        n->count=1;
        n->left=NULL;
        n->right=NULL;
        strcpy(n->text,w);
    }
    else if((cond=strcmp(w,n->text))==0){
        n->count++;
    }
    else if(cond>0){
        n->right=addNode(n->right,w);
    }
    else{
        n->left=addNode(n->left,w);
    }
    return n;
};

void printInTree(struct node* p){   
    while(p != NULL){                //infinite loop here.
        printInTree(p->left);
        printf("%3s - %d\n",p->text,p->count);
        printInTree(p->right);

    }
}

void b_treeDemo(){
    struct node *root=NULL;
    FILE* f=fopen("main.c","r");
    char word[100];
    while(1){
        if(getWord(f,word)>0){
            if(isalpha(word[0])){
                root=addNode(root,word);
            }
        }else{
            break;
        }
    }
    printInTree(root);
}
Run Code Online (Sandbox Code Playgroud)

如何打破这个循环,以便它按顺序打印树.

Mic*_*ker 5

p在循环中没有变化,会使它变得有限吗?你想做的可能是

if(!p) return;
Run Code Online (Sandbox Code Playgroud)

而不是while循环.(要首先了解递归,您需要了解递归).