***Please do not just report answers from other questions or websites. They do not work, it will be downvoted. Doing tha

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

***Please do not just report answers from other questions or websites. They do not work, it will be downvoted. Doing tha

Post by answerhappygod »

***Please do not just report answers from other
questions or websites. They do not work, it will be downvoted.
Doing that is not helpful****
add(self, value: object) -> None:
This method adds a new value to the tree while maintaining its AVL
property. Duplicate
values are not allowed. If the value is already in the tree, the
method should not change
the tree. It must be implemented with O(log N) runtime
complexity.
remove(self, value: object) -> bool:
This method should remove the value from the AVL tree. The method
returns True if the
value is removed from the AVL Tree; otherwise, it returns False. It
must be implemented
with O(log N) runtime complexity.
Errors I get from Tests:
***** Code is as follows:
queue_and_stack.py
bst.py
import random
from queue_and_stack import Queue, Stack
class BSTNode:
"""
Binary Search Tree Node class
DO NOT CHANGE THIS CLASS IN ANY WAY
"""
def __init__(self, value: object) ->
None:
"""
Initialize a new BST node
DO NOT CHANGE THIS METHOD IN ANY
WAY
"""
self.value = value # to store
node's data
self.left = None #
pointer to root of left subtree
self.right = None #
pointer to root of right subtree
def __str__(self) -> str:
"""
Override string method
DO NOT CHANGE THIS METHOD IN ANY
WAY
"""
return 'BST Node:
{}'.format(self.value)
class BST:
"""
Binary Search Tree class
"""
def __init__(self, start_tree=None) ->
None:
"""
Initialize new Binary Search Tree
DO NOT CHANGE THIS METHOD IN ANY
WAY
"""
self._root = None
# populate BST with initial values
(if provided)
# before using this feature, implement
add() method
if start_tree is not None:
for value in
start_tree:

self.add(value)
def __str__(self) -> str:
"""
Return content of BST in human-readable
form using pre-order traversal
DO NOT CHANGE THIS METHOD IN ANY
WAY
"""
values = []
self._str_helper(self._root,
values)
return "BST pre-order { " + ",
".join(values) + " }"
def _str_helper(self, node: BSTNode, values: [])
-> None:
"""
Helper method for __str__. Does
pre-order tree traversal
DO NOT CHANGE THIS METHOD IN ANY
WAY
"""
if not node:
return
values.append(str(node.value))
self._str_helper(node.left,
values)
self._str_helper(node.right,
values)
def get_root(self) -> BSTNode:
"""
Return root of tree, or None if
empty
DO NOT CHANGE THIS METHOD IN ANY
WAY
"""
return self._root
def is_valid_bst(self) -> bool:
"""
Perform pre-order traversal of the
tree.
DO NOT CHANGE THIS METHOD IN ANY
WAY
"""
stack = Stack()
stack.push(self._root)
while not stack.is_empty():
node = stack.pop()
if node:
if
node.left and node.left.value >= node.value:

return False
if
node.right and node.right.value < node.value:

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
def contains(self, value: object) ->
bool:
"""
TODO: Write your implementation
"""
pass
def inorder_traversal(self) -> Queue:
"""
TODO: Write your implementation
"""
pass
def find_min(self) -> object:
"""
TODO: Write your implementation
"""
pass
def find_max(self) -> object:
"""
TODO: Write your implementation
"""
pass
def is_empty(self) -> bool:
"""
TODO: Write your implementation
"""
pass
def make_empty(self) -> None:
"""
TODO: Write your implementation
"""
pass
*****avl.py
import 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:
"""
Return content of AVL in human-readable
form
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.
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
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
"""
pass
def _rebalance(self, node: AVLNode) ->
None:
"""
TODO: Write your implementation
"""
pass
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply