根據先序序列和中序序列求後序序列的遞迴實現

2022-09-18 23:42:12 字數 2971 閱讀 1491

示例**:( 測試位址 : )

1

1 #include2

2 #include3

3 #include445

5using

namespace

std ;66

77/////////

--------------------設計樹的資料模型--------------------

///////////8

8/////////

---start99

1010 template< class t >

1111

class treeseq : public vector12

1217

1718

18 template< class iterator >

1919 treeseq( iterator beg, iterator end ) : vector( beg, end )

2020

2121

treeseq(){}

2222

2323 friend ostream& operator

<<( ostream& out_stream, const treeseq&tree_seq )

2424

2929

3030

return

out_stream ;

3131}32

32} ;

3333

3434 template< class t >

3535

const treeseqoperator+( const treeseq& tree_obj, const treeseq&tree_seq )

3636

4444

4545

return

temp ;

4646}47

4748

48 template< class t >

4949

const treeseqoperator+( const treeseq& tree_obj, const t&tree_node )

5050

5757

/////////

--------------------設計樹的資料模型--------------------

/////////

//58

58/////////

---end !

5959

6060

6161

/////////

---------------根據先序中序求後序的遞迴實現---------------

/////////

//62

62/////////

---start

6363

6464 template< class t >

6565

void getafttraseq( const treeseq& pretraseq, const treeseq& midtraseq, treeseq&afttraseq )

6666

102102

}103

103/////////

---------------根據先序中序求後序的遞迴實現---------------

/////////

//104

104/////////

---end !

105105

106106

107107

/////////

---------------------- 數 據 測 試 -------------------

/////////

//108

108/////////

---109

109int main( int argc, char **ar** )

110110

執行結果

1

已知先序遍歷序列 : a b d e c f g

2已知中序遍歷序列 : d b e a f c g 34

運算過程的資料變化情況如下 : 57

6pretraseq : a b d e c f g

7midtraseq : d b e a f c g 83

9pretraseq : b d e

10midtraseq : d b e 111

12pretraseq : d

13midtraseq : d

14d

1516117

pretraseq : e

18midtraseq : e

19e

2021

d e b

2223324

pretraseq : c f g

25midtraseq : f c g 261

27pretraseq : f

28midtraseq : f

29f

3031132

pretraseq : g

33midtraseq : g

34g

3536

f g c

3738

d e b f g c a

3940 求得的後序遍歷序列為 : d e b f g c a

根據先序序列和中序序列求後序序列的迴圈實現

1 include2 include3 include4 include5 6using namespace std 78 設計樹的資料模型 9 start 1011 template class t 12class treeseq public vector13 1819 template cla...

已知先序中序序列求後序序列

way 1.由先序和中序遍歷序列確定一棵二叉樹,再後序遍歷得到後序序列。如何確定呢?1.根據先序遍歷的第乙個結點確定根節點 2.根據根節點在中序遍歷序列的位置分割出左右兩個子串行,即根節點的左右子樹 3.對左右子樹按此方法遞迴進行分解。定義二叉樹的資料結構 typedef struct treeno...

已知後序中序序列求先序序列

方法呢,與前一篇一樣,建樹或者不建樹皆可,這裡不做過多說明,直接show code。way 1.typedef struct treenode bintree struct treenode bintree buildtree char post,char in,int n way 2.const ...