Question: class Node(object): def __init__(self, value = None): self.value = value self.left = None self.right= None def insert_node(self, new_value): if new_value if self.left == None: self.left

class Node(object):

def __init__(self, value = None):

self.value = value

self.left = None

self.right= None

def insert_node(self, new_value):

if new_value

if self.left == None:

self.left = Node(new_value)

else:

# This is the recursive function call

self.left.insert_node(new_value)

else: #case when new_value > self.value:

if self.right == None:

self.right = Node(new_value)

else:

self.right.insert_node(new_value)

def construct_binary_tree(self, L):

self.value = L[0]

for ele in L[1:]:

self.insert_node(ele)

def print_tree(self):

print(self.value)

if self.left == None:

print("left: none!")

else:

print("left: ", self.left.value)

if self.right == None:

print("right: none!")

else:

print("right: ", self.right.value)

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

Question 2: Copy and paste the Node class and all its methods (from the binary tree sample code on Blackboard). Define a new method question_2 that takes as input a binary tree self and then stores the information in the binary tree in a text file called q2.txt, storing the nodes in the same order as pre-order traversal. The formatting of the file should conform to the following format. Suppose that new_tree is a Node object that contains the following tree: 6 3 7 2 5 9 4 Then, the grader should be able to run your code and type new-tree.question 2() and your method should create a file q2.txt that looks like the following. Binary Tree PreOrder Node: 6 Node: 3 Node: 2 Node: 5 Node: 4 Node: 7 Node: 9 Hint: you will want to modify the pre-order traversal method that is already in the sample code. You should modify it so that instead of printing each node, you save each node somehow (perhaps in a list or something...). Then only at the end try to write the data to the file. If needed, add more inputs to allow for data to be passed from one recursive function call to another

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!