Algorithms
May 5, 2024
Northeastern University CS5800 Algorithms, Spring 2024 Instructor: Hyonho Lee Assignment 5 (Total Marks: 50 pts) Due Date: March 18, 2024, 6pm Name: Student Number: Collaborators: I, , read and understood Northeastern University’s Academic Integrity Policy (https://osccr.sites.northeastern.edu/academic-integrity-policy/). 1. (40 pts) An AVL tree is a binary search tree such that, for each node x, the height of the left and right subtrees of x differ by at most 1. (This question is from CLRS Problem 13-3 (AVL trees).) The below Python program is a part of AVL tree implementation. avl insert() function inserts a key to the AVL tree and avl delete() function deletes a key from the AVL tree. Note that avl insert() calls avl fixup() function at the end, but avl fixup() function is not implemented yet. avl delete() function is not implemented either. Your task is to complete these two functions, so that avl insert() and avl delete() correctly inserts and deletes a given key, respectively, while maintaining the AVL tree property after the operation. You may add more helper functions. You also need to test your implementation. Below codes contain sample functions for testing. You may add more test cases. class Node: def __init__(self, key: int, p = None, l = None, r = None, h = 1): self.left = l self.right = r self.parent = p self.value = key self.height = h 1 class Tree: def __init__(self, key): self.root = Node(key) def left_rotate(T: Tree, x: Node): y = x.right x.right = y.left if y.left: y.left.parent = x y.parent = x.parent if not x.parent: T.root = y elif x == x.parent.left: x.parent.left = y else: x.parent.right = y y.left = x x.parent = y def right_rotate(T: Tree, y: Node): x = y.left y.left = x.right if x.right: x.right.parent = y x.parent = y.parent if not y.parent: T.root = x elif y == y.parent.left: y.parent.left = x else: y.parent.right = x x.right = y y.parent = x def avl_insert(T: Tree, key: int): cur, cur_p = T.root, None while cur: cur_p = cur if key == cur.value: print(“The given key already exists:”, key) return elif key < cur.value: cur = cur.left else: 2 cur = cur.right new_node = Node(key, cur_p) if not cur_p: T.root = new_node elif key < cur_p.value: cur_p.left = new_node else: cur_p.right = new_node avl_fixup(T, new_node) def avl_fixup(T: Tree, z: Node):
Trust your assignments to an essay writing service with the fastest delivery time and fully original content.