Question: A nested list [55, [24, [8, None, None], [51, [25, None, None], None]], [72, None, [78, None, None]]]. The nested list format always uses a
A nested list [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 the list is the data value of the root, the second item in the list is the left subtree (this may be None if the left subtree is empty, or it may be a nested list) and the third item in the list is the right subtree (this may be None if the right subtree is empty, or it may be a nested list) A flat list [None, 55, 24, 72, 8, 51, None, 78, None, None, 25]. The flat list format always begins with the value None, so that the data value of the root is stored in index position 1. For any node at index position i, the left child is stored at index position 2*i, and the right child is stored at index position 2*i+1. A value of None in the list means there is no child at the corresponding index position. Task) Define a function, convert_flat_to_nested_list() that will take a list input representing a flat list binary tree. This function should output a list of the same tree in nested list format Example: flat_list = [None, 10, 5, 15, None, None, 11, 22] nested_list = convert_flat_to_nested_list(flat_list) print(nested_list) output: [10, [5, None, None], [15, [11, None, None], [22, None, None]]] Hint: Completing these tasks will be easier if you use the BinaryTree class to create BinaryTree objects representing the list. You can do this by writing a recursive method to move through the flat list and create the necessary BinaryTree nodes. After creating the BinaryTree nodes you could write another recursive function to convert the tree into the nested list format. Some empty functions have been included which may make this task easier to complete. You do not have to implement these if using a different strategy. (convert_flat_to_nested_list()
class BinaryTree: def __init__(self, data): self.data = data self.left = None self.right = None
def get_left(self): return self.left
def get_right(self): return self.right
def get_data(self): return self.data
def set_data(self, data): self.data = data
##You may choose to implement this function which adds a ##BinaryTree to the left instead of any data def insert_tree_left(self, left_tree): pass
##You may choose to implement this function which adds a ##BinaryTree to the right instead of any data def insert_tree_right(self, right_tree): pass def insert_left(self, new_data): if self.left == None: self.left = BinaryTree(new_data) else: t = BinaryTree(new_data) t.left = self.left self.left = t
def insert_right(self, new_data): if self.right == None: self.right = BinaryTree(new_data) else: t = BinaryTree(new_data) t.right = self.right self.right = t
##This function should take in a list in flat list format and ##return a list in nested list format def convert_flat_to_nested_list(flat_list): pass
##You may choose to implement the following recursive method to ##turn a flat list into a BinaryTree structure def convert_flat_to_tree(flat_list, index): pass
##You may choose to implement the following recursive method to ##turn a BinaryTree into a nested list format def convert_tree_to_nested(tree): pass
##test implementation flat_list = [None, 10, 5, 15, None, None, 11, 22] nested_list = convert_flat_to_nested_list(flat_list) print(nested_list)
must be done in python
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
