哪位高手帮小弟我看看啊 二叉树有关问题 c
谁帮我看看啊 二叉树问题 c
#include "string.h "
#include "stdio.h "
#define elemType char
#define Maxsize 20
struct node {
elemType *data;
struct node *lchild;
struct node *rchild;
int count;
};
int main()
{
struct node *root,*create_tree(struct node *p,elemType *ch );
char *ch;
char *getChar();
void visit_node(elemType *data);
void print_tree(struct node *p);
root=NULL;
while((ch=getChar())!=NULL){
root=create_tree(root,ch );
}
print_tree(root);
system( "pause ");
return 0;
}
char *getChar(){
char *p;
p=(char*)malloc(Maxsize);
if(!p){
printf( "out of memory\n ");
return NULL;
}
gets(p);
if(strlen(p)==0) return NULL;
return p;
}
struct node *create_tree(p,w)
struct node *p;char *w;
{
int con;
if(p==NULL) {
if( (p=(struct node*)malloc(sizeof(struct node))) ==NULL){
printf( "out of memory \n "); return NULL;
}
p-> count=1;p-> lchild=NULL; p-> rchild=NULL; p-> data=w;
}
if((con=strcmp(w,p-> data))> 0) {
create_tree(p-> rchild,w);
}
else if(con==0) {
p-> count++;
}
else if(con <0){
create_tree(p-> lchild,w);
}
return p ;
}
void print_tree(p)
struct node *p;
{
if(!p)
{
printf( "there has no information\n ");
}
if(p){
print_tree(&p-> lchild);
visit_node(p-> data);
print_tree(&p-> rchild);
}
return ;
}
void visit_node(data)
elemType *data;
{
puts(data);
return ;
}
------解决方案--------------------
你用的c语言是不是比较老的c。
我给你一个标准c的代码,功能应该是一样的,你自己对照一下看哪里错了。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h> //分配空间
#define MAXWORD 100
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp=0;
struct tnode{
char *word;
int count;
struct tnode *left;
struct tnode *right;
};
struct tnode *addtree(struct tnode *,char *);
void treeprint(struct tnode *);
int getword(char*,int);
main()
{
struct tnode *root;
char word[MAXWORD];
root =NULL;
while(getword(word,MAXWORD)!=EOF)
if(isalpha(word[0]))
root=addtree(root,word);
treeprint(root);
return 0;
}
struct tnode *talloc(void);
char *strdup(char *);
struct tnode *addtree(struct tnode *p,char *w)
{
int cond;
if(p==NULL){
p=talloc();
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)
#include "string.h "
#include "stdio.h "
#define elemType char
#define Maxsize 20
struct node {
elemType *data;
struct node *lchild;
struct node *rchild;
int count;
};
int main()
{
struct node *root,*create_tree(struct node *p,elemType *ch );
char *ch;
char *getChar();
void visit_node(elemType *data);
void print_tree(struct node *p);
root=NULL;
while((ch=getChar())!=NULL){
root=create_tree(root,ch );
}
print_tree(root);
system( "pause ");
return 0;
}
char *getChar(){
char *p;
p=(char*)malloc(Maxsize);
if(!p){
printf( "out of memory\n ");
return NULL;
}
gets(p);
if(strlen(p)==0) return NULL;
return p;
}
struct node *create_tree(p,w)
struct node *p;char *w;
{
int con;
if(p==NULL) {
if( (p=(struct node*)malloc(sizeof(struct node))) ==NULL){
printf( "out of memory \n "); return NULL;
}
p-> count=1;p-> lchild=NULL; p-> rchild=NULL; p-> data=w;
}
if((con=strcmp(w,p-> data))> 0) {
create_tree(p-> rchild,w);
}
else if(con==0) {
p-> count++;
}
else if(con <0){
create_tree(p-> lchild,w);
}
return p ;
}
void print_tree(p)
struct node *p;
{
if(!p)
{
printf( "there has no information\n ");
}
if(p){
print_tree(&p-> lchild);
visit_node(p-> data);
print_tree(&p-> rchild);
}
return ;
}
void visit_node(data)
elemType *data;
{
puts(data);
return ;
}
------解决方案--------------------
你用的c语言是不是比较老的c。
我给你一个标准c的代码,功能应该是一样的,你自己对照一下看哪里错了。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h> //分配空间
#define MAXWORD 100
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp=0;
struct tnode{
char *word;
int count;
struct tnode *left;
struct tnode *right;
};
struct tnode *addtree(struct tnode *,char *);
void treeprint(struct tnode *);
int getword(char*,int);
main()
{
struct tnode *root;
char word[MAXWORD];
root =NULL;
while(getword(word,MAXWORD)!=EOF)
if(isalpha(word[0]))
root=addtree(root,word);
treeprint(root);
return 0;
}
struct tnode *talloc(void);
char *strdup(char *);
struct tnode *addtree(struct tnode *p,char *w)
{
int cond;
if(p==NULL){
p=talloc();
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)