《编程之美》读书笔记19: 3.9 重建二叉树
《编程之美》读书笔记19: 3.9 重建二叉树
对根节点a以及先序遍历次序P和中序遍历次序I,查找a在I中的位置,将I分为两部分,左边部分的元素都在a的左子树上,右边的元素都在a的右子树上,因而可以确定a的左子树节点数和a的右子树节点数,再结合P,可以确定a的左孩子和右孩子,以及各个孩子的先序和中序遍历次序。
由于已经知道节点数,可以事先分配好内存,可以按先序遍历次序连续存放节点。
rebuild_tree_1struct Node {
Node* left;
Node* right;
char data;
};
void rebuild(char preorder[], char inorder[], Node result[], size_t size)
{
result->data = *preorder;
result->left=NULL;
result->right=NULL;
char *p = inorder;
size_t left_size=0;
while (left_size<size && *p++!=*preorder) ++left_size;
assert (left_size<size);
size_t right_size = size-1-left_size;
if (left_size) {
result->left=result+1;
rebuild(preorder+1, inorder, result+1, left_size);
}
if (right_size) {
result->right=result+left_size+1;
rebuild(preorder+left_size+1, inorder+left_size+1,
result+left_size+1, right_size);
}
}
Node* left;
Node* right;
char data;
};
void rebuild(char preorder[], char inorder[], Node result[], size_t size)
{
result->data = *preorder;
result->left=NULL;
result->right=NULL;
char *p = inorder;
size_t left_size=0;
while (left_size<size && *p++!=*preorder) ++left_size;
assert (left_size<size);
size_t right_size = size-1-left_size;
if (left_size) {
result->left=result+1;
rebuild(preorder+1, inorder, result+1, left_size);
}
if (right_size) {
result->right=result+left_size+1;
rebuild(preorder+left_size+1, inorder+left_size+1,
result+left_size+1, right_size);
}
}
上面的代码,栈深度是O(n),有可能出现栈溢出,可以修改代码,减少一次递归调用,实现栈深度为O(lg n)。
rebuild_tree_2void rebuild2(char preorder[], char inorder[], Node result[], size_t size)
{
while (1){
result->data = *preorder;
result->left=NULL;
result->right=NULL;
char *p = inorder;
size_t left_size=0;
while (left_size<size && *p++!=*preorder) ++left_size;
assert (left_size<size);
size_t right_size = size-1-left_size;
if (left_size==0 && right_size==0) break;
if (left_size<right_size) {
if (left_size) {
result->left=result+1;
rebuild2(preorder+1, inorder, result+1, left_size);
}
result->right=result+left_size+1;
preorder += left_size+1;
inorder += left_size+1;
result += left_size+1;
size = right_size;
} else {
if (right_size) {
result->right=result+left_size+1;
rebuild2(preorder+left_size+1, inorder+left_size+1,
result+left_size+1, right_size);
}
if (left_size) {
result->left=result+1;
++preorder;
++result;
size = left_size;
}
}
}
}
{
while (1){
result->data = *preorder;
result->left=NULL;
result->right=NULL;
char *p = inorder;
size_t left_size=0;
while (left_size<size && *p++!=*preorder) ++left_size;
assert (left_size<size);
size_t right_size = size-1-left_size;
if (left_size==0 && right_size==0) break;
if (left_size<right_size) {
if (left_size) {
result->left=result+1;
rebuild2(preorder+1, inorder, result+1, left_size);
}
result->right=result+left_size+1;
preorder += left_size+1;
inorder += left_size+1;
result += left_size+1;
size = right_size;
} else {
if (right_size) {
result->right=result+left_size+1;
rebuild2(preorder+left_size+1, inorder+left_size+1,
result+left_size+1, right_size);
}
if (left_size) {
result->left=result+1;
++preorder;
++result;
size = left_size;
}
}
}
}
书上的代码(P246):
* pTemp = new NODE;
pTemp -> chValue = *pPreOrder;
pTemp -> pLeft = NULL;
pTemp -> pRight = NULL;
// 如果节点为空,把当前节点复制到根节点
if(*pRoot == NULL)
{
*pRoot = pTemp;
}
pTemp -> chValue = *pPreOrder;
pTemp -> pLeft = NULL;
pTemp -> pRight = NULL;
// 如果节点为空,把当前节点复制到根节点
if(*pRoot == NULL)
{
*pRoot = pTemp;
}
可能引起内存泄漏(当*pRoot!=NULL,新申请的内存没释放),注释也不对(不是复制节点,而是更改指针指向新建的节点)。另外,频繁的new,极有可能会产生内存碎片。当节点很小时,内存浪费很严重(每new一次都要额外分配空间储存相关信息)。
作者: flyinghearts
出处: http://www.cnblogs.com/flyinghearts/
本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。