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)
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 anotherStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started