二叉树的基础问题
帮忙看一下哪里错了,谢谢。创建二叉树的,我是在linux环境写的。
程序代码:
#include<stdlib.h> #include<ctype.h> #include<stdio.h> struct data { long content; int count; struct data *pleft=NULL; struct data *pright=NULL; }; int main() { struct data* creat_node(long); struct data* add_node(long,struct data*); char con; struct data *proot; long value; printf("please input the first data:"); setbuf(stdin,NULL); scanf("%ld",&value); proot=creat_node(value); while(1) { printf("continue to input?(y or n)"); setbuf(stdin,NULL); scanf("%c",&con); if(tolower(con)=='n') break; scanf("%ld",&value); add_node(value,proot); } } struct data* creat_node(long value) { struct data *node=malloc(sizeof(struct data)); node->content=value; node->count=1; return node; }; struct data* add_node(long value,struct data *node) { if(!node) return creat_node(value); if(node->content==value) { node->count++; return node; } if(node->content<value) return add_node(value,node->pleft); if(node->content>value) return add_node(value,node->pright); };