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'
"""
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
Python Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserial...
Can you help me with python question: Implement the function isBinarySearchTree(root), which returns True if the binary tree passed to it is a valid binary search tree. I've provided a simple TreeNode class, and the beginnings of isBinarySearchTree(root). class TreeNode: '''Node for a simple binary tree structure''' def __init__(self, value, left, right): self.value = value self.left = left self.right = right def isBinarySearchTree(tree):
Question 3 (24 points) (0) Given Red-Black RBBST Node, implement insertNode ( ) function class RBBST Node: class RBBST: def init_(self, val, color): self.val val self.left None self.right None self.color color def init_(self) : self.root None def init_rbbst(self, val, color): self.root def insert(self, val): if (self.root is None): self.init_rbbst (val, RED) else: RBBST_Node(val, color) RED True BLACK False self. insertNode (self. root, val) def insertNode(self, current, val): (ii) def rotate_left (self, current) : (ii) def flip_colors (self, current) :
Exercise 1: BSTree operations For this exercise you'll implement three additional methods in the binary search tree data structure completed in class, so that you have an opportunity to practice both using the recursive pattern covered in class and navigating the binary tree structure. The methods you'll implement are: count_less_than: takes an argument x, and returns the number of elements in the tree with values less than x successor: takes an argument x, and returns the smallest value from the...
Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The natural way to solve this problem is to use recursion. The diagram below illustrates a recursive solution to the problem, which consists of three simple steps: Step1: Print the root node Step 3: Print the right sub-tree 10 Step 2: Print the...
python pls and noticed the output added ""
One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be represented using a nested list as follows [55, [24, [8, None, None], [51, [25, None, None], None]], [72, None, [78, None, None ]]] The nested list format always uses a list of length three to represent a binary tree. The first item in...
in PYTHON create a binary search tree that has the minimum possible height (i.e., is as balanced as possible). You're given the elements in sorted order. Here are some example inputs and the minimum possible height of the resulting BST: [0] 0 [-3, -2, -1] 1 [-3, 0, 1, 3, 4] 2 [-9, -6, -5, -3, -2, 1, 2, 4, 8] 3 [-19, -17, -15, -12, -11, -9, -7, -1, 1, 4, 5, 7, 8, 9, 11, 12, 13, 14, 18]...
Python question. How do i make this code work? im trying to print the value of the nodes using the level order trasversal class Node: def __init__(self, x): self.val = x self.left = None self.right = None class BinarySearchTree: def insertNode(self, value): self.root = self._insert(self.root, value) def levelOrderTraversal(self, root): if root is None: return [] result, current = [], [root] while current: next_level, vals = [], [] for node in current: vals.append(node.val) if node.left: next_level.append(node.left) if node.right: next_level.append(node.right) current...
[Python]
Construct Tree Using Inorder and Preorder
Given Preorder and Inorder traversal of a binary tree, create
the binary tree associated with the traversals.You just need to
construct the tree and return the root.
Note: Assume binary tree contains only unique elements.
Input format :
Line 1 : n (Total number of nodes in binary tree)
Line 2 : Pre order traversal
Line 3 : Inorder Traversal
Output Format :
Elements are printed level wise, each level in new line...
PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...
PYTHON 3 PLEASE FOLLOW INSTRUCTIONS COMPLETE CODE Problem : Complete the function predecessor() to take in an arbitrary node of a BST, and return the value of the predecessor of the given node. Code : class Node: def __init__(self, value): self.value = value self.left = None self.right = None def predecessor(node): # TODO