Question

Python Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserial...

Python

Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. For example, given the following Node class

class Node: def __init__(self, val, left=None, right=None):

self.val = val self.left = left self.right = right

The following test should pass:

node = Node('root', Node('left', Node('left.left')), Node('right')) assert deserialize(serialize(node)).left.left.val == 'left.left'

0 0
Add a comment Improve this question Transcribed image text
Answer #1

"""

Python

Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. For example, given the following Node class

class Node: def __init__(self, val, left=None, right=None):

self.val = val self.left = left self.right = right

The following test should pass:

node = Node('root', Node('left', Node('left.left')), Node('right')) assert deserialize(serialize(node)).left.left.val == 'left.left'

"""


class Node:
   def __init__(self, value):
       self.val = value
       self.left = None
       self.right = None

class Tree:
   def __init__(self):
       self.root = None


   def addNode(self, noe, value):
       if node == None:
           self.root = Node(value)
       else:
           if value < node.value:
               if not node.left:
                   node.left = Node(value)
               else:
                   self.addNodde(node.left, value)
           else:
               if not node.right:
                   node.right = Node(value)
               else:
                   self.adddNode(node.right, value)


def serialize(root):
   values = []
   def serializer(node):
       if not node:
           values.append('?')
       else:
           values.append(str(node.value))
           serializer(node.left)
           serializer(node.right)
   serializer(root)
   return ','.join(values)

def deserialize(s):
   values = iter(s.split(','))
   def deserializer():
       val = next(values)
       if val == '?':
           return None
       else:
           node = Node(int(val))
           node.left = deserializer()
           node.right = deserializer()
           return node
   return deserializer()


if __name__ == '__main__':
   #read input, number separated by comas
   numbers = [int(n) for n in input().split(',')]
   theTree = Tree()
   for number in numbers:
       theTree.addNode(theTree.root, number)
   s1 = serialize(theTree.root)
   s2 = serialize(deserialize(s1))
   print(s1)
   print(s2)
   assert s1 == s2


          

Add a comment
Know the answer?
Add Answer to:
Python Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserial...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT