中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C++對樹進行后序遍歷的代碼

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
#include <stdio.h>
#include <string.h>
struct Node{
    Node *lchild;// 左兒子指針
    Node *rchild;// 右兒子指針
    char c;//結(jié)點字符信息
}Tree[50];// 靜態(tài)內(nèi)存分配數(shù)組
  
int loc;// 靜態(tài)數(shù)組中已經(jīng)分配的結(jié)點個數(shù)
Node *creat(){//申請一個結(jié)點空間,返回指向其的指針
    Tree[loc].lchild=Tree[loc].rchild=NULL;//初始化左右兒子為空
    return &Tree[loc++];
}
char str1[30],str2[30];// 保存前序和中序遍歷字符串
  
//修改打印輸出的位置就可以進行相應的前序和中序遍歷了
void postOrder(Node *T){
    if(T->lchild!=NULL){
        postOrder(T->lchild);
    }
    if(T->rchild!=NULL){
        postOrder(T->rchild);
    }
    printf("%c",T->c);// 遍歷該結(jié)點,輸出字符信息
}
Node *build(int s1,int e1,int s2,int e2){
    Node* ret=creat();
    ret->c=str1[s1];//該結(jié)點字符為前序遍歷中的第一個字符
    int rootIdx;
    for(int i=s2;i<=e2;i++){
        if(str2[i]==str1[s1]){
            rootIdx=i;
            break;
        }
    }
    if(rootIdx!=s2){
        ret->lchild=build(s1+1,s1+(rootIdx-s2),s2,rootIdx-1);//遞歸還原其左子樹
    }
    if(rootIdx!=e2){
        ret->rchild=build(s1+(rootIdx-s2)+1,e1,rootIdx+1,e2);//遞歸還原其右子樹
    }
    return ret;
}
  
  
int main(){
    freopen("in.txt", "r", stdin);
    while(scanf("%s",str1)!=EOF){
        scanf("%s",str2);//輸入
        loc=0;// 初始化靜態(tài)內(nèi)存空間中已經(jīng)使用結(jié)點個數(shù)為0
        int L1=strlen(str1);
        int L2=strlen(str2);
        Node *T=build(0,L1-1,0,L2-1);
        postOrder(T);//后序遍歷
        printf("n");
    }
    return 0;
}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡,如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:android 實現(xiàn)搖一搖功能

下一篇:C++解決背包問題(Knapsacks Problem)