二叉树遍历,递归与非递归,前序中序后序遍历,C代码

来源:百度知道 编辑:UC知道 时间:2024/05/17 22:15:46
谁有二叉树遍历,递归与非递归,前序中序后序遍历,C代码
如果适合我们的教材 给你满分~~~~

#include<stdio.h>
#include<stdlib.h>
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;//二叉树节点类型和节点指针类型

bitree create()//先序创建
{
bitree root=NULL;
char c;
scanf("%c",&c);
fflush(stdin);
if(c=='#')return NULL;
else
{
root=(bitnode*)malloc(sizeof(bitnode));
root->data=c;
root->lchild=create();
root->rchild=create();
}
return root;
}

void preorder(bitree root)//先根遍历
{
if(!root)return;
else
{
putchar(root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}

void inorder(bitree root)//中根遍历
{
if(!root)return;
else
{
inorder(root->lchild);
putchar(root->data);
inorder(root->rchild);
}
}

void postorder(bitree root)//后