Order traversal using binary reconstruction and preorder

Order traversal using binary reconstruction and preorder

Foreword

I encountered such a problem, and now know preorder traversal and post-order traversal of binary tree, how to reconstruct a binary tree this information.
At first sight a little bit ignorant estimated, the data out to a careful analysis to know, in fact, quite simple.

Algorithm thinking

There are the following data:

Preorder traversal: 32154879
preorder: 12345789

According to the law preorder traversal of us know, the first data preorder traversal is the root node of data, OK we marked the first bit of data:

Preorder traversal: [3] 2,154,879
preorder: 1 2 [3] 45,789

According to the law then we traverse (before, during and after traversing order are the same), we know that traverse always first output left subtree, and then output the right subtree, differ only the root position data (before, during and after ). So in order traversal, the root node data is data bound to the left of the left subtree, right is right subtree data. From this point we can put data into the root, left subtree and right subtree.

Root before preorder: 3 preorder: 3
front left subtree order traversal: 21 preorder: 12
front right subtree preorder: 54879 preorder: 45789

OK, to this point later, wise friend should know how to do then. The left subtree and right subtree recursively perform the same operation as above, until all the data to do a root node, the end of the algorithm execution.

Code

//二叉树结构体
struct Tree {
	int value;
	Tree *left;
	Tree *right;
};

//pre:前序遍历
//mid:中序遍历
//length:数组长度
Tree* Fun(const int *pre, const int *mid,const int length) {
	//当空指针或者数组长度为0时返回
	if (pre == NULL || mid == NULL || length <= 0) 
	    return NULL;

	//建立根节点
	Tree *head = new Tree();
	head->value=pre[0];

	//找到中序遍历中根节点所在位置
	int index = 0;
	while (index<length) {
		if (head->value == mid[index])
			break;
		++index;
	}

	//划分左子树
	head->left = Fun(pre + 1, mid, index);
	//划分右子树
	head->right = Fun(pre + index +1, mid + index + 1, length - index - 1);

	return head;
}

Spread

Order traversal and how to pre-order traversal if after replace? The idea is still the same, interested friends can explore on their own. Boring I put the code also lost here.

//mid:中序遍历
//back:后序遍历
//length:数组长度
Tree* Fun2(const int *mid,const int *back,const int length){
    //当空指针或者数组长度为0时返回
    if(back==NULL||mid==NULL||length<=0)
        return NULL;
    
    //建立根节点
    Tree *head=new Tree();
    head->val=back[length-1];
    head->left=head->right=NULL;
    
    //找到中序遍历中根节点所在位置
    int index=0;
    while(index<length){
        if(head->val==mid[index]){
            break;
        }
        ++index;
    }
    
    //划分左子树
    head->left=Fun2(mid,back,index);
    //划分右子树
    head->right=Fun2(mid+index+1, back+index, length-index-1);
    
    return head;
}

If you do not preorder it? Front-order traversal and postorder tree can fall to launch it? Interested parties can see another article I use any two binary tree traversal reconstruction .

Published 63 original articles · won praise 73 · views 70000 +

Guess you like

Origin blog.csdn.net/jjwwwww/article/details/85799421