二叉树的三种建立以及三种遍历递归与非递归

直接上代码话不多说

#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <stdlib.h>
using namespace std;
typedef int ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;
int N;
int preorder[31];
int postorder[31];
int inorder[31];
int layoder[31];
void createByInAndPost(BiTree &tree,int pleft,int pright,int ileft,int iright)
{
    if(pright>=pleft)
    {
        int i=0;
        tree=new BiTNode;
        tree->data=postorder[pright];
        for(i=ileft; i<=iright; i++) //在inorder中寻找根 划分左右子树从而确定p和i两数组的界限
            if(inorder[i]==postorder[pright])
                break;
        createByInAndPost(tree->lchild,pleft,pleft+i-ileft-1,ileft,i-1);
        createByInAndPost(tree->rchild,pleft+i-ileft,pright-1,i+1,iright);
        // the difficult point is the division of the array
        // 删掉postorder的最后一个 删掉inorder的中间一个
    }
    else
    {
        tree=NULL;
    }
}
void createByInAndPre(BiTree &tree,int pleft,int pright,int ileft,int iright)
{
    if(pright>=pleft)
    {
        int i=0;
        tree=new BiTNode;
        tree->data=preorder[pleft];
        for(i=ileft; i<=iright; i++) //在inorder中寻找根 划分左右子树从而确定p和i两数组的界限
            if(inorder[i]==preorder[pleft])
                break;
        createByInAndPre(tree->lchild,pleft+1,pleft+i-ileft,ileft,i-1);
        createByInAndPre(tree->rchild,pleft+i-ileft+1,pright,i+1,iright);
        // the difficult point is the division of the array
        // 删掉pretorder的第一个 删掉inorder的中间一个
    }
    else
    {
        tree=NULL;
    }

}
void createByLayAndIn(BiTree &tree,int lleft,int lright,int ileft,int iright){
    if(ileft<=iright){

		int i = lleft, j = ileft;//分别指向level和in中数组的元素
		int flag = 0;

		//寻找根结点,若level中第一个与in中元素匹配的即为根结点
		for (i = lleft; i <= lright; ++i)
		{
			for (j = ileft; j <= iright; ++j)
			{
				if (layoder[i] == inorder[j])
				{
					flag = 1;
					break;
				}
			}

			if (flag == 1)
				break;
		}
        tree = new BiTNode;
		tree->data = layoder[i];
        createByLayAndIn(tree->lchild,lleft+1,lright,ileft,j-1);
        createByLayAndIn(tree->rchild,lleft+1,lright,j+1,iright);

    }else{
        tree=NULL;
    }
}
void InOrder(BiTNode *bitree)
{
    if(!bitree)
        return;
    InOrder(bitree->lchild);
    cout<<bitree->data<<" ";
    InOrder(bitree->rchild);
}
void InOrderNotRe(BiTree T)
{
    stack<BiTree> S;
    BiTree p = T;
    while(p||!S.empty())
    {
        if(p)
        {
            S.push(p);
            p=p->lchild;
        }
        else
        {
            p = S.top();
            S.pop();
            cout<<p->data<<" ";
            p=p->rchild;
        }
    }
    cout<<endl;
}
void PreOrder(BiTNode *bitree)
{
    if(!bitree)
        return;
    cout<<bitree->data<<" ";
    PreOrder(bitree->lchild);
    PreOrder(bitree->rchild);
}
void PreOrderNotRe(BiTree T){
    stack<BiTree> S;
    BiTree p = T;
    while(p||!S.empty()){
        if(p){
            cout<<p->data<<" ";
            S.push(p);
            p = p->lchild;
        }
        else{
            p = S.top();
            S.pop();
            p = p->rchild;
        }
    }
    cout<<endl;
}
void PostOrder(BiTNode *bitree)
{
    if(!bitree)
        return;
    PostOrder(bitree->lchild);
    PostOrder(bitree->rchild);
    cout<<bitree->data<<" ";
}
void PostOrderNotRe(BiTree T){
    stack<BiTree> S;
    BiTree p = T, r = NULL;
    while(p||!S.empty()){
        if(p){
            S.push(p);
            p=p->lchild;
        }
        else{
            p = S.top();
            if(p->rchild&&p->rchild!=r){
                p = p->rchild;
                S.push(p);
                p = p->lchild;
            }
            else{
                S.pop();
                cout<<p->data<<" ";
                r = p;
                p = NULL;
            }
        }
    }
    cout<<endl;
}
void LayerOrder(BiTNode *bitree) //输出层次感
{
    queue<BiTree> Q;
    BiTree p;
    Q.push(bitree);
    BiTNode *temp = new BiTNode;
    Q.push(temp);
    while(!Q.empty())
    {
        p = Q.front();
        Q.pop();
        if(p==temp)
        {
            cout<<endl;
            if(!Q.empty())
                Q.push(temp);
            continue;
        }
        cout<<p->data<<" ";
        if(p->lchild)
            Q.push(p->lchild);
        if(p->rchild)
            Q.push(p->rchild);
    }

}
int main()
{
    cin>>N;
    for(int i=1; i<=N; i++)
        cin>>preorder[i];
    for(int i=1; i<=N; i++)
        cin>>postorder[i];
    for(int i=1; i<=N; i++)
        cin>>inorder[i];
    for(int i=1; i<=N; i++)
        cin>>layoder[i];
    BiTree bitree;
    createByLayAndIn(bitree,1,N,1,N);
    cout<<"先序遍历:";
    PreOrderNotRe(bitree);
    cout<<endl;
    cout<<"中序遍历:";
    InOrder(bitree);
    cout<<endl;
    cout<<"后序遍历:";
    PostOrderNotRe(bitree);
    cout<<endl;
    cout<<"中序遍历:";
    InOrderNotRe(bitree);
    cout<<"层次遍历:"<<endl;
    LayerOrder(bitree);
    return 0;
}
/* input
6
1 2 3 4 5 6
3 4 2 6 5 1
3 2 4 1 6 5
1 2 5 3 4 6
*/
//试给出实现哪些功能,并分析一下样例,最好画出树

上一篇:4.3.2线索二叉树


下一篇:【自考】数据结构导论-第4章二叉树代码