Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

himport random from queue _ and _ stack import Queue, Stack from bst import BSTNode, BST class AVLNode ( BSTNode ) :

himport random
from queue_and_stack import Queue, Stack
from bst import BSTNode, BST
class AVLNode(BSTNode):
"""
AVL Tree Node class. Inherits from BSTNode
DO NOT CHANGE THIS CLASS IN ANY WAY
"""
def __init__(self, value: object)-> None:
"""
Initialize a new AVL node
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
# call __init__() from parent class
super().__init__(value)
# new variables needed for AVL
self.parent = None
self.height =0
def __str__(self)-> str:
"""
Override string method
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return 'AVL Node: {}'.format(self.value)
class AVL(BST):
"""
AVL Tree class. Inherits from BST
"""
def __init__(self, start_tree=None)-> None:
"""
Initialize a new AVL Tree
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
# call __init__() from parent class
super().__init__(start_tree)
def __str__(self)-> str:
"""
Override string method
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
values =[]
super()._str_helper(self._root, values)
return "AVL pre-order {"+",".join(values)+"}"
def is_valid_avl(self)-> bool:
"""
Perform pre-order 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.push(self._root)
while not stack.is_empty():
node = stack.pop()
if node:
# check for correct height (relative to children)
left = node.left.height if node.left else -1
right = node.right.height if node.right else -1
if node.height !=1+ max(left, right):
return False
if node.parent:
# parent and child pointers are in sync
if node.value < node.parent.value:
check_node = node.parent.left
else:
check_node = node.parent.right
if check_node != node:
return False
else:
# NULL parent is only allowed on the root of the tree
if node != self._root:
return False
stack.push(node.right)
stack.push(node.left)
return True
# ------------------------------------------------------------------ #
def add(self, value: object)-> None:
"""
TODO: Write your implementation
"""
pass
def remove(self, 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._remove_no_subtrees(parent, node) #
# You need to override the _remove_two_subtrees() 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 _remove_two_subtrees(self, remove_parent: AVLNode, remove_node: 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 _balance_factor(self, node: AVLNode)-> int:
"""
TODO: Write your implementation
"""
pass
def _get_height(self, node: AVLNode)-> int:
"""
TODO: Write your implementation
"""
pass
def _rotate_left(self, node: AVLNode)-> AVLNode:
"""
TODO: Write your implementation
"""
pass
def _rotate_right(self, node: AVLNode)-> AVLNode:
"""
TODO: Write your implementation
"""
pass
def _update_height(self, node: AVLNode)-> None:
"""
TODO: Write your implementation here
def _rebalance(self, node: AVLNode)-> None:
"""
pass
de
""
TODO: Writ

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Postgresql 16 Administration Cookbook Solve Real World Database Administration Challenges With 180+ Practical Recipes And Best Practices

Authors: Gianni Ciolli ,Boriss Mejias ,Jimmy Angelakos ,Vibhor Kumar ,Simon Riggs

1st Edition

1835460585, 978-1835460580

More Books

Students also viewed these Databases questions

Question

1. Who are the tourism sector stakeholders within the destination?

Answered: 1 week ago