根据前序和中序序列重建二叉树
2013-01-30 23:44
295 查看
http://blog.chinaunix.net/uid-1844931-id-3033009.html
今天一个小朋友问到这个问题,顺便复习一下数据结构了。
PreOrder(T)=T的根节点+PreOrder(T的左子树)+PreOrder(T的右子树)
InOrder(T)=InOrder(T的左子树)+T的根节点+InOrder(T的右子树)
PostOrder(T)=PostOrder(T的左子树)+PostOrder(T的右子树)+T的根节点
其中加号表示字符串连接运算
例如,对下图所示的二叉树,先序遍历为DBACEGF,中序遍历为ABCDEFG。重建该二叉树:
这个算法其实很简单的。 首先你自己要能够根据先序和中序能够手动的建立起来树。 先序串:DBACEGF,先序的第一个节点一定是根节点,这样我们就知道了根节点是D. 再看中序, 在中序串之中,根结点的前边的所有节点都是左子树中,ABCDEFG,所以D节点前面的ABC就是左子树的中序串。再看前续串 DBACEGF, 由于左子树的节点是ABC,我们可以得到左子树的前续周游的串为:
BAC. 有了左子树的前序串BAC,和中序串ABC ,我们就可以递归的把左子树给建立起来。 同样,可以建立起右子树。
下面是写的伪代码,(没有编译过)
class TreeNode
{
pubic:
char value;
TreeNode *left;
TreeNode *right;
TreeNode(char c): value(c){
left = NULL;
rigth = NULL;
}
~TreeNode() {
if(left != NULL) delete left;
if(right != NULL) delete right;
}
};
/**根据前序周游和中序周游,重建一颗二叉树。
* @param pre 前序周游的结果,其中每一个字符表示一个节点的值
* @param mid 中序周游的结果,其中每一个字符表示一个节点的值
* @param n 该树节点总数。
* @return 生成的树的根节点。
*/
TreeNode* buildTree(char *pre, char *mid, int n)
{
if (n==0) return NULL;
char c = pre[0];
TreeNode *node = new TreeNode(c); //This
is the root node of this tree/sub tree.
for(int i=0; i<n && mid[i]!=c; i++);
int lenL = i; // the node number
of the left child tree.
int lenR = n - i -1; //
the node number of the rigth child tree.
//build the left child tree. The first order for thr left child tree is from
// starts from pre[1], since the first element in pre order sequence is the root
// node. The length of left tree is lenL.
if(lenL > 0) node->left = buildTree(&pre[1], &mid[0], lenL);
//build the right child tree. The first order stree of right child is from
//lenL + 1(where 1 stands for the root node, and lenL is the length of the
// left child tree.)
if(lenR > 0) node->right = buildTree(&pre[lenL+1], &mid[lenL+1], lenR);
return node;
}
今天一个小朋友问到这个问题,顺便复习一下数据结构了。
PreOrder(T)=T的根节点+PreOrder(T的左子树)+PreOrder(T的右子树)
InOrder(T)=InOrder(T的左子树)+T的根节点+InOrder(T的右子树)
PostOrder(T)=PostOrder(T的左子树)+PostOrder(T的右子树)+T的根节点
其中加号表示字符串连接运算
例如,对下图所示的二叉树,先序遍历为DBACEGF,中序遍历为ABCDEFG。重建该二叉树:
这个算法其实很简单的。 首先你自己要能够根据先序和中序能够手动的建立起来树。 先序串:DBACEGF,先序的第一个节点一定是根节点,这样我们就知道了根节点是D. 再看中序, 在中序串之中,根结点的前边的所有节点都是左子树中,ABCDEFG,所以D节点前面的ABC就是左子树的中序串。再看前续串 DBACEGF, 由于左子树的节点是ABC,我们可以得到左子树的前续周游的串为:
BAC. 有了左子树的前序串BAC,和中序串ABC ,我们就可以递归的把左子树给建立起来。 同样,可以建立起右子树。
下面是写的伪代码,(没有编译过)
class TreeNode
{
pubic:
char value;
TreeNode *left;
TreeNode *right;
TreeNode(char c): value(c){
left = NULL;
rigth = NULL;
}
~TreeNode() {
if(left != NULL) delete left;
if(right != NULL) delete right;
}
};
/**根据前序周游和中序周游,重建一颗二叉树。
* @param pre 前序周游的结果,其中每一个字符表示一个节点的值
* @param mid 中序周游的结果,其中每一个字符表示一个节点的值
* @param n 该树节点总数。
* @return 生成的树的根节点。
*/
TreeNode* buildTree(char *pre, char *mid, int n)
{
if (n==0) return NULL;
char c = pre[0];
TreeNode *node = new TreeNode(c); //This
is the root node of this tree/sub tree.
for(int i=0; i<n && mid[i]!=c; i++);
int lenL = i; // the node number
of the left child tree.
int lenR = n - i -1; //
the node number of the rigth child tree.
//build the left child tree. The first order for thr left child tree is from
// starts from pre[1], since the first element in pre order sequence is the root
// node. The length of left tree is lenL.
if(lenL > 0) node->left = buildTree(&pre[1], &mid[0], lenL);
//build the right child tree. The first order stree of right child is from
//lenL + 1(where 1 stands for the root node, and lenL is the length of the
// left child tree.)
if(lenR > 0) node->right = buildTree(&pre[lenL+1], &mid[lenL+1], lenR);
return node;
}
相关文章推荐
- 根据前序和中序列 重建二叉树
- 剑指:根据输入的前序和中续遍历序列重建二叉树
- POJ 2255 根据二叉树的前序和中序序列来重建二叉树
- 根据前序和中序的遍历序列重建二叉树
- 根据二叉树的前序和中序序列来重建二叉树
- 根据前序和中序序列重建二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- 根据中序和后序序列重建二叉树 Construct Binary Tree from Inorder and Postorder Traversal
- 根据前序序列和中序序列重建二叉树
- 根据前序和中序重建二叉树
- [面试] 根据前序和中序重建二叉树,并且中序非递归遍历
- C++根据前序和中序重建二叉树
- 根据树的后序和中序序列或者前序和中序序列构建二叉树,
- 根据给定先序和中序序列来重建二叉树
- 【构建二叉树】01根据前序和中序序列构造二叉树【Construct Binary Tree from Preorder and Inorder Traversal】
- Python练手之根据前序和中序&根据中序和后序重建二叉树,输出前序、中序和后序遍历结果
- 根据前序和中序重建二叉树的代码,出了错误 请指教
- 二叉树重建 ( 根据中序序列和前序序列获取后序序列)
- [LeetCode] 根据中序和后序序列重建二叉树
- 根据前序遍历序列和中序遍历序列重建二叉树
- 重建二叉树(根据前序和中序遍历结果)