Complete the implementation of the binary search tree started in class. You know you are done
when the main function runs correctly with all commented lines uncommented, and printing
shows the expected behavior of a BST.
CODE:
# TODO: implement inorder
# TODO: implement find
# TODO: implement maximum, minimum
# TODO: implement successor, predecessor
# TODO: implement delete
def main():
bst = Node(3, None)
for k in [5, 7, 4, 5, 6]:
bst.insert(k)
# print(bst.inorder())
# for k in [4, 3, 6, 2, 1, 7]:
# print(\"find {}: {}\".format(k, bst.find(k) != None))
# for k in [4,3, 2]:
# n = bst.find(k)
# if n != None:
# s = n.successor()
# print(\"successor of {}: {}\".format(k, s.key if s != None else None))
# s = n.predecessor()
# print(\"predecessor of {}: {}\".format(k, s.key if s != None else None))
# s = n.delete()
# print(\"deleted {}\".format(s.key))
# print(bst.inorder())
class Node:
def __init__(self, key, parent):
self.key = key # self is Python for \"this\"
self.parent = parent
self.left = None
self.right = None
def inorder(self):
\'\'\'returns list of the keys in an inorder traversal of the tree rooted at self\'\'\'
# TODO: replace \"pass\" keyword with actual code
# Hint: li.append(value) appends a value to list li
# Hint: li = list1 + list2 returns the concatnenation of list1 and list2
# Hint: use dot notation to call method: inorder(node) ---> node.inorder()
# To test: uncomment line 10 and click \"Run\"
li = [] # empty list
if self.left is not None:
pass
pass
if self.right is not None:
pass
return li
def insert(self, key):
\'\'\'inserts a new node with given key into subtree rooted at self node\'\'\'
if key < self.key:
if self.left is None: # None is Python for null, \"is\" = \"==\", \"is not\" = \"!=\"
self.left = Node(key, self)
else:
self.left.insert(key)
else:
if self.right is None:
self.right = Node(key, self)
else:
self.right.insert(key)
def find(self, key):
\'\'\'returns a node in the subtree rooted at self with this key or
None if no such key exists\'\'\'
# Hint: similar to insert()
# Test: uncomment lines 11 and 12, click \"Run\"
pass
def minimum(self):
\'\'\'returns the node with minimum key in the subtree rooted at self\'\'\'
pass
def maximum(self):
\'\'\'returns the node with maximum key in the is subtree rooted at self\'\'\'
pass
def successor(self):
\'\'\'returns successor of the self node or None if it does not have one\'\'\'
# TODO: fill the if statement
if self.right is not None:
pass
c, p = self, self.parent
while p is not None and c is p.right:
c, p = p, p.parent
return p
def predecessor(self):
\'\'\'returns the predecessor of the self node or None if it does not have one\'\'\'
pass
def delete(self):
\'\'\'deletes the self node from the tree and returns that node\'\'\'
parent, left, right = self.parent, self.left, self.right
# case 0: no children
if left is None and right is None:
if parent is not None:
if self is parent.left:
parent.left = None
else:
parent.right = None
return self
# case 1: one or two children, no pa.
Complete the implementation of the binary search tree started in cla.pdf
1. Complete the implementation of the binary search tree started in class. You know you are done
when the main function runs correctly with all commented lines uncommented, and printing
shows the expected behavior of a BST.
CODE:
# TODO: implement inorder
# TODO: implement find
# TODO: implement maximum, minimum
# TODO: implement successor, predecessor
# TODO: implement delete
def main():
bst = Node(3, None)
for k in [5, 7, 4, 5, 6]:
bst.insert(k)
# print(bst.inorder())
# for k in [4, 3, 6, 2, 1, 7]:
# print("find {}: {}".format(k, bst.find(k) != None))
# for k in [4,3, 2]:
# n = bst.find(k)
# if n != None:
# s = n.successor()
# print("successor of {}: {}".format(k, s.key if s != None else None))
# s = n.predecessor()
# print("predecessor of {}: {}".format(k, s.key if s != None else None))
# s = n.delete()
# print("deleted {}".format(s.key))
# print(bst.inorder())
class Node:
def __init__(self, key, parent):
self.key = key # self is Python for "this"
self.parent = parent
self.left = None
self.right = None
def inorder(self):
2. '''returns list of the keys in an inorder traversal of the tree rooted at self'''
# TODO: replace "pass" keyword with actual code
# Hint: li.append(value) appends a value to list li
# Hint: li = list1 + list2 returns the concatnenation of list1 and list2
# Hint: use dot notation to call method: inorder(node) ---> node.inorder()
# To test: uncomment line 10 and click "Run"
li = [] # empty list
if self.left is not None:
pass
pass
if self.right is not None:
pass
return li
def insert(self, key):
'''inserts a new node with given key into subtree rooted at self node'''
if key < self.key:
if self.left is None: # None is Python for null, "is" = "==", "is not" = "!="
self.left = Node(key, self)
else:
self.left.insert(key)
else:
if self.right is None:
self.right = Node(key, self)
else:
self.right.insert(key)
def find(self, key):
'''returns a node in the subtree rooted at self with this key or
None if no such key exists'''
# Hint: similar to insert()
# Test: uncomment lines 11 and 12, click "Run"
pass
def minimum(self):
'''returns the node with minimum key in the subtree rooted at self'''
3. pass
def maximum(self):
'''returns the node with maximum key in the is subtree rooted at self'''
pass
def successor(self):
'''returns successor of the self node or None if it does not have one'''
# TODO: fill the if statement
if self.right is not None:
pass
c, p = self, self.parent
while p is not None and c is p.right:
c, p = p, p.parent
return p
def predecessor(self):
'''returns the predecessor of the self node or None if it does not have one'''
pass
def delete(self):
'''deletes the self node from the tree and returns that node'''
parent, left, right = self.parent, self.left, self.right
# case 0: no children
if left is None and right is None:
if parent is not None:
if self is parent.left:
parent.left = None
else:
parent.right = None
return self
# case 1: one or two children, no parent
elif parent is None:
s = self.predecessor() or self.successor() # whichever is not None
self.key, s.key = s.key, self.key
return s.delete()
4. # case 2: one child, one parent
# Hint: similar to case 0
elif left == None or right == None:
pass
# case 3: 2 children, one parent
# Hint: similar to case 1
else:
pass
main()
Solution
# TODO: implement inorder
# TODO: implement find
# TODO: implement maximum, minimum
# TODO: implement successor, predecessor
# TODO: implement delete
def main():
bst = Node(3, None)
for k in [5, 7, 4, 5, 6]:
bst.insert(k)
print( bst.inorder())
# print(bst.inorder())
for k in [4, 3, 6, 2, 1, 7]:
print("find {0}: {1}".format(k, bst.find(k) != None))
for k in [4,3, 2]:
n = bst.find(k)
if n != None:
s = n.successor()
print("successor of {}: {}".format(k, s.key if s != None else None))
s = n.predecessor()
print("predecessor of {}: {}".format(k, s.key if s != None else None))
s = n.delete()
print("deleted {}".format(s.key))
print(bst.inorder())
5. list1=[]
list2=[]
li = []
class Node:
def __init__(self, key, parent):
self.key = key # self is Python for "this"
self.parent = parent
self.left = None
self.right = None
def inorder(self):
#Node.inorder1(self.parent)
'''returns list of the keys in an inorder traversal of the tree rooted at self'''
# TODO: replace "pass" keyword with actual code
# Hint: li.append(value) appends a value to list li
# Hint: li = list1 + list2 returns the concatnenation of list1 and list2
# Hint: use dot notation to call method: inorder(node) ---> node.inorder()
# To test: uncomment line 10 and click "Run"
# empty list
if self.left is not None:
list1.append(self.left.key)
self.left.inorder()
if self.right is not None:
list2.append(self.right.key)
self.right.inorder()
li = list1+list2
return li
def insert(self,key):
'''inserts a new node with given key into subtree rooted at self node'''
if key < self.key:
if self.left is None: # None is Python for null, "is" = "==", "is not" = "!="
self.left = Node(key, self)
else:
self.left.insert(key)
6. else:
if self.right is None:
self.right = Node(key, self)
else:
self.right.insert(key)
def find(self, key):
#returns a node in the subtree rooted at self with this key or
# None if no such key exists'''
# Hint: similar to insert()
# Test: uncomment lines 11 and 12, click "Run"
if key < self.key:
if self.left is None: # None is Python for null, "is" = "==", "is not" = "!="
return True
else:
return True
elif self.right is None:
return True
else:
return False
def minimum(self):
'''returns the node with minimum key in the subtree rooted at self'''
current = self
# loop down to find the lefmost leaf
while(current.left is not None):
current = current.left
return current.data
def maximum(self):
'''returns the node with maximum key in the is subtree rooted at self'''
current = self
# loop down to find the rightmost right
while(current.right is not None):
current = current.right
return current.data
def successor(self):
7. '''returns successor of the self node or None if it does not have one'''
# TODO: fill the if statement
if self.right is not None:
return self.right.minimum()
c, p = self, self.parent
while p is not None and c is p.right:
c, p = p, p.parent
return p
def predecessor(self):
'''returns the predecessor of the self node or None if it does not have one'''
pass
def delete(self):
'''deletes the self node from the tree and returns that node'''
parent, left, right = self.parent, self.left, self.right
# case 0: no children
if left is None and right is None:
if parent is not None:
if self is parent.left:
parent.left = None
else:
parent.right = None
return self
# case 1: one or two children, no parent
elif parent is None:
s = self.predecessor() or self.successor() # whichever is not None
self.key, s.key = s.key, self.key
return s.delete()
# case 2: one child, one parent
# Hint: similar to case 0
elif left == None or right == None:
pass
# case 3: 2 children, one parent
# Hint: similar to case 1
else: