Question: Can someone help me with this PYTHON code! In this notebook you will complete the following implementation of the balanced (AVL) binary search tree. Note

Can someone help me with this PYTHON code!

In this notebook you will complete the following implementation of the balanced (AVL) binary search tree. Note that you should not be implementing the map-based API described in the plain (unbalanced) BSTree notebook i.e., nodes in the AVLTree will only contain a single value.

In [ ]:

-

x 
class AVLTree: 
 class Node: 
 def __init__(self, val, left=None, right=None): 
 self.val = val 
 self.left = left 
 self.right = right 
 
 def rotate_right(self): 
 n = self.left 
 self.val, n.val = n.val, self.val 
 self.left, n.left, self.right, n.right = n.left, n.right, n, self.right 
 
 def rotate_left(self): 
 # YOUR CODE HERE 
 raise NotImplementedError() 
 
 @staticmethod 
 def height(n): 
 if not n: 
 return 0 
 else: 
 return max(1+AVLTree.Node.height(n.left), 1+AVLTree.Node.height(n.right)) 
 
 def __init__(self): 
 self.size = 0 
 self.root = None 
 
 @staticmethod 
 def rebalance(t): 
 # YOUR CODE HERE 
 raise NotImplementedError() 
 
 def add(self, val): 
 assert(val not in self) 
 # YOUR CODE HERE 
 raise NotImplementedError() 
 
 def __delitem__(self, val): 
 assert(val in self) 
 # YOUR CODE HERE 
 raise NotImplementedError() 
 
 def __contains__(self, val): 
 def contains_rec(node): 
 if not node: 
 return False 
 elif val < node.val: 
 return contains_rec(node.left) 
 elif val > node.val: 
 return contains_rec(node.right) 
 else: 
 return True 
 return contains_rec(self.root) 
 
 def __len__(self): 
 return self.size 
 
 def __iter__(self): 
 def iter_rec(node): 
 if node: 
 yield from iter_rec(node.left) 
 yield node.val 
 yield from iter_rec(node.right) 
 yield from iter_rec(self.root) 
 
 def pprint(self, width=64): 
 """Attempts to pretty-print this tree's contents.""" 
 height = self.height() 
 nodes = [(self.root, 0)] 
 prev_level = 0 
 repr_str = '' 
 while nodes: 
 n,level = nodes.pop(0) 
 if prev_level != level: 
 prev_level = level 
 repr_str += ' ' 
 if not n: 
 if level < height-1: 
 nodes.extend([(None, level+1), (None, level+1)]) 
 repr_str += '{val:^{width}}'.format(val='-', width=width//2**level) 
 elif n: 
 if n.left or level < height-1: 
 nodes.append((n.left, level+1)) 
 if n.right or level < height-1: 
 nodes.append((n.right, level+1)) 
 repr_str += '{val:^{width}}'.format(val=n.val, width=width//2**level) 
 print(repr_str) 
 
 def height(self): 
 """Returns the height of the longest branch of the tree.""" 
 def height_rec(t): 
 if not t: 
 return 0 
 else: 
 return max(1+height_rec(t.left), 1+height_rec(t.right)) 
 return height_rec(self.root) 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!