首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

哪位高手帮小弟我看看啊 二叉树有关问题 c

2012-02-05 
谁帮我看看啊 二叉树问题 c#include string.h #include stdio.h #defineelemTypechar#defineMaxsize20

谁帮我看看啊 二叉树问题 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)


p-> left =addtree(p-> left ,w);
else
p-> right =addtree(p-> right ,w);
return p;
}

void treeprint(struct tnode *p)
{
if(p!=NULL){
treeprint(p-> left );
printf( "%4d %s\n ",p-> count ,p-> word );
treeprint(p-> right );
}
}


struct tnode *talloc(void)//给新的结构体分配空间
{
return(struct tnode *)malloc(sizeof(struct tnode));
}

char *strdup(char *s)//save the word
{
char *p;
p=(char *)malloc(strlen(s)+1);
if(p!=NULL)
strcpy(p,s);
return p;
}
int getword(char *word,int lim)
{
char c;
char *w=word;
while(isspace(c=getchar()))
;
if(c!=EOF)
*w++=c;
if(!isalpha(c))
{
*w= '\0 ';
return c;
}
while(isalpha(c=getchar()))
{
*w++=c;
}
*w= '\0 ';
return word[0];
}

[解决办法]
root=create_tree(root,ch );!!!!!看这里ch不是一个指针不能带到create_tree中。

热点排行