![]() ![]() The "vaule string" is the values of each node (using preorder order) in string format. The "inorder string" is the indices of each node in inorder order. The "preorder string" is the indices of each node in preorder order, actually, I have given the sequence from 1 to the number of nodes in this string for simplicity. So, our encoding format is: "preorder string inorder string value string". Note that in this problem, there might be duplicate values in different nodes, we have to use the indices of each node for the traversal. Please take a look at my previous post for details. Therefore, we could use preorder and inorder traversal to reconstruct the binary tree. Particularly, we have a binary tree, rather than the level by level traversal, we still have three depth-first traversal: preorder, inorder, and postorder. Although it is not a quite efficient method, it still provides good practice of binary tree and binary tree traversal. Secondly, I'l like to show the implementation using a different way. For the deserialization, we could still keep two queues for the BFS, and keep track of the node we are expanding. Here in my solution, I choose to use two queues, to store nodes in current level, and nodes in next level, respectively. Since we are "searching" the tree level by level, DFS is usually a good way to do so. ![]() We will encode the node value level by level, and only encode the "Null" (or "None" in python) node when it is a leaf node. ![]() Let's first see the simple way of doing this. If you would like to see the simple version which is the same way of LeetCode OJ, please check out the pyhton code below. Using (var reader = new BinaryReader(File.In this problem, I have implemented a differrent way of serialization (in C++ code shown below). Public BST ReadBSTFromFile(string TreeStoreFile) Using (var writer = new BinaryWriter(File.Create(TreeStoreFile))) Public void StoreBSTToFile(BST bst, string TreeStoreFile) It could be anything, the most natural would be a bool representing something similar to Nullable.HasValue, but for Node reference, like this class ObjectPersistence When writing a binary data to a file (or stream), you need to put some "marker" (indicator) for null (in contrast with XML where you have a natural "missing" element/attribute). Else, store current node and recur for its childrenīut this is very specific to your tree, as it only works for simple keys (like integers in your case), and it's not very space/speed efficient. Void Serialize(Node root, StreamWriter writer) If your intention is to port the C++ to C# completely, then the serialization method would look like this: // This function stores a tree in a file pointed by fp If you are storing plain C# objects, than a null value clearly describes an empty node. And since the author of the linked article used the tree to store integers, (s)he chose to use the "special" -1 value for empty nodes.īut if you are not storing the tree this way internally (I presume you are using a linked list), then there is not point in adding these dummy values. When represented this way, you have to use a "dummy" value for empty nodes. This solution requires requires space twice the size of Binary Tree. I don't see why you wouldn't use some of standard (de)serialization techniques ( BinaryFormatter, XmlSerializer, data contracts, protocol buffers)?īut if you really want to use the approach given in the link, the point of the article can be summarized into:Ī simple solution is to store both Inorder and Preorder traversals.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |