1、以二叉链表作存储结构,设计求二叉树高度的算法。
2、一棵 n 个结点的完全二叉树用向量作存储结构,用非递归算法实现对该
二叉树进行前序遍历。
3、以二叉链表作存储结构,编写非递归的前序、中序、后序遍历算法。
#include <iostream>
#include <string>
#define MAXSIZE 100
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef int SqBiTree[MAXSIZE];
typedef struct StackNode
{
char datac;
struct StackNode *next;
}StackNode,*LinkStack;
typedef struct StackNode1
{
BiTree data;
struct StackNode1 *next;
}StackNode1,*LinkStack1;
void InitTree(BiTree &T)
{
T=new BiTNode;
T=NULL;
}
void InitStack(LinkStack &S)//
{
S=new StackNode;
S=NULL;
}
void InitStack1(LinkStack1 &S)//
{
S=new StackNode1;
S=NULL;
}
void Build(int *bt,int n)
{
int x;
cout<<"输入该完全二叉树:";
for(int i=1;i<=n;i++)
{
cin>>x;
bt[i]=x;
//cout<<bt[i];
}
}
void CreateTree(BiTree &T)
{
char x;
cin>>x;
if('#'==x)
T=NULL;
else
{
T=new BiTNode;
T->data=x;
CreateTree(T->lchild);
CreateTree(T->rchild);
}
}
void Push(LinkStack &S,char e)
{
LinkStack p=new StackNode;
p->datac=e;
p->next=S;
S=p;
}
void Push1(LinkStack1 &S,BiTree e)
{
LinkStack1 p=new StackNode1;
p->data=e;
p->next=S;
S=p;
}
int Pop(LinkStack &S)
{
LinkStack p=new StackNode;
if(S==NULL)
cout<<"栈为空";
p=S;
S=S->next;
delete p;
}
void Pop1(LinkStack1 &S,BiTree &e)
{
LinkStack1 p=new StackNode1;
if(S==NULL)
{
cout<<"栈为空";
return;
}
p=S;
e=p->data;
S=S->next;
//return p->data;
delete p;
}
int StackSize(LinkStack1 S)
{
int i=0;
while(S)
{
i++;
S=S->next;
}
return i;
}
int GetTop(LinkStack &S)
{
if(S!=NULL)
return S->datac;
}
BiTree GetTop1(LinkStack1 &S)
{
if(S!=NULL)
return S->data;
}
int EmptyStack(LinkStack &S)
{
if(S==NULL)
return 1;
else
return 0;
}
int EmptyStack1(LinkStack1 &S)
{
if(S==NULL)
return 1;
else
return 0;
}
int Deepth(BiTree &T)
{
int deep1,deep2;
if(T==NULL)
return 0;
else
{
deep1=Deepth(T->lchild);
deep2=Deepth(T->rchild);
if(deep1>deep2)
return (deep1+1);
else
return (deep2+1);
}
}
void Order1(BiTree &T)
{
LinkStack1 S;
BiTNode *p=T;
InitStack1(S);
//p=new BiTNode;
//p=T;
while(p||!EmptyStack1(S))
{
while(p)
{
cout<<p->data<<" ";
Push1(S,p);
p=p->lchild;
}
if(!EmptyStack1(S))
{
Pop1(S,p);
p=p->rchild;
}
}
}
void Order2(BiTree &T)
{
LinkStack1 S;
BiTNode *p=T;
InitStack1(S);
//p=new BiTNode;
//p=T;
//栈不空或者p不空时循环
while(p||!EmptyStack1(S)){
while(p)
{
//存入栈中
Push1(S,p);
//遍历左子树
p=p->lchild;
}
if(!EmptyStack1(S)){
//退栈,访问根节点
Pop1(S,p);
cout<<p->data<<" ";
//Pop(S);
//访问右子树
p=p->rchild;
}
}//while
}
void Order3(BiTree &T)
{
int flag[20];
LinkStack1 S;
BiTNode *p;
InitStack1(S);
p=new BiTNode;
p=T;
if(!p)
{
cout<<"空树!\n";
}
while(p||!EmptyStack1(S))
{
while(p)
{
Push1(S,p);
flag[StackSize(S)]=0;
p=p->lchild;
}
while(!EmptyStack1(S)){
p=GetTop1(S);
if(p->rchild&&flag[StackSize(S)]==0)
{
flag[StackSize(S)]=1;
p=p->rchild;
break;
}
else{
p=GetTop1(S);
cout<<p->data<<" ";
Pop1(S,p);
if(EmptyStack1(S)){
return ;
}
}
}
}
}
void PreOrder(int *bt,int n)
{
LinkStack S;
InitStack(S);
int i=1;
while(i<=n||!EmptyStack(S))
{
while(i<=n)
{
cout<<bt[i]<<" ";
Push(S,bt[i]);
i=i*2;
}
if(!EmptyStack(S))
{
i=GetTop(S);
Pop(S);
i=2*i+1;
}
}
}
int main()
{
BiTree T;
LinkStack S;
LinkStack1 S1;
int i,t,n=0;
int *bt;
do{
cout<<"\n1.二叉树高度 2.二叉树非递归遍历 3.向量二叉树遍历\n";
cin>>t;
if(t==1)
{
InitTree(T);
InitStack(S);
cout<<"建立一个树,空结点用#表示:";
CreateTree(T);
cout<<"二叉树高度为:";
i=Deepth(T);
cout<<i;
}
if(t==2)
{
InitTree(T);
InitStack1(S1);
cout<<"建立一个树,空结点用#表示:";
CreateTree(T);
cout<<"二叉树前序遍历:";
Order1(T);
cout<<endl;
cout<<"二叉树中序遍历:";
Order2(T);
cout<<endl;
cout<<"二叉树后序遍历:";
Order3(T);
cout<<endl;
}
if(t==3)
{
InitTree(T);
cout<<"定义一个长度:";
cin>>n;
bt=new int[n+1];
Build(bt,n);
cout<<"输出原来的二叉树:";
for(int i=1;i<=n;i++)
cout<<bt[i]<<" ";
cout<<endl;
PreOrder(bt,n);
cout<<endl;
}
}while(t!=3);
}