![]() |
#template.py (the two screen shots are in one script) |
#avltester.py (tester) import random def CheckHeight(tree): def CheckAVL(tree): def printTree_(tree, prefix): def printTree(tree): def Test(lib, seed=0, size=10, rounds=10, verbose=False): # Test MyTree n = random.randint(0, size) h=1 tH =tree.getHeight() if tH != h: del(tree) m = size*3 try: try: try: if bst.getHeight() != 1 or bst.getHeight() !=CheckHeight(bst): if bst.getHeight() != 3 or bst.getHeight() !=CheckHeight(bst): del(bst) a = [] del(bst) if verbose: n=10 if not(first is avl.getLeft()): try: try: if not(first isavl.getLeft().getRight()): try: # Force rotations if not (first isavl.getRight().getLeft().getRight()): for i in range(0, size): if not CheckAVL(avl): if verbose: yield True # Big test complete if __name__ == “__main__”: print(f”Test result: {score}/5″) |
Python Trees, Binary Search Tree and AVL init__(x), getLeft(), getRight (), Create a class called MyTree with the methods getData (), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node’s position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a Binary Search Tree. Create a method called _contains__(x) that returns true if x is in the tree. Create a class called MYAVL that extends MyBST. Override the method insert(x) to meet the definitions of an AVL Tree. Create a method called getBalanceFactor() that returns the balance factor of the node. You will need to implement the methods leftRotate() and rightRotate() Please find below the template and tester for the question class MyTree): definit_(self, data) Initialize this node, and store data in it self.data = data self.left None self.right =None self.height = 0 self.descendents = 0 def getLeft(sel f) # Return the left child of this node, or None return self.left def getRight(self): #Return the right child of this node, or None return self.right def getData(self) # Return the data contained in this node return self. data def insert(self, data): # Insert data into the tree, descending from this node # Ensure the tree remains complete every level is filled save for the last, and each node is as far left as possible # Return this node after data has been inserted pass def getHeight(self): # Return the height of this node pass class MyBST(MyTree): def init_(self, data) # Initialize this node, and store data in it super.init__(data) pass def insert(self, data) # Insert data into the tree, descending from this node # Ensure that the tree remains a valid Binary Search Tree # Return this node after data has been inserted pass pass def _contains__(self, data): #Returns true if data is in this node or a node descending from it pass class MyAVL(MYBST): def _init__(self, data): # Initialize this node, and store data in it super()._init__(data) pass def getBalanceFactor(self): #Return the balance factor of this node pass def insert(self, data): # Insert data into the tree, descending from this node # Ensure that the tree remains a valid AVL tree # Return the node in this node’s position after data has been inserted pass def leftRotate(self): #Perform a left rotation on this node and return the new node in its spot pass def rightRotate(self): # Perform a right rotation on this node and return the new node in its spot pass Show transcribed image text Python Trees, Binary Search Tree and AVL init__(x), getLeft(), getRight (), Create a class called MyTree with the methods getData (), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node’s position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a Binary Search Tree. Create a method called _contains__(x) that returns true if x is in the tree. Create a class called MYAVL that extends MyBST. Override the method insert(x) to meet the definitions of an AVL Tree. Create a method called getBalanceFactor() that returns the balance factor of the node. You will need to implement the methods leftRotate() and rightRotate() Please find below the template and tester for the question
class MyTree): definit_(self, data) Initialize this node, and store data in it self.data = data self.left None self.right =None self.height = 0 self.descendents = 0 def getLeft(sel f) # Return the left child of this node, or None return self.left def getRight(self): #Return the right child of this node, or None return self.right def getData(self) # Return the data contained in this node return self. data def insert(self, data): # Insert data into the tree, descending from this node # Ensure the tree remains complete every level is filled save for the last, and each node is as far left as possible # Return this node after data has been inserted pass def getHeight(self): # Return the height of this node pass class MyBST(MyTree): def init_(self, data) # Initialize this node, and store data in it super.init__(data) pass def insert(self, data) # Insert data into the tree, descending from this node # Ensure that the tree remains a valid Binary Search Tree # Return this node after data has been inserted pass
pass def _contains__(self, data): #Returns true if data is in this node or a node descending from it pass class MyAVL(MYBST): def _init__(self, data): # Initialize this node, and store data in it super()._init__(data) pass def getBalanceFactor(self): #Return the balance factor of this node pass def insert(self, data): # Insert data into the tree, descending from this node # Ensure that the tree remains a valid AVL tree # Return the node in this node’s position after data has been inserted pass def leftRotate(self): #Perform a left rotation on this node and return the new node in its spot pass def rightRotate(self): # Perform a right rotation on this node and return the new node in its spot pass
Expert Answer
Answer to #template.py (the two screen shots are in one script) #avltester.py (tester) import random #Module Imports import sys fr…