Question: Programming Language: Python Consider the following binary tree How could we calculate the number of nodes of this tree i.e. the size of this data
Programming Language: Python

Consider the following binary tree How could we calculate the number of nodes of this tree i.e. the size of this data structure)? Hint: Use a recursive approach, i.e. if the tree is empty there is no node. If it's not empty the number of nodes is 1 (the root) plus the number of nodes of the left subtree and the number of nodes of the right subtree For this question, you must define a function called size0 which is passed a binary tree as an input, and which calculates the sum of all of the data values in the binary tree The BinaryTree class is provided to you - you do not need to write the code for the BinaryTree class. A complete listing of this class is given below 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.let. BinaryTree(new data) else t BinaryTree(new data) t.left = Selfleft self. left = t def insert right self, new _data) if self.rightNone: 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 Define the size0 function below: def size(my_tree): For example: Test Result a = Bina ryTree(100) | 1 print(size(a)) a = BinaryTree(1) a.insert_left a.insert_right(3) print(size(a))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
