Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me with this PYTHON code! In this notebook you will modify the

ID: 3853819 • Letter: C

Question

Can someone help me with this PYTHON code!

In this notebook you will modify the binary search tree implementation completed in class so that it can be used as a mapping structure. The Node class will be updated so as to hold separate key and value attributes (instead of a single value, as it currently does), and instead of the add method, you should implement the __getitem__ and __setitem__ methods in order to associate keys and values. __delitem__, __contains__, and __iter__ will also need to be updated, to perform key-based removal, search, and iteration. Finally, the keys, values, and items methods will return iterators that allow the keys, values, and key/value tuples of the tree (all sorted in order of their associated keys) to be traversed.

If __setitem__ is called with an existing key, the method will simply locate the associated node and update its value with the newly provided value (as you would expect a mapping structure to do). If either __getitem__ or __delitem__ are called with a key that does not exist in the tree, a KeyError should be raised.

The API described above will allow the tree to be used as follows:

The expected output of the above follows:

The following BSTree class contains an updated Node, and stubs for the methods you are to implement. The first few simple test cases beneath the class definition should help clarify the required behavior.

In [ ]:

Explanation / Answer

class BSTree: class Node: def __init__(self, key, val, left=None, right=None): self.key = key self.val = val self.left = left self.right = right def __init__(self): self.size = 0 self.root = None def __getitem__(self, key): def get_rec(node): if not node: raise KeyError elif key node.key: return get_rec(node.right) elif key == node.key: return node.val else: raise KeyError return get_rec(self.root) def __setitem__(self, key, val): def set_rec(node): if not node: return BSTree.Node(key, val) elif key == node.key: node.val = val return node elif key node.key: node.right = set_rec(node.right) return node self.root = set_rec(self.root) self.size += 1 def __delitem__(self, key): def delitem_rec(node): if node.key key: node.left = delitem_rec(node.left) return node elif node.key == key: if not node.left and not node.right: return None elif node.left and not node.right: return node.left elif not node.left and node.right: return node.right else: t = node.left if not t.right: node.left = t.left node.val = t.val node.key = t.key else: n = t while n.right.right: n = n.right t = n.right n.right = t.left node.val = t.val node.key = t.key return node else: raise KeyError self.root = delitem_rec(self.root) self.size -= 1 def __contains__(self, key): def find(t): if not t: return False elif t.key == key: return True elif t.key > key: return find(t.left) elif t.key