二叉树的建立和操作

[复制链接]
查看11 | 回复0 | 2007-12-15 03:51:14 | 显示全部楼层 |阅读模式
分好少为了能在叶子节点返回,我们得多添加叶子节点,使所有的叶子节点都为NULL,他们的直为'?'. 运行该程序,输入ABD??EG???CFH??I?J???+回车 #include #include #define NULLKEY '?'typedef struct btnode{ char data; struct btnode *lchild,*rchild;}btnode,*bitree;bitree preCreateBitree(bitree &root){ char ch; scanf("%c",&ch); if(ch==NULLKEY) {root=NULL;return(root); } else {root=(bitree)malloc(sizeof(btnode));root->data=ch;preCreateBitree(root->lchild);preCreateBitree(root->rchild);return(root); }}void fsearch(bitree root){ if(root==NULL)return ; else {
printf("%c",root->data);//先序遍历每个结点的值
fsearch(root->lchild);fsearch(root->rchild); }}void msearch(bitree root){ if(root==NULL)return ; else {
msearch(root->lchild);printf("%c",root->data);//中序遍历每个结点的值msearch(root->rchild); }}void lsearch(bitree root){ if(root==NULL)return ; else {
printf("%c",root->data);//后序遍历每个结点的值lsearch(root->rchild);
lsearch(root->lchild); }}void main(){ bitree root; root=preCreateBitree(root);
printf("first:"); fsearch(root); printf("\n"); printf("middle:"); msearch(root); printf("\n");
printf("last:"); msearch(root); printf("\n");
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行