二叉樹遞歸遍歷(先,中,後),非遞歸遍歷(先,中,後) (數據結構做業)


數據結構實驗:ios


(1)採用下列方法之一創建二叉樹的二叉鏈表:c++

① 輸入徹底二叉樹的先序序列,用#表明虛結點(空指針),如ABD###CE##F##,創建二叉樹的二叉鏈表。算法

② 已知二叉樹的先序遍歷序列和中序遍歷序列,或者已知二叉樹的中序遍歷序列和後序遍歷序列,創建二叉樹的二叉鏈表。數組

③ 將一棵二叉樹的全部結點存儲在一維數組中,虛結點用#表示,利用二叉樹的性質5,創建二叉樹的二叉鏈表。例如用數組a存儲的二叉樹的結點以下(0單元不用):數據結構

(2)寫出對用二叉鏈表存儲的二叉樹進行先序、中序和後序遍歷的遞歸和非遞歸算法。函數

(3)寫出對用二叉鏈表存儲的二叉樹進行層次遍歷算法。測試

(4)求二叉樹的全部葉子及結點總數。ui


【持續更新 ing】spa

【已完成】:先序創建二叉樹, 已知先序和中序創建二叉樹3d

對二叉樹進行 遞歸非遞歸遍歷


【思路】

遞歸思路很清晰l; 對於非遞歸和層次遍歷,本身構造 棧和隊列 模擬實現, 後序非遞歸 較麻煩


【代碼實現】

FIN  文件  input 裏面的內容爲:

ABD###CE##F##
ABDCEF
DBAECF

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <string.h>
#include <bits/stdc++.h>
#include <algorithm>
#define FIN      freopen("input.txt","r",stdin)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
/*
(1)採用下列方法之一創建二叉樹的二叉鏈表:
① 輸入徹底二叉樹的先序序列,用#表明虛結點(空指針),如ABD###CE##F##,創建二叉樹的二叉鏈表。
② 已知二叉樹的先序遍歷序列和中序遍歷序列,或者已知二叉樹的中序遍歷序列和後序遍歷序列,創建二叉樹的二叉鏈表。
③ 將一棵二叉樹的全部結點存儲在一維數組中,虛結點用#表示,利用二叉樹的性質5,創建二叉樹的二叉鏈表。例如用數組a存儲的二叉樹的結點以下(0單元不用):
(2)寫出對用二叉鏈表存儲的二叉樹進行先序、中序和後序遍歷的遞歸和非遞歸算法。
(3)寫出對用二叉鏈表存儲的二叉樹進行層次遍歷算法。
(4)求二叉樹的全部葉子及結點總數。

*/
const int MAX=1200;
const int n=120;
const int nn=120;
typedef struct Node{
    char x;
    struct Node *lson,*rson;
}Tree,*BiTree;
typedef struct Queue{
    //本身構造隊列
    char a[12000];
    int front;
    int rear;
    void Initqueue()
    {
        front=rear=0;
    }
    int length()
    {
        return (rear-front +MAX)%MAX;
    }
    bool emptys()
    {
        if(front==rear)
            return 1;
        return 0;
    }
    void Push_back(char str)
    {
        if((rear+1)%MAX==front)
            return;
        a[rear]=str;
        rear=(rear+1)%MAX;
    }
    char Getfront()
    {
        if(front==rear)
            return '?';
        char st;
        st=  a[front];
        front=(front+1)%MAX;
        return st;
    }
}queues;

