Tags: Tree Try It! Discussion Video Solution # Definition for a binary tree node. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure. There is no restriction on how your serialization/deserialization algorithm should work. Flatten Binary Tree to Linked Listĭesign an algorithm to serialize and deserialize a binary tree. Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored. Vertical Order Traversal of a Binary Tree Longest Palindrome by Concatenating Two Letter Words Painting a Grid With Three Different Colors Longest Substring Without Repeating Characters Find the Edit Distance Between Two Words (Deprecated) Construct Binary Tree from Preorder and Inorder Traversal Preorder Serialization Verification of a Binary Tree (Deprecated) '' if self.right is None else repr(self.right))ĭeserialization is left as an exercise for the reader. '' if self.left is None else repr(self.left), Serialization is easy: def serialize(self): The following diagram shows serialization where ‘)’ is used as end of. The idea is to store an ‘end of children’ marker with every node. An N-ary tree is represented by storing an array or list of child pointers with every node. Instead, serialization could return a more stripped form such as (8,) for Node(8) or ('#',(5,),) for Node('#', Node(5)) and deserialization would be a parser tailored for this representation. In an N-ary tree, there are no designated left and right children. I however advise against the use of eval for deserialization purposes as it is too generic and thus dangerous. Using eval on this string would even deserialize it and return a Node equivalent to t. Lets have a recap on how to serialize and deserialize a binary tree, Serialization is the process of converting a data structure into a sequence of bits. It's a bit verbose, but it is serialized as a string. Return ')'.format(self._class_._name_, self.value, self.left, self.right)Īnd repr(t) would be the string: Node(value=13, left=Node(value=8, left=None, right=None), right=Node(value=15, left=None, right=Node(value=18, left=None, right=None))) If we were to write an official representation for such objects, we could end up with: def _repr_(self): # Īn other way to look at the problem is to take inspiration from repr. So I can more easily write trees: t = Node(13, Node(8), Node(15, right=Node(18))) Return deserialize(cls, source, sentinel='#'):Īnd at the very last, I would provide default values for the left and right parameters: class Node(object):ĭef _init_(self, value, left=None, right=None): This allow for ease of use and easy subclassing: class Node(object): I would also improve the interface of your class: turning serialize into a method and deserialize into a classmethod. Using a parameter with a default value in both serialize and `deserialize is a good option. If you want to keep this intermediate format, you should at least allow the user to chose its sentinel value. Now, what if I what to serialize the node Node('#', None, None)? I will get as output that will get deserialized as None. Python tuples make it easy to return several values at once, you can return the new index with the deserialized value: deserialize(source): Root = Node('a', Node('b', None, None), Node('c', Node('e', Node('g', None, None), None), Node('f', None, None)))įor starter, you don't need to use a global index. One issue in my mind which I cannot resolve is, I am using a global variable index, wondering if more elegant solutions in Python 2.7 - since in Python there is no solution to pass a reference as Java/C++ did. Here is my code of serialize and de-serialize a binary tree, looking for advice.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |