Question: PYTHON 3 - - AVL tree - please do the add ( ) function. Existing skeleton code cannot be altered, but helper methods can be
PYTHON AVL tree please do the add function. Existing skeleton code cannot be altered, but helper methods can be added. NO BUILTIN DATA STRUCTURES CAN BE USED. Instructions for add are attached along with skeleton code. Please make sure that the given test code works.
addself 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, isempty
DO NOT CHANGE THIS CLASS IN ANY WAY
YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION
def initself:
Initialize empty stack based on Python list
self.data
def pushself value: object None:
Add new element on top of the stack
self.data.appendvalue
def popself:
Remove element from top of the stack and return its value
return self.data.pop
def topself:
Return value of top element without removing from stack
return self.data
def isemptyself:
Return True if the stack is empty, return False otherwise
return lenselfdata
def strself:
Return content of the stack as a string for use with print
datastr stri for i in self.data
return "STACK: joindatastr
class Queue:
Class implementing QUEUE ADT.
Supported methods are: enqueue, dequeue, isempty
DO NOT CHANGE THIS CLASS IN ANY WAY
YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION
def initself:
Initialize empty queue based on Python list
self.data
def enqueueself value: object None:
Add new element to the end of the queue
self.data.appendvalue
def dequeueself:
Remove element from the beginning of the queue and return its value
return self.data.pop
def isemptyself:
Return True if the queue is empty, return False otherwise
return lenselfdata
def strself:
Return content of the stack as a string for use with print
datastr stri for i in self.data
return "QUEUE joindatastr
class TreeNode:
AVL Tree Node class
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
self.value value
self.left None
self.right None
self.parent None
self.height
def strself:
return 'AVL Node: formatselfvalue
class AVL:
def initself starttreeNone 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 starttree is not None:
for value in starttree:
self.addvalue
def strself str:
Return content of AVL in humanreadable form using preorder traversal
DO NOT CHANGE THIS METHOD IN ANY WAY
values
self.strhelperselfroot, values
return "AVL preorder joinvalues
def strhelperself cur, values:
Helper method for str Does preorder traversal
DO NOT CHANGE THIS METHOD IN ANY WAY
if cur:
values.appendstrcurvalue
self.strhelpercurleft, values
self.strhelpercurright, values
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
s Stack
spushselfroot
while not sisempty:
node spop
if node:
# check for correct height relative to children
l node.left.height if node.left else
r node.right.height if node.right else
if node.height maxl r:
return False
if node.parent:
# parent and child pointers are in sync
if node.value node.par
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
