赞
踩
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _bitree
{
char data;
struct _bitree *lchild,*rchild;
}*BiTree,BiTNode;
BiTree bitree_create(BiTree tree);
void prev_display(BiTree tree);
void midl_display(BiTree tree);
void last_display(BiTree tree);
//建立二叉树
BiTree bitree_create(BiTree tree)
{
char ch = getchar();
if(ch == '#')
tree = NULL;
else
{
tree = (BiTNode *)malloc(sizeof(BiTNode));
if(tree == NULL )
{
printf("creat error!\n");
return;
}
tree->data = ch;
tree->lchild = bitree_create(tree->lchild);
tree->rchild = bitree_create(tree->rchild);
}
return tree;
}
//先序遍历
//中,左,右
void prev_display(BiTree tree)
{
if(tree != NULL)
{
printf("%c ",tree->data);
prev_display(tree->lchild);
prev_display(tree->rchild);
}
}
//中序遍历
//左,中,右
void midl_display(BiTree tree)
{
if(tree != NULL)
{
prev_display(tree->lchild);
printf("%c ",tree->data);
prev_display(tree->rchild);
}
}
//后序遍历
//左,右,中
void last_display(BiTree tree)
{
if(tree != NULL)
{
prev_display(tree->lchild);
prev_display(tree->rchild);
printf("%c ",tree->data);
}
}
//求深度
int main()
{
BiTree tree = bitree_create(tree);
prev_display(tree);
printf("\n");
midl_display(tree);
printf("\n");
last_display(tree);
printf("\n");
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。