The serialization and deserialization of binary search tree

Source: Internet
Author: User
Tags min serialization tree serialization valid

Note: Transfer from http://blog.csdn.net/sgbfblog/article/details/7774347

(1) serialization and deserialization of Binary search tree: (1.0) Background:
Binary tree serialization, will be the binary tree according to some kind of traversal method to keep the values of each node to the file;
Binary tree deserialization, that is, according to the value of the file or give an array, and then to construct a binary tree;
(1.1) Questions:
To design an algorithm that saves a binary search tree (TREE,BST) to a file, you need to be able to recover the original two-fork search tree from the file. Notice the space-time complexity of the algorithm.


(1.2) Ideas:
The binary tree traversal algorithm has the first order traversal, the middle sequence traversal, the post-order traversal algorithm and so on. But only one of them has a traversal algorithm that matches the condition of the problem, which is used to save BST to the file and restore the original BST from the file.
Suppose we want to save the BST as follows:
_ 30_
/    \
20 40
/      / \
10 35 50
1.2.1) Middle sequence traversal
If we do a middle-order traversal of this BST, we can get 10 20 30 35 40 50, but we cannot infer the original two-fork search tree structure from it. The output is 10 20 30 35 40 50, a possible BST structure is shown below, this is an unbalanced BST, obviously this is not the original BST.
_50
/
40
/
35
/
30
/
20
/
10
1.2.2) post-traversal
Since the middle sequence traversal cannot satisfy the condition, then look at how the post-order traversal. Post-order traversal prints the leaf nodes before printing out the parent node. The subsequent traversal of this BST can be achieved by: 10 20 35 50 40 30. Reading these nodes and constructing the original BST is a challenge, because when constructing a two-fork tree, the parent node is first inserted into the child's node, and the sequence traversal is first read to the child node and then the parent node, so it is not eligible.


1.2.3) First Order traversal
Both the sequence traversal and the post-order traversal do not satisfy the condition, only the first order traversal can satisfy the condition. The first order traversal results of BST are: 30 20 10 40 35 50. The important point we observe is:
The Father node of a node is always output before the node.
With this observation, after we read the BST node sequence from a file, it is always possible to construct their parent node before constructing the child node. The code that writes BST to a file is the same as the first-order traversal.


(1.3) Deserialization:
1.3.1) The question now is how to reconstruct BST from a sequence of read nodes.
The simple approach is to use the two-fork search tree Insert method to perform N-insertions for each node, requiring time O (LgN) for each insertion, so that a total of O (NLGN) time is required. This method is not efficient enough.
That is, given an array, constructs a binary search tree based on the array;


Solution:
Review the previous article to determine whether a binary tree is a two-fork sorting tree, the solution 2 uses the range determination to determine whether each node meets the criteria.
Similar ideas are used here to give a more efficient solution to reconstruct the original two-fork search tree from the file. We pass a valid range from the parent node to the child node.
When we want to insert a node, we determine whether the insertion node is in the valid range, if it is inserted, otherwise look for a new position to insert. The entire time complexity is O (N).


void Readbsthelper (int min, int max, int &insertval,
struct node *&p, ifstream &fin)
{
if (Insertval > Min && insertval < max) {
int val = insertval;
p = NewNode (val);
if (Fin >> insertval) {
Readbsthelper (Min, Val, Insertval, P->left, Fin);
Readbsthelper (Val, Max, Insertval, P->right, Fin);
}
}
}

void Readbst (struct node *&root, ifstream &fin)
{
int Val;
Fin >> val;
Readbsthelper (Int_min, Int_max, Val, Root, Fin);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.