#include<iostream>
using namespace std;
typedef char DataType;
/******结点类型**************/
typedef struct BitNode
{
DataType data;
struct BitNode *lchild,*rchild;
}BitNode,*BitTree;
BitTree BinTreeInit(BitTree BT);
BitTree BinTreeCreat(BitTree BT);
BitTree BinTreeEmpty(BitTree BT);
BitTree PreOrderTraverse(BitTree BT);
BitTree InOrderTraverse(BitTree BT);
BitTree PostOrderTraverse(BitTree BT);
BitTree BinTreeClear(BitTree BT);
BitNode *T=NULL;
int main()
{
system(“cls”);
BinTreeInit(T);
cout<<endl;
BinTreeCreat(T);
cout<<endl;
cout<<”是否为空”<<endl;
BinTreeEmpty(T);
cout<<endl;
cout<<”前序”<<endl;
PreOrderTraverse(T);
cout<<endl;
cout<<”中序”<<endl;
InOrderTraverse(T);
cout<<endl;
cout<<”后序”<<endl;
PostOrderTraverse(T);
cout<<endl;
cout<<”删除”<<endl;
BinTreeClear(T);
cout<<endl;
return 0;
}
/**********二叉树初始化*****************/
BitTree BinTreeInit(BitTree BT)
{
BT=NULL;
return BT;
}
/***********建立一个二叉树**************/
BitTree BinTreeCreat(BitTree BT)
{
char ch;
cout<<”输入数据:”<<endl;
cin>>ch;
if(ch==’#') BT=NULL;
else
{
BT=(BitNode *)malloc(sizeof(BitNode));
BT->data=ch;
BT->lchild=BinTreeCreat(BT->lchild);
BT->rchild=BinTreeCreat(BT->rchild);
}
return BT;
}
/*************检查二叉树是否为空****************/
BitTree BinTreeEmpty(BitTree BT)
{
if(BT==NULL)
{
return NULL;
}
else
{
return 0;
}
return BT;
}
/*************按任意一种次序输出二叉树********/
/***************前序遍历*****************/
BitTree PreOrderTraverse(BitTree BT)
{
if(BT)
{
cout<<BT->data;
if(PreOrderTraverse(BT->lchild))
{
if(PreOrderTraverse(BT->rchild))
{
return 0;
}
}
}
return BT;
}
/*********中序遍历**********/
BitTree InOrderTraverse(BitTree BT)
{
if(BT)
{
if(InOrderTraverse(BT->lchild))
{
cout<<BT->data;
if(InOrderTraverse(BT->rchild))
{
return 0;
}
}
}
return BT;
}
/****************后序遍历*****************/
BitTree PostOrderTraverse(BitTree BT)
{
if(BT)
{
if(InOrderTraverse(BT->lchild))
{
if(InOrderTraverse(BT->rchild))
{
return 0;
}
cout<<BT->data;
}
}
return BT;
}
/***************删除二叉树*****************/
BitTree BinTreeClear(BitTree BT)
{
if(BT)
{
if(BinTreeClear(BT->lchild))
{
if(BinTreeClear(BT->rchild))
{
cout<<”Deleting…”<<BT->data;
free((void*)BT);
return 0;
}
}
}
else
{
return 0;
}
return BT;
}
>> 本文固定链接: http://www.vcgood.com/archives/2349