Question: Chapter 8 - Programming Project: Implement the binary tree ADT using a linked structure as we developed in class, add the following methods: - def
Chapter Programming Project: Implement the binary tree ADT using a linked structure as we developed in class, add the following methods:
def countKself num: function that takes in num and counts number of times num appears in the binary tree
def equalself other: BinaryTree: function thats takes in another binary tree other as parameter and returns true if both binary trees are equal, otherwise function returns False.
Use the base code provided:
class BinaryTree:
class Node:
def initself e leftNone, rightNone:
self.left left
self.right right
self.element e
def initself:
self.root None
self.size
def isemptyself:
return self.root is None
def addrootself e:
if self.root:
return None
self.root self.Nodee
return self.root
def addleftself e p:
pleft self.Nodee
return pleft
def addrightself e p:
pright self.Nodee
return pright
def heightself:
return self.heightselfroot
def heightself v:
if not v:
return
x self.heightvleft
y self.heightvright
return maxx y
def countKself k:
# Write code here
# Pseudocode for countK function
Initialize a variable 'count' to
Create a helper function countK' that takes a node and a number as arguments.
If the node is None, return
If the node's element is equal to the given number, increment 'count' by
Recursively call countK' for the left and right children of the node.
Return the count.
Call the helper function countK' starting from the root of the tree.
Return the count.
def countKself p k:
# Write code here
def equalself other:
# Write code here
# Pseudocode for equal function
Create a helper function equal' that takes two nodes as arguments.
If both nodes are None, return True.
If one of the nodes is None and the other is not, return False.
If the elements of the two nodes are not equal, return False.
Recursively call equal' for the left children of both nodes.
Recursively call equal' for the right children of both nodes.
If both recursive calls return True, return True. Otherwise, return False.
Call the helper function equal' with the root nodes of both trees.
Return the result.
def equalself p p:
# Write code here
if namemain:
bt BinaryTree
root btaddroot
# Create a tree with height and multiple s in it
leftchild btaddleft root
rightchild btaddright root
l btaddleft leftchild
r btaddright leftchild
l btaddleft l
r btaddright l
l btaddleft l
r btaddright l
btaddleft l
btaddright l
bt BinaryTree
root btaddroot
# Create another identical tree
leftchild btaddleft root
rightchild btaddright root
l btaddleft leftchild
r btaddright leftchild
l btaddleft l
r btaddright l
l btaddleft l
r btaddright l
btaddleft l
btaddright l
printHeight of bt: btheight
printCount of in bt: btcountK
printAre bt and bt equal?", btequalbt
# Making bt different from bt
btaddleft l
printAre bt and bt equal after modification?", btequalbt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
