#include <bits/stdc++.h>
#define MaxSize 100
/*
* Created by HarvestWu on 2018/06/19.
*/
using namespace std;
typedef char ElemType;
//定义二叉树结构
typedef struct BTNode
{
ElemType data;
struct BTNode *lchild;
struct BTNode *rchild;
} BTNode;
//定义存储结点指针及层号结构
typedef struct St
{
BTNode *p;
int lno;
} St;
//由str串创建二树
void CreateBTNode (BTNode *&b,ElemType *str)
{
BTNode *St[MaxSize],*p= NULL;
int top=-1, k = 0,j=0;
ElemType ch;
b= NULL;
ch=str[j];
while (ch!='\0')
{
switch (ch)
{
case '(':
top++;
St[top]=p;
k=1;
break;
case ')':
top--;
break;
case ',':
k=2;
break;
default:
p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=p->rchild= NULL;
if (b== NULL)
b=p;
else
{
switch(k)
{
case 1:
St[top]->lchild=p;
break;
case 2:
St[top]->rchild=p;
break;
default:
break;
}
}
}
j++;
ch=str[j];
}
}
//递归先序遍历
void preOrder(BTNode *p)
{
if (p!= NULL)
{
cout<<p->data<<" ";
preOrder(p->lchild);
preOrder(p->rchild);
}
}
//非递归先序遍历
void preOrderNonRecursion(BTNode *p)
{
if (p!= NULL)
{
BTNode *Stack[MaxSize];
int top=-1;
BTNode *q;
Stack[++top]=p;
while(top!=-1)
{
q=Stack[top--];
cout<<q->data<<" ";
if(q->rchild!=NULL)
Stack[++top]=q->rchild;
if(q->lchild!=NULL)
Stack[++top]=q->lchild;
}
}
}
//递归中序遍历
void inOrder(BTNode *p)
{
if (p!= NULL)
{
inOrder(p->lchild);
cout<<p->data<<" ";
inOrder(p->rchild);
}
}
//非递归中序遍历
void inOrderNonRecursion(BTNode *p)
{
if(p!=NULL)
{
BTNode *Stack[MaxSize];
int top=-1;
BTNode *q;
q=p;
while(top!=-1||q!=NULL)
{
while(q!=NULL)
{
Stack[++top]=q;
q=q->lchild;
}
if(top!=-1)
{
q=Stack[top--];
cout<<q->data<<" ";
q=q->rchild;
}
}
}
}
//递归后序遍历
void postOrder(BTNode *p)
{
if (p!= NULL)
{
postOrder(p->lchild);
postOrder(p->rchild);
cout<<p->data<<" ";
}
}
//非递归后序遍历
void postOrderNonRecursion(BTNode *p)
{
if (p!= NULL)
{
BTNode *Stack1[MaxSize],*Stack2[MaxSize];
int top1=-1,top2=-1;
BTNode *q;
Stack1[++top1]=p;
while(top1!=-1)
{
q=Stack1[top1--];
Stack2[++top2]=q;
if(q->lchild!=NULL)
Stack1[++top1]=q->lchild;
if(q->rchild!=NULL)
Stack1[++top1]=q->rchild;
}
while(top2!=-1)
{
q=Stack2[top2--];
cout<<q->data<<" ";
}
}
}
//求二叉树深度
int getDepth(BTNode *p)
{
int LD,RD;
if(p==NULL)
return 0;
else
{
LD=getDepth(p->lchild);
RD=getDepth(p->rchild);
return (LD>RD?LD:RD)+1;
}
}
//寻找二叉树中是否存在某值节点
void searchBTNode(BTNode *p,BTNode *&q,int key)
{
if(p!=NULL)
{
if(p->data==key)
q=p;
else
{
searchBTNode(p->lchild,q,key);
searchBTNode(p->rchild,q,key);
}
}
}
//层序遍历
void level(BTNode *p)
{
int front,rear;
BTNode *que[MaxSize];
front=rear=0;
BTNode *q;
if (p!=NULL)
{
rear=(rear+1)%MaxSize;
que[rear]=p;
while (front!=rear)
{
front=(front+1)%MaxSize;
q=que[front];
cout<<q->data<<" ";
if (q->lchild!=NULL)
{
rear=(rear+1)%MaxSize;
que[rear]=q->lchild;
}
if(q->rchild!=NULL)
{
rear=(rear+1)%MaxSize;
que[rear]=q->rchild;
}
}
}
}
//求二叉树宽度
int maxNode(BTNode *b)
{
St que[MaxSize];
int front,rear;
int Lno,i,j,n,max;
front=rear=0;
BTNode *q;
if (b!=NULL)
{
++rear;
que[rear].p=b;
que[rear].lno=1;
while (front!=rear)
{
++front;
q=que[front].p;
Lno=que[front].lno;
if(q->lchild!=NULL)
{
++rear;
que[rear].p=q->lchild;
que[rear].lno=Lno+1;
}
if(q->rchild!=NULL)
{
++rear;
que[rear].p=q->rchild;
que[rear].lno=Lno+1;
}
}
max=0;
for(i=1; i<=Lno; ++i)
{
n=0;
for(j=1; j<rear; ++j)
{
if(que[j].lno==i)
++n;
}
if(max<n)
max=n;
}
return max;
}
else return 0;
}
int main()
{
BTNode *b,*p,*lp,*rp;;
CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
cout<<"递归先序遍历:"<<endl;
preOrder(b);
cout<<"\n非递归先序遍历:"<<endl;
preOrderNonRecursion(b);
cout<<"\n递归中序遍历:"<<endl;
inOrder(b);
cout<<"\n非递归中序遍历:"<<endl;
inOrderNonRecursion(b);
cout<<"\n递归后序遍历:"<<endl;
postOrder(b);
cout<<"\n非递归后序遍历:"<<endl;
postOrderNonRecursion(b);
cout<<"\n二叉树深度:"<<endl;
cout<<getDepth(b);
cout<<"\n寻找二叉树中是否存在某值节点:"<<endl;
searchBTNode(b,p,'A');
cout<<p->data;
cout<<"\n层序遍历:"<<endl;
level(b);
cout<<"\n二叉树宽度:"<<endl;
cout<<maxNode(b)<<endl;
return 0;
}
C++ 二叉树的遍历
最新推荐文章于 2021-09-09 22:46:51 发布