Question: In Python, Link to Class: https://pastebin.com/muMtz9PJ Consider the following binary tree: How could we calculate the size of the tree i.e. the total number of
In Python,
Link to Class: https://pastebin.com/muMtz9PJ

Consider the following binary tree: How could we calculate the size of the tree i.e. the total number of nodes in the tree)? Well, if we knew the number of nodes in the left sub-tree, and the number of nodes in the right sub-tree, then we would just need to add 1 to these values to find the number of nodes in the overall tree. The following diagram ilustrates this: Number of nodes in tree For this question, you must define a function called tree size0 which is passed a binary tree as input, and which counts the number of nodes in the binary tree The Binary Tree class is provided to you - you do not need to write the code for the Binary Tree class. class is given below A complete listing of this class BinaryTree definit..(self, data): self.data-data self.leftNone self.right None def insert left(self, new_data) if self.left 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.rightNone: self.right BinaryTreeCnew_data) else: t = Bina ryTree(new-data) t.right self.right self.right t def getleft(self def get.right(self def set data(self, data): def get_data(self): return self.left return self.right self.data data return self.data Your tree size) function will, of course, be recursive (because it will call the tree size) function to calculate the number of nodes in the left and right sub-trees). Define the tree size0 function below def tree size(t): For example: Test Result a BinaryTree(100) 1 print(tree size(a)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
