Question
I have written a code but when I run on tester it is showing Attribute Error . Could any one solve this ? I am
I have written a code but when I run on tester it is showing "Attribute Error" . Could any one solve this ? I am attaching my code and tester.
COde:
def getName():
return "li, foo"
class MyTree():
def __init__(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(self):
# 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
if self.data:
if data if self.left is None: self.left=MyTree(data) else: self.left.insert(data) elif data>self.data: if self.right is None: self.right=MyTree(data) else: self.right.insert(data) else: self.data=data pass def getHeight(self): #Return the height of this node if self: if self.left and self.right: return 1+max(self.left.getHeight(),self.right.getHeight()) elif self.left: return 1+self.left.getHeight() elif self.right: return 1+self.right.getHeight() else: return 0; else: return -1 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 if self is None: return MyTree(data) if data < self.data: self.left=self.insert(self.left,data) else: self.right = self.insert(self.right, data) return self pass def __contains__(self, data): # Returns true if data is in this node or a node descending from it if not self: return False if self.data == data: return True if self.data > data: return self.__contains__(self.left,data) return self.__contains__(self.right,data) 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 if not self: return 0 return self.getHeight(self.left) - self.getHeight(self.right) 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 if not self: return MyTree(data) elif data < self.data: self.left = self.insert(self.left, data) else: self.right = self.insert(self.right, data) # Step 2 - Update the height of the # ancestor node self.height = 1 + max(self.getHeight(self.left), self.getHeight(self.right)) # Step 3 - Get the balance factor balance = self.getBalanceFactor(self) # Step 4 - If the node is unbalanced, # then try out the 4 cases # Case 1 - Left Left if balance > 1 and data < self.left.data: return self.rightRotate(self) # Case 2 - Right Right if balance < -1 and data> self.right.data: return self.leftRotate(self) # Case 3 - Left Right if balance > 1 and data > self.left.data: self.left = self.leftRotate(self.left) return self.rightRotate(self) # Case 4 - Right Left if balance < -1 and data < self.right.data: self.right = self.rightRotate(self.right) return self.leftRotate(self) return self pass def leftRotate(self): # Perform a left rotation on this node and return the new node in its spot x = self.right y = x.left x.left = self self.right = y self.height = 1 + max(self.getHeight(self.left), self.getHeight(self.right)) x.height = 1 + max(self.getHeight(x.left), self.getHeight(x.right)) # Return the new root return x pass def rightRotate(self): # Perform a right rotation on this node and return the new node in its spot y = self.left T3 = y.right # Perform rotation y.right = self self.left = T3 # Update heights self.height = 1 + max(self.getHeight(self.left), self.getHeight(self.right)) y.height = 1 + max(self.getHeight(y.left), self.getHeight(y.right)) # Return the new root return y pass Tester: import random #Module Imports import sys from importlib import import_module def CheckHeight(tree): if tree is None: return -1 else: return max(CheckHeight(tree.getLeft())+1,CheckHeight(tree.getRight())+1) def CheckAVL(tree): ifCheckHeight(tree) > 0: l = 0 if tree.getLeft() is None else CheckHeight(tree.getLeft())+1 r = 0 if tree.getRight() is None else CheckHeight(tree.getRight())+1 b = l-r if abs(b) >= 2 or b != tree.getBalanceFactor(): print(f"balance factor is {b} and tree claims it is {tree.getBalanceFactor()}") printTree(tree) return False else: return CheckAVL(tree.getLeft()) and CheckAVL(tree.getRight()) else: return True def printTree_(tree, prefix): print(f"{prefix}{tree.data}") if tree.getLeft() is not None: printTree_(tree.getLeft(),prefix+"-") if tree.getRight() is not None: printTree_(tree.getRight(),prefix+"-") def printTree(tree): printTree_(tree,"") def Test(lib, seed=0, size=10, rounds=10, verbose=False): random.seed(a=seed) # Test MyTree flag = True n = random.randint(0, size) try: tree = lib.MyTree(n) except: if verbose: print("Error: MyTree not creatable") flag = False h=1 c=2 for i in range(1,size): n = random.randint(n,n*2) print(n) try: tree = tree.insert(n) except: print(c) if verbose: print("Error: Tree not insertable") flag = False tH = tree.getHeight() if CheckHeight(tree) != tH: if verbose: print("Error: Tree height calculation incorrect") flag = False if tH != h: if verbose: print(f"Error: Tree height incorrect: Should be {h} but is {tH}") flag = False if i == c: h+=1 c+=(2**h) del(tree) if verbose: if flag: print("Tree test complete.") else: print("Tree test failed.") # Tree works yield flag flag = True m = size*3 n = random.randint(size, size*2) try: bst = lib.MyBST(n) except: if verbose: print("Error: MyBST not creatable") flag = False try: bst=bst.insert(n+1) except: if verbose: print("Error: BST not insertable") flag = False try: bst=bst.insert(n-1) except: if verbose: print("Error: BST not insertable") flag = False if bst.getHeight() != 1 or bst.getHeight() != CheckHeight(bst): if verbose: print("Error: BST height incorrect") flag = False try: bst=bst.insert(n+2) bst=bst.insert(n+3) except: if verbose: print("Error: BST not insertable") flag = False if bst.getHeight() != 3 or bst.getHeight() != CheckHeight(bst): if verbose: print("Error: BST height incorrect.") flag = False del(bst) bst= lib.MyBST(n) a = [] for i in range(size): v = random.randint(0,m) bst= bst.insert(v) if not(v in a): a.append(v) for i in range(size): if len(a) >= size: m*=2 v = random.randint(0,m) if (v in a) != (v in bst): if verbose: print("Error: BST Search incorrect") flag = False del(bst) if verbose: if flag: print("BST test complete.") else: print("BST test failed.") yield flag #BST works flag = True n=10 try: avl = lib.MyAVL(n) except: if verbose: print("Error: MyAVL not creatable") flag = False first = avl try: avl = avl.insert(n+6) except: if verbose: print("Error: AVL not insertable #1") flag = False try: avl = avl.insert(n+12) except: if verbose: print("Error: AVL not insertable #2") flag = False if not(first is avl.getLeft()): if verbose: print("Error: AVL Rotation incorrect #1") flag = False try: second = avl.getRight() second except: if verbose: print("Error: AVL Node lost during rotation") flag = False try: avl=avl.insert(8) avl=avl.insert(6) except: if verbose: print("Error: AVL Node not insertable") flag = False if not(first is avl.getLeft().getRight()): if verbose: print("Error: AVL Rotation incorrect #2") flag = False if verbose: if flag: print("AVL Double Rotation test complete") else: print("AVL Double Rotation test failed") yield flag # Simple rotation correct flag = True try: avl=avl.insert(n-3) except: if verbose: print("Error: AVL Node not insertable") flag = False # Force rotations avl = avl.insert(n-2) avl = avl.insert(n-1) avl = avl.insert(n*2+4) avl = avl.insert(n*2+3) if not (first is avl.getRight().getLeft().getRight()): if verbose: print("Error: AVL Rotation incorrect #3") flag = False if verbose: if flag: print("AVL Double Rotation test complete") else: print("AVL Double Rotation test failed") yield flag # Rotation test complete flag = True for i in range(0, size): avl = avl.insert(random.randint(0,size)) if not CheckAVL(avl): if verbose: print("Error: AVL Property not maintained across inserts") flag = False if verbose: if flag: print("AVL Property test complete") else: print("AVL Property test failed") yield flag # Big test complete if __name__ == "__main__": if len(sys.argv) < 2: print("Include at least a library name as an argument.") exit() name = sys.argv[1] if name.startswith(".\\"): name = name[2:] if name.endswith(".py"): name = name[:-3] module=import_module(name,package=__name__) print(f"Testing module {name} by {module.getName()}") score=0 for i in Test(module,seed=123456, size=1000, rounds=200, verbose=True): if i: score+=1 print(f"Test result: {score}/5")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started