Question: PYTHON 3 - - AVL tree - please do the add ( ) function. Existing skeleton code cannot be altered, but helper methods can be

PYTHON 3-- AVL tree- please do the add() function. Existing skeleton code cannot be altered, but helper methods can be added. NO BUILT-IN DATA STRUCTURES CAN BE USED. Instructions for add() are attached along with skeleton code. Please make sure that the given test code works.
add(self, value: object)-> None:
This method adds a new value to the tree while maintaining its AVL property. Duplicate
valu
Skeleton Code:
class Stack:
"""
Class implementing STACK ADT.
Supported methods are: push, pop, top, is_empty
DO NOT CHANGE THIS CLASS IN ANY WAY
YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION
"""
def __init__(self):
""" Initialize empty stack based on Python list """
self._data =[]
def push(self, value: object)-> None:
""" Add new element on top of the stack """
self._data.append(value)
def pop(self):
""" Remove element from top of the stack and return its value """
return self._data.pop()
def top(self):
""" Return value of top element without removing from stack """
return self._data[-1]
def is_empty(self):
""" Return True if the stack is empty, return False otherwise """
return len(self._data)==0
def __str__(self):
""" Return content of the stack as a string (for use with print)"""
data_str =[str(i) for i in self._data]
return "STACK: {"+",".join(data_str)+"}"
class Queue:
"""
Class implementing QUEUE ADT.
Supported methods are: enqueue, dequeue, is_empty
DO NOT CHANGE THIS CLASS IN ANY WAY
YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION
"""
def __init__(self):
""" Initialize empty queue based on Python list """
self._data =[]
def enqueue(self, value: object)-> None:
""" Add new element to the end of the queue """
self._data.append(value)
def dequeue(self):
""" Remove element from the beginning of the queue and return its value """
return self._data.pop(0)
def is_empty(self):
""" Return True if the queue is empty, return False otherwise """
return len(self._data)==0
def __str__(self):
""" Return content of the stack as a string (for use with print)"""
data_str =[str(i) for i in self._data]
return "QUEUE {"+",".join(data_str)+"}"
class TreeNode:
"""
AVL Tree Node class
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
"""
self.value = value
self.left = None
self.right = None
self.parent = None
self.height =0
def __str__(self):
return 'AVL Node: {}'.format(self.value)
class AVL:
def __init__(self, start_tree=None)-> None:
"""
Initialize a new AVL tree
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self.root = None
# populate AVL 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 AVL in human-readable form using pre-order traversal
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
values =[]
self._str_helper(self.root, values)
return "AVL pre-order {"+",".join(values)+"}"
def _str_helper(self, cur, values):
"""
Helper method for __str__. Does pre-order traversal
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
if cur:
values.append(str(cur.value))
self._str_helper(cur.left, values)
self._str_helper(cur.right, 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
"""
s = Stack()
s.push(self.root)
while not s.is_empty():
node = s.pop()
if node:
# check for correct height (relative to children)
l = node.left.height if node.left else -1
r = node.right.height if node.right else -1
if node.height !=1+ max(l, r):
return False
if node.parent:
# parent and child pointers are in sync
if node.value node.par
 PYTHON 3-- AVL tree- please do the add() function. Existing skeleton

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!