二叉树

#include "stdio.h"
#include "stdlib.h"

#define OVERFLOW -1
#define ERROR -1
#define OK 1

typedef char Elemtype;
typedef int Status;
typedef struct BitNode
{
	Elemtype data;
	struct BitNode *lchild,*rchild;
}bitnode ,*Bitree;



Status creatbitree(Bitree &t)
{
	char ch;
	scanf("%c",&ch);
	if(ch==' ') t=NULL;
	else
	{
		if(!(t=(bitnode *)malloc(sizeof(Bitree))))  exit(OVERFLOW);
		t->data=ch;
		creatbitree(t->lchild);
		creatbitree(t->rchild);
	}
	return OK;
}

Status printelemt(Elemtype e)
{
	printf("%c",e);
	return OK;
}

Status preordertraverse(Bitree t,Status (*visit)(Elemtype e))
{
	if(t)
	{
		if(visit(t->data))
			if(preordertraverse(t->lchild,visit))
					if(preordertraverse(t->rchild,visit))  return OK;
		return ERROR;
	}
	else 
		return OK;
}

Status inordertraverse(Bitree t,Status (*visit)(Elemtype e))
{
	if(t)
	{
	    if(inordertraverse(t->lchild,visit))
		    if(visit(t->data))
				if(inordertraverse(t->rchild,visit))  return OK;
		return ERROR;
	}
	else 
		return OK;
}

Status postordertraverse(Bitree t,Status (*visit)(Elemtype e))
{
	if(t)
	{
	    if(postordertraverse(t->lchild,visit))
			if(postordertraverse(t->rchild,visit))  	
					if(visit(t->data)) return OK;
		return ERROR;
	}
	else 
		return OK;
}

main()
{
	Bitree t;
	creatbitree(t);
	preordertraverse(t,printelemt);
	printf("\n");

	inordertraverse(t,printelemt);
	printf("\n");

	postordertraverse(t,printelemt);
	printf("\n");

}






 

posted @ 2013-11-26 20:30  博园少主  阅读(208)  评论(0编辑  收藏  举报