1.[文件] main.c ~ 2KB 下载(11)
#include
#include
#include
#include
/*定义二叉树结构体*/
/*word表示节点,left指针及right指针均表示左右孩子节点*/
struct tnode{
char *word;
int count;
struct tnode *left;
struct tnode *right;
};
/*用于分配结构体内存*/
struct tnode *talloc(void)
{
return (struct tnode *) malloc (sizeof(struct tnode));//注意此处有一个强制转换,原本是返回一个无类型的指针
}
/*用于赋值字符串到返回值指针上面*/
char *strdup1(char *s)
{
char *p;
p=(char *)malloc(strlen(s)+1);//分配一定空间,注意此处要加上最后一个字符串结束符的空间1
if(p!=NULL)
strcpy(p,s);
return p;
}
/*打印树*/
void treeprint(struct tnode *p)
{
if(p!=NULL) {
treeprint(p->left);
printf("%4d %s\n",p->count,p->