Question: 4 . Write a method is _ balanced in LinkedBST which determines if the tree is balanced or not. This method should traverse the tree
Write a method isbalanced in LinkedBST which
determines if the tree is balanced or not. This method should
traverse the tree and check to see if any Node is the root of
an unbalanced subtree. If you find an unbalanced Node,
return False. If the whole tree is traversed and no
unbalanced Nodes are found, return True.
Hint:You can reuse some of your code from breadth in
this method to traverse the tree. Or if you prefer, you can
reuse some of the code from inorder.
class AbstractCollection:
def initself sourcecollectionNone:
Sets the initial state of self, which includes the contents of sourcecollection
self.size
if sourcecollection:
for item in sourcecollection:
self.additem
def lenself:
Return the number of items in this collection
return self.size
def isemptyself:
Returns True if the collection is empty and False otherwise
return lenself
def addself other:
Overloads the operator. A new collection is created with everything from self and other.
result typeselfself
for item in other:
result.additem
return result
class Node:
Represents a Node in a singlylinked list
def initself data, nextNone:
By default, this Node will not link to another Node
self.data data
self.next next
class BinaryNode:
def initself data, leftNone, rightNone:
By default, this Node will not have any children
self.data data
self.left left
self.right right
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
