Question: ****Python**** 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)

****Python**** 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

-----------------------------------------------

Write a function named get_height which calculates the height in the binary tree recursively. For example, the following code fragment:

get_height(my_tree) 

prints 3.

You may assume that the tree is given as follow:

This is what i have got:

def get_height(t): if t == None: return 0 return (max(get_height(t.left), get_height(t.right)) + 1)

However, the answers should be like this:

print(get_height(my_tree)) gives 3

print(get_height(my_tree1)) gives 2

print(get_height(BinaryTree(41))) gives 0

Thank you!

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!