根据先序序列和中序序列求后序序列的递归实现
示例代码:( 测试地址 :http://codepad.org/6XDTlfcl )
1 1 #include<vector> 2 2 #include<iostream> 3 3 #include<algorithm> 4 4 5 5 using namespace std ; 6 6 7 7 /////////--------------------设计树的数据模型--------------------/////////// 8 8 /////////---start 9 9 10 10 template< class T > 11 11 class TreeSeq : public vector<T> 12 12 { 13 13 public : 14 14 typedef T TreeNode ; 15 15 16 16 TreeSeq( typename vector<T>::iterator beg, typename vector<T>::iterator end ) : vector<T>( beg, end ) { } 17 17 18 18 template< class Iterator > 19 19 TreeSeq( Iterator beg, Iterator end ) : vector<T>( beg, end ) { } 20 20 21 21 TreeSeq(){} 22 22 23 23 friend ostream& operator<<( ostream& out_stream, const TreeSeq& tree_seq ) 24 24 { 25 25 for( unsigned index = 0; index != tree_seq.size(); index++ ) 26 26 { 27 27 out_stream << tree_seq[index] << '\t' ; 28 28 } 29 29 30 30 return out_stream ; 31 31 } 32 32 } ; 33 33 34 34 template< class T > 35 35 const TreeSeq<T> operator+( const TreeSeq<T>& tree_obj, const TreeSeq<T>& tree_seq ) 36 36 { 37 37 TreeSeq<T> temp( tree_obj ); 38 38 39 39 for ( typename vector<T>::const_iterator ite = tree_seq.begin(); 40 40 ite != tree_seq.end(); ite++ ) 41 41 { 42 42 temp.push_back( *ite ); 43 43 } 44 44 45 45 return temp ; 46 46 } 47 47 48 48 template< class T > 49 49 const TreeSeq<T> operator+( const TreeSeq<T>& tree_obj, const T& tree_node ) 50 50 { 51 51 TreeSeq<T> temp( tree_obj ); 52 52 53 53 temp.push_back( tree_node ); 54 54 55 55 return temp ; 56 56 } 57 57 /////////--------------------设计树的数据模型--------------------/////////// 58 58 /////////---end ! 59 59 60 60 61 61 /////////---------------根据先序中序求后序的递归实现---------------/////////// 62 62 /////////---start 63 63 64 64 template< class T > 65 65 void getAftTraSeq( const TreeSeq<T>& preTraSeq, const TreeSeq<T>& midTraSeq, TreeSeq<T>& aftTraSeq ) 66 66 { 67 67 typedef TreeSeq<T> TreeSeq ; 68 68 typedef typename vector<T>::const_iterator TreeIte ; 69 69 typedef typename TreeSeq::TreeNode TreeNode ; 70 70 typedef unsigned DifSize ; 71 71 72 72 TreeIte pre_beg = preTraSeq.begin() ; 73 73 TreeIte pre_end = preTraSeq.end() ; 74 74 75 75 if( pre_beg != pre_end ) 76 76 { 77 77 cout << preTraSeq.size() << endl ; 78 78 cout << "preTraSeq : " << preTraSeq << endl ; 79 79 cout << "midTraSeq : " << midTraSeq << endl ; 80 80 81 81 TreeNode t_node = *pre_beg ; 82 82 83 83 TreeIte mid_beg = midTraSeq.begin() ; 84 84 TreeIte mid_end = midTraSeq.end() ; 85 85 86 86 TreeIte tar_ite = find( mid_beg, mid_end, t_node ); 87 87 88 88 DifSize dif_size = tar_ite - mid_beg ; 89 89 90 90 TreeSeq aft_lch , aft_rch ; 91 91 92 92 getAftTraSeq( TreeSeq( pre_beg + 1, pre_beg + 1 + dif_size ), 93 93 TreeSeq( mid_beg, mid_beg + dif_size ), aft_lch ); 94 94 95 95 getAftTraSeq( TreeSeq( pre_beg + 1 + dif_size, pre_end ), 96 96 TreeSeq( mid_beg + dif_size + 1, mid_end ), aft_rch ); 97 97 98 98 aftTraSeq = aft_lch + aft_rch + t_node ; 99 99 100 100 cout << aftTraSeq << endl << endl ; 101 101 } 102 102 } 103 103 /////////---------------根据先序中序求后序的递归实现---------------/////////// 104 104 /////////---end ! 105 105 106 106 107 107 /////////---------------------- 数 据 测 试 -------------------/////////// 108 108 /////////--- 109 109 int main( int argc, char **argv ) 110 110 { 111 111 char pre_seq_ary[8] = "ABDECFG" ; 112 112 char mid_seq_ary[8] = "DBEAFCG" ; 113 113 114 114 TreeSeq<char> pre_seq( pre_seq_ary, pre_seq_ary + 7 ) ; 115 115 TreeSeq<char> mid_seq( mid_seq_ary, mid_seq_ary + 7 ) ; 116 116 TreeSeq<char> aft_seq ; 117 117 118 118 cout << "已知先序遍历序列 : " << pre_seq << endl ; 119 119 cout << "已知中序遍历序列 : " << mid_seq << endl << endl ; 120 120 cout << "运算过程的数据变化情况如下 : " << endl ; 121 121 122 122 getAftTraSeq( pre_seq, mid_seq, aft_seq ); 123 123 124 124 cout << "求得的后序遍历序列为 : " << aft_seq << endl ; 125 125 }
运行结果 :
1 已知先序遍历序列 : A B D E C F G 2 已知中序遍历序列 : D B E A F C G 3 4 运算过程的数据变化情况如下 : 5 7 6 preTraSeq : A B D E C F G 7 midTraSeq : D B E A F C G 8 3 9 preTraSeq : B D E 10 midTraSeq : D B E 11 1 12 preTraSeq : D 13 midTraSeq : D 14 D 15 16 1 17 preTraSeq : E 18 midTraSeq : E 19 E 20 21 D E B 22 23 3 24 preTraSeq : C F G 25 midTraSeq : F C G 26 1 27 preTraSeq : F 28 midTraSeq : F 29 F 30 31 1 32 preTraSeq : G 33 midTraSeq : G 34 G 35 36 F G C 37 38 D E B F G C A 39 40 求得的后序遍历序列为 : D E B F G C A