Answered step by step
Verified Expert Solution
Question
1 Approved Answer
himport random from queue _ and _ stack import Queue, Stack from bst import BSTNode, BST class AVLNode ( BSTNode ) :
himport random from queueandstack import Queue, Stack from bst import BSTNode, BST class AVLNodeBSTNode: AVL Tree Node class. Inherits from BSTNode DO NOT CHANGE THIS CLASS IN ANY WAY def initself value: object None: Initialize a new AVL node DO NOT CHANGE THIS METHOD IN ANY WAY # call init from parent class superinitvalue # new variables needed for AVL self.parent None self.height def strself str: Override string method DO NOT CHANGE THIS METHOD IN ANY WAY return 'AVL Node: formatselfvalue class AVLBST: AVL Tree class. Inherits from BST def initself starttreeNone None: Initialize a new AVL Tree DO NOT CHANGE THIS METHOD IN ANY WAY # call init from parent class superinitstarttree def strself str: Override string method DO NOT CHANGE THIS METHOD IN ANY WAY values superstrhelperselfroot, values return "AVL preorder joinvalues def isvalidavlself bool: Perform preorder traversal of the tree. Return False if there are any problems with attributes of any of the nodes in the tree. This is intended to be a troubleshooting 'helper' method to help find any inconsistencies in the tree after the add or remove operations. Review the code to understand what this method is checking and how it determines whether the AVL tree is correct. DO NOT CHANGE THIS METHOD IN ANY WAY stack Stack stack.pushselfroot while not stack.isempty: node stack.pop if node: # check for correct height relative to children left node.left.height if node.left else right node.right.height if node.right else if node.height maxleft right: return False if node.parent: # parent and child pointers are in sync if node.value node.parent.value: checknode node.parent.left else: checknode node.parent.right if checknode node: return False else: # NULL parent is only allowed on the root of the tree if node self.root: return False stack.pushnoderight stack.pushnodeleft return True # # def addself value: object None: TODO: Write your implementation pass def removeself value: object bool: TODO: Write your implementation pass # Experiment and see if you can use the optional # # subtree removal methods defined in the BST here in the AVL. # # Call normally using self self.removenosubtreesparent node # # You need to override the removetwosubtrees method in any case. # # Remove these comments. # # Remove these method stubs if you decide not to use them. # # Change this method in any way you'd like. # def removetwosubtreesself removeparent: AVLNode, removenode: AVLNode AVLNode: TODO: Write your implementation pass # It's highly recommended to implement # # the following methods for balancing the AVL Tree. # # Remove these comments. # # Remove these method stubs if you decide not to use them. # # Change these methods in any way you'd like. # def balancefactorself node: AVLNode int: TODO: Write your implementation pass def getheightself node: AVLNode int: TODO: Write your implementation pass def rotateleftself node: AVLNode AVLNode: TODO: Write your implementation pass def rotaterightself node: AVLNode AVLNode: TODO: Write your implementation pass def updateheightself node: AVLNode None: TODO: Write your implementation here def rebalanceself node: AVLNode None: pass de TODO: Writ
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started