Question: class Node: # Implement a node of the binary search tree. # Constructor for a node with key and a given parent # parent can
class Node:
# Implement a node of the binary search tree.
# Constructor for a node with key and a given parent
# parent can be None for a root node.
def initself key, parent None:
self.key key
self.parent parent
self.left None # We will set left and right child to None
self.right None
# Make sure that the parent's leftright pointer
# will point to the newly created node.
if parent None:
if key parent.key:
assertparentleft None 'parent already has a left child unable to create node'
parent.left self
else:
assert key parent.key, 'key is same as parent.key. We do not allow duplicate keys in a BST since it breaks some of the algorithms.
assertparentright None 'parent already has a right child unable to create node'
parent.right self
# Utility function that keeps traversing left until it finds
# the leftmost descendant
def getleftmostdescendantself:
if self.left None:
return self.left.getleftmostdescendant
else:
return self
# TODO: Complete the search algorithm below
# You can call search recursively on left or right child
# as appropriate.
# If search succeeds: return a tuple True and the node in the tree
# with the key we are searching for.
# Also note that if the search fails to find the key
# you should return a tuple False and the node which would
# be the parent if we were to insert the key subsequently.
def searchself key:
if self.key key:
return True self
# your code here
elif key self.key and self.left:
return self.left.searchkey
elif key self.key and self.right:
return self.right.searchkey
else:
return False self
#TODO: Complete the insert algorithm below
# To insert first search for it and find out
# the parent whose child the currently inserted key will be
# Create a new node with that key and insert.
# return None if key already exists in the tree.
# return the new node corresponding to the inserted key otherwise.
def insertself key:
# your code here
found node self.searchkey
if found:
return None
else:
return Nodekey node
# TODO: Complete algorithm to compute height of the tree
# height of a node whose children are both None is defined
# to be
# height of any other node is maximum of the height
# of its children.
# Return a number that is th eheight.
def heightself:
# your code here
if self.left is None and self.right is None:
return
elif self.left is None:
return self.right.height
elif self.right is None:
return self.left.height
else:
The code above does not pass to delete with recursion error
# Testing deletion
t Node None
# insert the nodes in the list
lst
for elt in lst:
tinsertelt
# The tree should look like this
#
#
#
#
#
#
#
# Let us test the three deletion cases.
# case let's delete node
# node does not have left or right children.
tdelete # should have both children nil.
bn tsearch
assert not b 'Test A: deletion fails to delete node.
bn tsearch
assert b 'Test B failed: search does not work'
assert nleft None, 'Test C failed: Node was not properly deleted.
# Let us test deleting the node whose right child is none.
# n is still pointing to the node after deleting
# let us ensure that it's right child is
assert nright None, 'Test D failed: node should have right child
assert nright.key 'Test E failed: node should have right child
# Let's delete node
tdelete
b n tsearch
assert not b 'Test F: Deletion of node failed it still exists in the tree.
bn tsearch
assert nright None 'Test G failed: deletion of node not handled correctly'
assert nright.key f'Test H failed: deletion of node not handled correctly: nright.key
# Let's delete node in the tree.
# It should be replaced by
tdelete
b n tsearch
assert not b 'Test I: Deletion of node failed'
assert tright.key Test J: Replacement of node with successor failed.
assert tright.right.left None, Test K: replacement of node with successor failed you did not delete the successor leaf properly?
print All tests passed: points!
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
