主题:如何实现先序遍历,中序遍历,后序遍历??
#include<stdio.h>
#include<malloc.h>
#define NULL 0
typedef struct btnode
{
char data;
struct btnode *lchild,*rchild;
}btnode;
btnode *t;
int leaves=0;
btnode *create (btnode *p)
{ char ch;
if ((ch=getchar())=='#')
p=NULL;
else
{
p=(btnode *)malloc(sizeof(btnode));
p->data=ch;
p->lchild=create(p->lchild);
p->rchild=create(p->rchild);
}
return p;
}
int nodes(btnode *p)
{
int node=0;
if(p==NULL)
node=0;
else
node=1+nodes(p->lchild)+nodes(p->rchild);
return(node);
}
int leaf(btnode *t)
{
if(t)
{
leaf(t->lchild);
if(!(t->lchild||t->rchild))
leaves++;
leaf(t->rchild);
}
return leaves;
}
void main()
{
btnode *s,*bt;
int nodenum,leafnum;
//clrscr();
s=NULL;
printf("please input node:\n");
bt=create(s);
nodenum=nodes(bt);
printf("nodenum=%d\n",nodenum);
leafnum=leaf(bt);
printf("leafnum=%d\n",leafnum);
}
如何在上面的程序中实现先序遍历,中序遍历,后序遍历??
在上面程序中插入void preorder(btnode *p)
{if (p!=NULL)
{printf("%c",p->data);
preorder(p->lchild);
preorder(p->rchild);
}
return btnode;
}
void inorder(btnode *P)
{
if(p!=NULL)
{inorder(p->lchild);
printf("%c",p->data);
inorder(p->rchild);
}
return btnode;
}
void postorder(btnode *p)
{
if(p!=NULL)
{postorder(p->lchild);
postorder(p->rchild);
printf("%c",p->data);
}
return btnode;
}后在主函数中该如何编写啊??麻烦各位帮帮忙!!谢谢··
#include<malloc.h>
#define NULL 0
typedef struct btnode
{
char data;
struct btnode *lchild,*rchild;
}btnode;
btnode *t;
int leaves=0;
btnode *create (btnode *p)
{ char ch;
if ((ch=getchar())=='#')
p=NULL;
else
{
p=(btnode *)malloc(sizeof(btnode));
p->data=ch;
p->lchild=create(p->lchild);
p->rchild=create(p->rchild);
}
return p;
}
int nodes(btnode *p)
{
int node=0;
if(p==NULL)
node=0;
else
node=1+nodes(p->lchild)+nodes(p->rchild);
return(node);
}
int leaf(btnode *t)
{
if(t)
{
leaf(t->lchild);
if(!(t->lchild||t->rchild))
leaves++;
leaf(t->rchild);
}
return leaves;
}
void main()
{
btnode *s,*bt;
int nodenum,leafnum;
//clrscr();
s=NULL;
printf("please input node:\n");
bt=create(s);
nodenum=nodes(bt);
printf("nodenum=%d\n",nodenum);
leafnum=leaf(bt);
printf("leafnum=%d\n",leafnum);
}
如何在上面的程序中实现先序遍历,中序遍历,后序遍历??
在上面程序中插入void preorder(btnode *p)
{if (p!=NULL)
{printf("%c",p->data);
preorder(p->lchild);
preorder(p->rchild);
}
return btnode;
}
void inorder(btnode *P)
{
if(p!=NULL)
{inorder(p->lchild);
printf("%c",p->data);
inorder(p->rchild);
}
return btnode;
}
void postorder(btnode *p)
{
if(p!=NULL)
{postorder(p->lchild);
postorder(p->rchild);
printf("%c",p->data);
}
return btnode;
}后在主函数中该如何编写啊??麻烦各位帮帮忙!!谢谢··