前言
關於二叉樹先、中、後遍歷序列給一箇中序序列以及其他一個就能夠推出另一個序列,而如果單單隻有先序與後序序列只能得到某兩個結點的父子關係而不能確定一個唯一的二叉樹。
算法
主要流程就是搜索,每次通過先序確定根節點,然後根據中序序列確定兩個子樹的規模,遞歸下去。
複雜度分析
在平均情況:
得:複雜度:
實現
先、中序列求後序序列:
#include <bits/stdc++.h>
using namespace std;
#define Elem char
struct BiTree {
Elem elem;
BiTree *lson, *rson;
BiTree(Elem ch):elem(ch),lson(NULL),rson(NULL){}
~BiTree(){}
};
//根結點 先序序列 中序序列 序列長度
void dfs (BiTree* &rt, Elem A[], Elem B[], int len) {
if (len <= 0) //null
return ;
if (len == 1) { //leaf
rt = new BiTree(A[0]);
return ;
}
int index = 0; //子樹根在中序序列中的位置
for (int i = 0; i < len; i++)
if (B[i] == A[0]) {
index = i;
break;
}
rt = new BiTree(A[0]);
dfs(rt->lson, A+1, B, index);
dfs(rt->rson, A+index+1, B+index+1, len-index-1);
}
void f(BiTree *rt) {
if (rt) {
f(rt->lson);
f(rt->rson);
printf("%c", rt->elem);
}
}
void mem_del(BiTree *rt) {
if (rt) {
mem_del(rt->lson);
mem_del(rt->rson);
delete rt;
}
}
int main() {
Elem A[] = "ABCDEF";
Elem B[] = "CBAEDF";
BiTree *rt = NULL;
int len = 6;
dfs(rt, A, B, len);
f(rt);
mem_del(rt);
printf("\n");
return 0;
}
由後、中序列求先序也同理:
#include <bits/stdc++.h>
using namespace std;
#define Elem char
struct BiTree {
Elem elem;
BiTree *lson, *rson;
BiTree(Elem ch):elem(ch),lson(NULL),rson(NULL){}
~BiTree(){}
};
//根結點 後序序列 中序序列 序列長度
void dfs (BiTree* &rt, Elem A[], Elem B[], int len) {
if (len <= 0) //null
return ;
if (len == 1) { //leaf
rt = new BiTree(A[0]);
return ;
}
int index = 0; //子樹根在中序序列中的位置
for (int i = 0; i < len; i++)
if (B[i] == A[len-1]) {
index = i;
break;
}
rt = new BiTree(A[len-1]);
dfs(rt->lson, A, B, index);
dfs(rt->rson, A+index, B+index+1, len-index-1);
}
void f(BiTree *rt) {
if (rt) {
printf("%c", rt->elem);
f(rt->lson);
f(rt->rson);
}
}
void mem_del(BiTree *rt) {
if (rt) {
mem_del(rt->lson);
mem_del(rt->rson);
delete rt;
}
}
int main() {
Elem A[] = "DABEC";
Elem B[] = "DEBAC";
BiTree *rt = NULL;
int len = strlen(A);
dfs(rt, A, B, len);
f(rt);
mem_del(rt);
printf("\n");
return 0;
}