#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char data;
struct node *lc,*rc;
}node,*Btree;
typedef struct{
Btree *data;
int top;
}Stack;
typedef struct {
Btree *data;
int front;
int rear;
}Queue;
void InitQueue(Queue &Q)
{
Q.front=0;
Q.rear=0;
}
int EmptyQ (Queue &Q)
{
if (Q.rear-Q.front==1)
return 1;
return 0;
}
Btree DeQueue (Queue &Q,Btree &x)
{
x=Q.data[Q.front];
Q.front=Q.front+1;
return x;
}
void EnQueue(Queue &Q,Btree &x)
{
Q.data[Q.rear]=x;
Q.rear=Q.rear+1;
}
Btree getelem (Queue &Q,Btree &p)
{
p=Q.data[Q.front];
return p;
}
void InitStack(Stack &S)
{
S.top=-1;
}
void Push (Stack &S,Btree &T)
{
S.top++;
S.data[S.top]=T;
}
void Pop(Stack &S,Btree &T)
{
T=S.data[S.top];
S.top--;
}
int gettop (Stack &S,Btree &p)
{
p=S.data[S.top];
if (p)
return 1;
return 0;
}
int Empty (Stack &S)
{
if (S.top==0)
return 1;
return 0;
}
void CreatBtree (Btree &T)
{
char ch;
ch=getchar();
if (ch=='#')
T=NULL;
else
{
T=(node*)malloc(sizeof(node));
T->data=ch;
CreatBtree(T->lc);
CreatBtree(T->rc);
}
}
void CreatBtree2(Btree &T,char pre[],char in[],int n)
{///由先序序列和中序序列建立二叉树
int i,j;
int ln,rn;
if (n<1)
{
T=NULL;
return;
}
T=(node*)malloc(sizeof (node));
T->data=pre[0];
i=0;
while (in[i]!=pre[0])
i++;
ln=i;
rn=n-i-1;
for (j=0;j<i;j++)
{
pre[j]=pre[j+1];
in[j]=in[j];
}
for (j=i+1;j<n;j++)
{
pre[j-i-1]=pre[j];
in[j-i-1]=in[j];
}
if (ln<1)
T->lc=NULL;
else
CreatBtree2(T->lc,pre,in,ln);
if (rn<1)
T->rc=NULL;
else
CreatBtree2(T->rc,pre,in,rn);
}
void PreorderTravel(Btree &T)
{///先序递归遍历
if (T)
{
printf ("%c ",T->data);
PreorderTravel(T->lc);
PreorderTravel(T->rc);
}
}
void InorderTravel (Btree &T)
{///中序递归遍历
if (T)
{
InorderTravel(T->lc);
printf ("%c ",T->data);
InorderTravel(T->rc);
}
}
void BackTravel(Btree &T)
{///后序递归遍历
if (T)
{
BackTravel(T->lc);
BackTravel(T->rc);
printf ("%c ",T->data);
}
}
void PreorderTravel1(Btree &T)
{///先序非递归遍历
Stack S;
Btree p;
if (T)
{
InitStack(S);Push (S,T);
while (!Empty(S))
{
while (gettop(S,p))
{
printf ("%c ",p->data);
Push (S,p->lc);
}
Pop(S,p);
if (Empty(S))
{
Pop(S,p);
Push (S,p->rc);
}
}
}
}
void InitStack2(Btree &T )
{///中序非递归遍历
Stack S;
Btree p=T;
if (T)
{
InitStack(S);Push (S,T);
while (!Empty(S))
{
while (gettop(S,p))
{
Push (S,p);
p=p->lc;
}
if (!Empty(S))
{
Pop(S,p);
printf("%c ",p->data);
p=p->rc;
}
}
}
}
void BackorderTravel1(Btree &T)
{ ///后序非递归遍历
Stack S;
Btree p,q;
if (T)
{
InitStack(S); Push (S,T);
while (!Empty(S))
{
while (gettop(S,p) && p)
{
Push (S,p);
}
Pop(S,p);
if (!Empty(S))
{
gettop(S,p);
if (!p->rc)
{
Push(S,p->rc);
}
else
{
Pop(S,p);
printf ("%c ",p->data);
while (!Empty(S) && !gettop(S,q) && q->rc==p);
Pop(S,q);
printf ("%c ",q->data);
if (!Empty(S))
{
gettop(S,p);
Push (S,p);
}
}
}
}
}
}
void GrateTravel (Btree &T)
{///层次遍历
Queue Q;
Btree p;
if (T)
{
InitQueue(Q);
EnQueue(Q,T);
while (!EmptyQ(Q))
{
getelem(Q,p);
printf ("%c ",p->data);
if (p->lc)
EnQueue(Q,p->lc);
if (p->rc)
EnQueue(Q,p->rc);
}
}
}
int countleaftree(Btree &T)
{///计算叶子数
int num=0;
if (T)
{
if (T->lc==NULL && T->rc==NULL)
num++;
countleaftree(T->lc);
countleaftree(T->rc);
}
return num;
}
int counttree(Btree &T)
{///计算结点数
int num=0;
if (T)
{
num++;
counttree(T->lc);
counttree(T->rc);
}
return num;
}
int main ()
{
Btree T1,T2;
char pre[20],in[20];
int i,j;
int n;
printf ("输入二叉树元素:\n");
CreatBtree(T1);
printf ("输入结点个数\n");
scanf("%d",&n);
printf ("输入二叉树先序序列:\n");
for (j=0;j<n;j++)
scanf("%c",&pre[j]);
printf ("输入二叉树中序序序列:\n");
for (j=0;j<n;j++)
scanf("%c",&in[j]);
CreatBtree2(T2,pre,in,strlen(pre));
printf("选择遍历方式:\n");
printf ("1,先序递归遍历序列\n2,中序递归遍历序列\n3,后序递归遍历序列\n");
printf ("4,先序非递归遍历序列\n5,中序非递归遍历序列\n6,后序非递归遍历序列\n");
printf ("7,层次遍历\n");
scanf ("%d",&i);
switch(i)
{
case 1:{printf ("先序递归遍历序列\n");
PreorderTravel(T1);
printf ("\n");
}
case 2:
{
printf ("中序递归遍历序列\n");
InorderTravel(T1);
printf ("\n");
}
case 3:
{
printf ("后序递归遍历序列\n");
BackTravel(T1);
printf ("\n");
}
case 4:
{
printf ("先序非递归遍历序列\n");
PreorderTravel1(T1);
printf ("\n");
}
case 5:
{
printf ("中序非递归遍历序列\n");
InorderTravel(T1);
printf ("\n");
}
case 6:
{
printf ("后序非递归遍历序列\n");
BackorderTravel1(T1);
printf ("\n");
}
case 7:
{
printf ("层次遍历序列\n");
GrateTravel(T1);
printf ("\n");
}
printf("叶子数\n");
printf("%d\n",countleaftree(T1));
}
return 0;
}
二叉树基本操作
最新推荐文章于 2020-03-08 00:37:41 发布