move_uploaded_file()
答案 0 :(得分:0)
我的猜测不是正确的输出,因为它没有使用stree
的返回值进行更新。
E.g。 if(info<r->info)stree(r,r->left,info);
&lt; - 已被丢弃的返回值。
您的代码无法编译。所以,我重写了代码。
#include <stdio.h>
#include <stdlib.h>
struct tree {
char info;
struct tree *left, *right;
};
struct tree *stree(struct tree *root, char info);//no need `struct tree *r`
void print_tree(struct tree *root, int level);
int main(void){
struct tree *root = NULL;//It does not need to be in a global variable
char letter;
for(;;){
printf("enter a letter:");
if(EOF == scanf("%c", &letter) || letter == '\n')//don't use `gets`
break;
scanf("%*[^\n]");//clear upto newline
scanf("%*c");
root = stree(root, letter);
}
print_tree(root, 0);
//deallocate
return 0;
}
struct tree *stree(struct tree *root, char info){
if(!root){
struct tree *r = malloc(sizeof(*r));
if(!r){
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
r->right = r->left = NULL;
r->info = info;
return r;
}
if(info < root->info)
root->left = stree(root->left, info);
else if(info > root->info)
root->right = stree(root->right, info);
else
;//root->right = stree(root->right, info);
return root;
}
void print_tree(struct tree *r, int level){
if(!r) return ;
print_tree(r->right, level + 1);
printf("%*s%c\n", level, "", r->info);
print_tree(r->left, level + 1);
}