分别用递归和非递归先序后序中序创建二叉树、层序创建二叉树、求二叉树的高度、输出叶子节点
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAXSIZE 100
typedef struct TNode{
char Data;
TNode * Left;
TNode * Right;
} *BinTree;
typedef struct SqQueue{
BinTree Data[MAXSIZE];
int front;
int rear;
}*Queue;
typedef struct SqStack{
BinTree Data[MAXSIZE];
int top;
}*Stack;
/*------------------------------------------------队列实现--------------------------------------*/
Queue CreatQueue(Queue Q){
Q=(Queue)malloc(sizeof(SqQueue));
Q->front =0;
Q->rear =0;
return Q;
}
int AddQueue(Queue Q,BinTree BT){
if((Q->rear+1)%MAXSIZE == Q->front)
return 0;
Q->Data[Q->rear] =BT;
Q->rear = (Q->rear +1)%MAXSIZE;
return 1;
}
BinTree DeQueue(Queue Q){
BinTree e;
if(Q->front == Q->rear)
return 0;
e=Q->Data [Q->front ];
Q->front = (Q->front+1)%MAXSIZE;
return e;
}
int QueueEmpty(Queue Q){
if(Q->front == Q->rear)
return 1;
return 0;
}
/*------------------------------------------------栈实现--------------------------------------*/
Stack CreatStack(Stack S){
S=(Stack)malloc(sizeof(SqStack));
S->top=-1;
return S;
}
int StackEmpty(Stack S){
if(S->top==-1)//空为1
return 1;
else
return 0;//不空为0
}
void Push(Stack S,BinTree e){
if(S->top ==MAXSIZE-1)
return ;
S->top ++;
S->Data [S->top ]=e;
}
BinTree Pop(Stack S){
BinTree e;
if(S->top==-1)
return 0;
else
e=S->Data[S->top];
S->top --;
return e;
}
/*------------------------------------------------创建实现--------------------------------------*/
void PreorderCreatBinTree(BinTree *BT){//先序-创建
char ch;
ch=getchar();
if(ch!='#'){
(*BT)=(BinTree)malloc(sizeof(TNode));
(*BT)->Data=ch;
PreorderCreatBinTree(&((*BT)->Left));
PreorderCreatBinTree(&((*BT)->Right));
}
else
*BT=NULL;
}
BinTree LevelCreatBinTree(BinTree root)//层序-创建
{
char ch;
Queue Q;
Q=CreatQueue(Q);
char a[100];
scanf("%s",a);
int len=strlen(a);
if (a[0] == '#') //如果第一个节点为空,就直接返回空树
return NULL;
else
{
root =(BinTree)malloc(sizeof(TNode));
root->Data = a[0];
AddQueue(Q, root); //根节点入队
}
int l=1;
while (!QueueEmpty(Q)) //当队列不为空
{
BinTree p= DeQueue(Q);
if(a[l]=='.')break;
if (a[l] == '#') //左孩子为空
p->Left = NULL;
else
{
p->Left =(BinTree)malloc(sizeof(TNode));
p->Left->Data = a[l];
AddQueue(Q, p->Left); //左孩子入队
}
l++;
if (a[l] == '#') //右孩子为空
p->Right = NULL;
else
{
p->Right = (BinTree)malloc(sizeof(TNode));
p->Right->Data = a[l];
AddQueue(Q, p->Right); //右孩子入队
}
}
return root;
}
BinTree PreInCreateBinTree(char A[],char B[],int N){//先序+中序创建
if(N==0)
return NULL;
char root=A[0]; //找到根节点
int r=-1;
for(int i=0;i<N;i++){
if(B[i]==root){
r=i;
break;
}
}
BinTree BT=(BinTree)malloc(sizeof(TNode));
BT->Data =root;
BT->Left=PreInCreateBinTree(A+1,B,r); //左子树
BT->Right=PreInCreateBinTree(A+r+1,B+r+1,N-r-1); //右子树
//printf("创建成功");
return BT;
}
/*------------------------------------------------简单应用--------------------------------------*/
void PreOrderPrintLeaves(BinTree BT){//输出叶子节点
if(BT){
if(!BT->Left &&!BT->Right)
printf("%C ",BT->Data );
PreOrderPrintLeaves(BT->Left );
PreOrderPrintLeaves(BT->Right );
}
}
int GetHeight(BinTree BT){//求二叉树高度
int HL,HR,MaxH;
if(BT){
HL=GetHeight(BT->Left );
HR=GetHeight(BT->Right );
MaxH=HL>HR?HL:HR;
return (MaxH+1);
}
else return 0;
}
/*------------------------------------------------递归遍历实现--------------------------------------*/
void recursion_InorderTraversal(BinTree BT){//递归-中序遍历
if(BT){
if(BT->Left)recursion_InorderTraversal(BT->Left);
printf("%c ",BT->Data);
if(BT->Right)recursion_InorderTraversal(BT->Right);
}
}
void recursion_PreorderTraversal( BinTree BT ){ //递归-先序遍历
if(BT){
printf("%c ",BT->Data);
if(BT->Left)recursion_PreorderTraversal(BT->Left);
if(BT->Right)recursion_PreorderTraversal(BT->Right);
}
}
void recursion_PostorderTraversal( BinTree BT ){//递归-后序遍历
if(BT){
if(BT->Left)recursion_PostorderTraversal(BT->Left);
if(BT->Right)recursion_PostorderTraversal(BT->Right);
printf("%c ",BT->Data);
}
}
/*------------------------------------------------非递归遍历实现--------------------------------------*/
void preOrderTraversal(BinTree BT)//非递归-先序遍历
{
BinTree T;
Stack S=CreatStack(S);
T=BT;
while(T||!StackEmpty(S)){
while(T){
printf("%c ",T->Data);
Push(S,T);
T=T->Left;
}
T=Pop(S);
T=T->Right;
}
}
void InOrderTraversal(BinTree BT)//非递归-中序遍历
{
BinTree T;
Stack S=CreatStack(S);
T=BT;
while(T||!StackEmpty(S)){
while(T){
Push(S,T);
T=T->Left;
}
T=Pop(S);
printf("%c ",T->Data);
T=T->Right;
}
}
void LevelOrderTraxersal(BinTree BT)//非递归-层序遍历
{
Queue Q;
BinTree T;
if(!BT) return;
Q=CreatQueue(Q);
AddQueue(Q,BT);
while(!QueueEmpty(Q))
{
T=DeQueue(Q);
printf("%c ",T->Data );
if(T->Left ) AddQueue(Q,T->Left);
if(T->Right) AddQueue(Q,T->Right );
}
}
/*------------------------------------------------主函数实现--------------------------------------*/
int main()// 测试数据:ABDH##I##E##CF#J##G## //测试数据:ABCDFGI##E##H###### //测试数据:ABCDEFG BDCFGEA
{
BinTree BT1,BT2,BT3;
char A[MAXSIZE],B[MAXSIZE];
printf("-----------------先序创建二叉树操作如下-----------------\n\n");
printf("输入测试数据:"); PreorderCreatBinTree(&BT1);
printf("递归先序遍历的输出:"); recursion_PreorderTraversal(BT1 ); printf("\n");
printf("递归中序遍历的输出:"); recursion_InorderTraversal(BT1); printf("\n");
printf("递归后序遍历的输出:"); recursion_PostorderTraversal( BT1 );printf("\n");
printf("非递归先序遍历输出:"); preOrderTraversal(BT1); printf("\n");
printf("非递归中序遍历输出:"); InOrderTraversal(BT1); printf("\n");
printf("层序遍历的输出:"); LevelOrderTraxersal(BT1); printf("\n");
printf("叶子节点的输出:"); PreOrderPrintLeaves(BT1); printf("\n");
printf("树的高度:%d",GetHeight(BT1)); printf("\n\n\n");
printf("-----------------层序创建二叉树操作如下-----------------\n\n");
printf("输入测试数据:"); BT2=LevelCreatBinTree(BT2);
printf("递归先序遍历的输出:"); recursion_PreorderTraversal(BT2 ); printf("\n");
printf("递归中序遍历的输出:"); recursion_InorderTraversal(BT2); printf("\n");
printf("递归后序遍历的输出:"); recursion_PostorderTraversal( BT2 );printf("\n");
printf("非递归先序遍历输出:"); preOrderTraversal(BT2); printf("\n");
printf("非递归中序遍历输出:"); InOrderTraversal(BT2); printf("\n");
printf("层序遍历的输出:"); LevelOrderTraxersal(BT2); printf("\n");
printf("叶子节点的输出:"); PreOrderPrintLeaves(BT2); printf("\n");
printf("树的高度:%d",GetHeight(BT2)); printf("\n\n\n");
printf("-----------------先序+中序创建二叉树操作如下-----------------\n\n");
printf("输入先序节点数据:"); scanf("%s",A); int N=strlen(A);
printf("输入中序节点数据:"); scanf("%s",B);BT3=PreInCreateBinTree(A,B,N);
printf("递归先序遍历的输出:"); recursion_PreorderTraversal(BT3 ); printf("\n");
printf("递归中序遍历的输出:"); recursion_InorderTraversal(BT3); printf("\n");
printf("递归后序遍历的输出:"); recursion_PostorderTraversal( BT3 );printf("\n");
printf("非递归先序遍历输出:"); preOrderTraversal(BT3); printf("\n");
printf("非递归中序遍历输出:"); InOrderTraversal(BT3); printf("\n");
printf("层序遍历的输出:"); LevelOrderTraxersal(BT3); printf("\n");
printf("叶子节点的输出:"); PreOrderPrintLeaves(BT3); printf("\n");
printf("树的高度:%d",GetHeight(BT3)); printf("\n\n\n");
return 0;
}