根據前序遍歷序列和中序遍歷序列構造二叉樹
阿新 • • 發佈:2019-02-08
根據前序遍歷序列和中序遍歷序列可以構造唯一的二叉樹。
假設序列為string型
根據前序遍歷的特點, 知前序序列(Pre)的首個元素(Pre[0])為根(root), 然後在中序序列(In)中查詢此根(Pre[0]), 根據中序遍歷特點, 知在查詢到的根(root) 前邊的序列為左子樹, 後邊的序列為右子樹。 設根前邊有left個元素.. 則又有, 在前序序列中, 緊跟著根(root)的left個元素序列(即Pre[1...left]) 為左子樹, 在後邊的為右子樹..而構造左子樹問題其實跟構造整個二叉樹問題一樣,只是此時前序序列為Pre[1...left]), 中序序列為In[0...left-1], 分別為原序列的子串, 構造右子樹同樣, 顯然可以用遞迴方法解決。
因此演算法可以描述為:
build_tree(sub_root, Pre, In)
1。if ( size( Pre) > 0)
2。 sub_root =new Node (Pre[0] );
3。 Index = Pre[0]在In中的位置
4。 In_left = In[0...Index-1] , In_right = In[Index...]
5。 Pre_left =Pre[1..Index], Pre_righ = Pre[Index+1...]
6。 build_tree(sub_root->left , Pre_left, In_left)
7。 build_tree(sub_root->right, Pre_right, Pre_right)
C++程式碼如下:
template <class Entry>
Binary_tree<Entry>::Binary_tree(string p, string s)
/* p是二叉樹的先序序列,s是二叉樹的中序序列。演算法建立二叉樹,使其先序序列和中序序列分別為p和s。
uses: pre_in_build
*/
{
pre_in_build(root, p, s);
}
template <class Entry>
void Binary_tree<Entry>::pre_in_build(Binary_node<Entry>*&sub_root, string pre, string in)
{
if(pre.length() >0)
{
sub_root=new Binary_node<char> (pre[0]);
int index=in.find(pre[0]);
string in_left_str=in.substr(0, index);
string in_right_str=in.substr(index+1);
string pre_left_str=pre.substr(1, index);
string pre_right_str=pre.substr(index+1);
pre_in_build(sub_root->left, pre_left_str, in_left_str);
pre_in_build(sub_root->right, pre_right_str, in_right_str);
}
}
string 類返回子串函式
basic_string substr( size_type _Off = 0, size_type _Count = npos )
引數說明:
_Off 為開始的下標, 預設為0。
_Count為子串的大小, 預設從開始到串的末尾。
npos = -1; 代表not found 或all remaining characters
返回子串string
如果_Count =0, 則返回為空的string
如果下標_Off >= length, 亦返回空string
假設序列為string型
根據前序遍歷的特點, 知前序序列(Pre)的首個元素(Pre[0])為根(root), 然後在中序序列(In)中查詢此根(Pre[0]), 根據中序遍歷特點, 知在查詢到的根(root) 前邊的序列為左子樹, 後邊的序列為右子樹。 設根前邊有left個元素.. 則又有, 在前序序列中, 緊跟著根(root)的left個元素序列(即Pre[1...left]) 為左子樹, 在後邊的為右子樹..而構造左子樹問題其實跟構造整個二叉樹問題一樣,只是此時前序序列為Pre[1...left]), 中序序列為In[0...left-1], 分別為原序列的子串, 構造右子樹同樣, 顯然可以用遞迴方法解決。
因此演算法可以描述為:
build_tree(sub_root, Pre, In)
1。if ( size( Pre) > 0)
2。 sub_root =new Node (Pre[0] );
3。 Index = Pre[0]在In中的位置
4。 In_left = In[0...Index-1] , In_right = In[Index...]
5。 Pre_left =Pre[1..Index], Pre_righ = Pre[Index+1...]
6。 build_tree(sub_root->left , Pre_left, In_left)
7。 build_tree(sub_root->right, Pre_right, Pre_right)
C++程式碼如下:
template
Binary_tree<Entry>::Binary_tree(string p, string s)
/* p是二叉樹的先序序列,s是二叉樹的中序序列。演算法建立二叉樹,使其先序序列和中序序列分別為p和s。
uses: pre_in_build
*/
{
pre_in_build(root, p, s);
}
template <class Entry>
void Binary_tree<Entry>::pre_in_build(Binary_node<Entry>*&sub_root, string
{
if(pre.length() >0)
{
sub_root=new Binary_node<char> (pre[0]);
int index=in.find(pre[0]);
string in_left_str=in.substr(0, index);
string in_right_str=in.substr(index+1);
string pre_left_str=pre.substr(1, index);
string
pre_in_build(sub_root->left, pre_left_str, in_left_str);
pre_in_build(sub_root->right, pre_right_str, in_right_str);
}
}
string 類返回子串函式
basic_string substr( size_type _Off = 0, size_type _Count = npos )
引數說明:
_Off 為開始的下標, 預設為0。
_Count為子串的大小, 預設從開始到串的末尾。
npos = -1; 代表not found 或all remaining characters
返回子串string
如果_Count =0, 則返回為空的string
如果下標_Off >= length, 亦返回空string