typedef struct Stack{
    // 本身構造棧
    BiTree *tt;
    int top;
    int size;
    void InitStack()
    {
        tt= ( BiTree *)malloc(n*sizeof(Tree));

        top=0;
        size=n;
    }
    bool emptys()
    {
        if(top==0)
            return true;
        return false;
    }
    void Push_back(BiTree num)
    {
        tt[top++]=num;
    }
    void Pop()
    {

        if(top==0)return;
        top--;
        //num=x[--top];
    }
    BiTree Gettop()
    {
        if(top==0) return NULL;
        return tt[top-1];// top
    }
}stacks;
void build_frist(BiTree &T)
// 先序創建
{
    char ch;
    scanf("%c",&ch);
    if(ch=='#')
        T=NULL;
    else
    {
        T= new Tree ;
        T->x=ch;
        build_frist(T->lson);
        build_frist(T->rson);
    }
}
void query(BiTree T,int oper)
{// 三種遞歸 先序中序與後序
    if(T)
    {
        if(oper==1)
            printf("%c ",T->x);
        query(T->lson,oper);
        if(oper==2)
            printf("%c ",T->x);
        query(T->rson,oper);
        if(oper==3)
            printf("%c ",T->x);
    }
}
void build(BiTree &T,char *Fi,char *In,int n)
// 已知先序和中序 創建二叉樹
{
    if(n<1)
    {
        T=NULL;
        return;
    }
    int i=0,l1=0,l2=0,p=0,m=0;
    char lson[120],rson[120];
    char str1[120],str2[120];
    mem(lson,0);
    mem(rson,0);
    T= new Tree;
    T->x=Fi[0];
    while(In[i]!=Fi[0]) i++;
    l1=i; l2=n-i-1;
    int temp=i;
    for(int j=1;j<=temp;j++)
        lson[j-1]=Fi[j];
    for(int j=temp+1;j<n;j++)
    {
        rson[j-temp-1]=Fi[j];
        str2[j-temp-1]=In[j];
    }
    for(int j=0;j<temp;j++)
        str1[j]=In[j];
    if(!lson[0])
        T->lson=NULL;
    else
        build(T->lson,lson,str1,l1);
    if(!rson[0])
        T->rson=NULL;
    else
        build(T->rson,rson,str2,l2);
}
void query_frist(BiTree T)
// 非遞歸先序
{
    stacks S;
    S.InitStack();
    S.Push_back(T);

    while(!S.emptys())
    {
        while(S.Gettop()!=NULL)
        {
            printf("%c ",S.Gettop()->x);
            S.Push_back(S.Gettop()->lson);
        }
        S.Pop();
        if(!S.emptys())
        {
            BiTree Temp;
            Temp=S.Gettop();
            S.Pop();
            S.Push_back(Temp->rson);

        }
    }
}
void query_mid(BiTree T)
//  非遞歸中序
{
    stacks S;
    S.InitStack();
    S.Push_back(T);
    while(!S.emptys())
    {
        while(S.Gettop()!=NULL)
        {
            S.Push_back(S.Gettop()->lson);
        }
        S.Pop();
        if(!S.emptys())
        {
            BiTree Temp;
            Temp=S.Gettop();
            printf("%c ",Temp->x);
            S.Pop();
            S.Push_back(Temp->rson);
        }
    }
}
void query_last(BiTree T)
// 非遞歸後序
{
    stacks S;
    S.InitStack();
    S.Push_back(T);
    while(!S.emptys())
    {
        while(S.Gettop()!=NULL)
        {
            S.Push_back(S.Gettop()->lson);
        }
        S.Pop();
        if(!S.emptys())
        {
            if(S.Gettop()->rson)
                S.Push_back(S.Gettop()->rson);
            else
            {
                BiTree  Temp;
                Temp=S.Gettop();
                printf("%c ",Temp->x);
                S.Pop();
                while(S.Gettop()!=NULL&&!S.emptys()&&S.Gettop()->rson==Temp)
                {
                    printf("%c ",S.Gettop()->x);
                    Temp=S.Gettop();
                    S.Pop();
                }
                if(!S.emptys())
                {
                    S.Push_back(S.Gettop()->rson);
                }

            }
        }
    }
}
void frist_and_mid()
// 已知先序和中序  調用函數 構造二叉樹
{
    cout<<"*****************************"<<endl;
    BiTree T2;
    char str1[120],str2[120],str3[120];
    printf("input of the first :\n");
    scanf("%s",str1);
    printf("input of the mid :\n");
    scanf("%s",str2);
    int len=strlen(str1);
    build(T2,str1,str2,len);
    printf("構造的二叉樹 後序遍歷爲: \n");
    query(T2,3);
    cout<<"\n*****************************"<<endl;
}
int main()
{

    FIN;
    BiTree T;
    build_frist(T);
    printf("遞歸遍歷 先序 中序 後序 依次爲:\n");
    for(int i=1;i<=3;i++)
    {
        query(T,i);//1 先序,2中序,3後序
        cout<<endl;
    }
    frist_and_mid();
    printf("非遞歸先序:\n");
        query_frist(T);
    printf("\n非遞歸中序:\n");
        query_mid(T);
    printf("\n非遞歸後序:\n");
        query_last(T);
    return 0;
}



【測試結果】

FIN  input 文件內容爲:

ABD###CE##F##
ABDCEF
DBAECF

相關文章
相關標籤/搜索