Question
class BinaryTree: def __init__(self, data): self.data = data self.left = None self.right = None def insert_left(self, new_data): if self.left == None: self.left = BinaryTree(new_data) else:
class BinaryTree: def __init__(self, data): self.data = data self.left = None self.right = None
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
def get_left(self): return self.left
def get_right(self): return self.right
def set_data(self, data): self.data = data
def get_data(self): return self.data
09.02.2029 Lab17erry Trees capac 101 Quector 5 Inspire Mark 0.00 out a 7.00 This question uses the same Binary Tree class as the previous questions. You can assume this class is available to you. Definition of the height of tree: the maximum distance of any node to the root of the tree, i.c., the longest path from any leaf node to the root. If a tree has only one node (the root), the height is zero. The height of an empty tree is not defined, For example, consider this binary tree: A Oo oo The height of this binary tree is 3. The red arrows show one path from a leaf node to the rool, and this is the longest such path in the tree. Define the get_tree_height( recursive function which is passed a binary trec as a parameter and returns the height of the tree. If you know the height of the left sub-tree, and you know the height of the right sub-tree, how will you solve the problem? The overall height of the tree will be one more than the maximum of the height of the left sub-tree and the height of the right sub tree. HINT: Think carefully about the base case. If you consider (b_tree == None) as the only basc casc, you may find your function retums a value that is one bigger than it should be A leaf node should also be a base case (a node on its own has a height of 0). The function header is: def get_tree_height(b_tree): For example: Test Result a = BinaryTree(188) print(get_tree_height(a)) a = BinaryTree(1) a.insert_left(2) a.insert_left(3) a.insert_left(4) a.insert_left(5) print(get_tree_height(a)) Answer: (penalty regime: 0.0.5, 10, 15, 20, 25, 30, 35, 40, 45, 50 %) m pimeIRE-A0811-464Step 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