Assignment Week 6 AVL Trees Heap Sort Imported Code class treeNode: def __init__(self, parent=None, leftright=None, superroot=True): self.value = None self.leftNode = None self.rightNode = None self.parent = parent self.leftright = leftright self.superroot = superroot if superroot: self.setValue(None) def clearTree(self): if self.superroot: self.setValue(None) def getNode(self, i): if self.superroot: return self.leftNode if self.value > i: return self.leftNode return self.rightNode def free(self): if self.superroot: return False if not self.value is None: return False return True def printTree(self, space=""): if self.superroot: self.leftNode.printTree(space) return if self.value is None: return self.rightNode.printTree(space + " ") print (space + self.value.__str__()) self.leftNode.printTree(space + " ") def setValue(self, i): self.value = i self.leftNode = self.__class__(self, "left", False) if not self.superroot: self.rightNode = self.__class__(self, "right", False) def setNode(self, node, leftright): if self.superroot: leftright = "left" if leftright == "left": self.leftNode = node self.leftNode.parent = self self.leftNode.leftright = "left" return self.rightNode = node self.rightNode.parent = self self.rightNode.leftright = "right" def sibling(self, child): if child.leftright == "left": return self.rightNode return self.leftNode def insertValue(self, i): if self.superroot: self.leftNode.insertValue(i) return if self.free(): self.setValue(i) return self.getNode(i).insertValue(i) def insertArray(self, a): if not self.superroot: self.parent.insertArray(a) return for i in a: self.insertValue(i) def insertFile(self, filename): f = open(filename) l = f.readline() l = l.split() a = [] for i in l: a.append(int(i)) self.insertArray(a) def searchPrint(self, k, complete=False, findString=""): if self.superroot: self.getNode(k).searchPrint(k, complete, findString) return if not self.parent.superroot: findString += " -> " findString += self.value.__str__() if self.value == k: print findString return if self.value is None: if complete: print(findString) else: print("the input number was not found in the BST") return self.getNode(k).searchPrint(k, complete, findString) def searchPrintComplete(self, k): self.searchPrint(k, True) def heightOfNode(self): if self.superroot: return self.leftNode.heightOfNode() if self.value is None: return -1 return max(self.leftNode.heightOfNode(), self.rightNode.heightOfNode()) + 1 def balancedNode(self): if self.superroot: if self.leftNode.balancedNode() == -2: return False return True if self.value is None: return -1 left = self.leftNode.balancedNode() right = self.rightNode.balancedNode() if (left == -2) or (right == -2): return -2 if abs(left - right) > 1: return -2 return max(left, right) + 1 Project Code class AVLTreeNode(treeNode): def insertValue(self, i): treeNode.insertValue(self, i) if self.superroot: self.AVLBalance() def AVLBalance(self, double=None): if self.superroot: self.leftNode.AVLBalance() return if self.value is None: return -1 left = self.leftNode.AVLBalance() right = self.rightNode.AVLBalance() if double == "left": if left < right: self.parent.setNode(self.rightNode, self.leftright) hold = self.rightNode self.setNode(self.rightNode.leftNode, "right") hold.setNode(self, "left") left += 1 right -= 1 return if double == "right": if right < left: self.parent.setNode(self.leftNode, self.leftright) hold = self.leftNode self.setNode(self.leftNode.rightNode, "left") hold.setNode(self, "right") left -= 1 right += 1 return if (left + 1) < right: self.rightNode.AVLBalance("right") self.parent.setNode(self.rightNode, self.leftright) hold = self.rightNode self.setNode(self.rightNode.leftNode, "right") hold.setNode(self, "left") left += 1 right -= 1 if (right + 1) < left: self.leftNode.AVLBalance("left") self.parent.setNode(self.leftNode, self.leftright) hold = self.leftNode self.setNode(self.leftNode.rightNode, "left") hold.setNode(self, "right") left -= 1 right += 1 return max(left, right) + 1 Commands tree = AVLTreeNode() tree.insertValue(1) tree.printTree() print '' tree.insertValue(2) tree.printTree() print '' tree.insertValue(3) tree.printTree() print '' tree.insertValue(4) tree.printTree() print '' tree.insertValue(5) tree.printTree() print '' tree.insertValue(6) tree.printTree() print '' tree.insertValue(7) tree.printTree() print '' tree.insertValue(8) tree.printTree() print '' tree.insertValue(9) tree.printTree() print '' tree.insertValue(0) tree.printTree() Run Output Imported Code Project Code def heapSort(a, printFlag=False): a.insert(0, len(a)) heapify(a) for i in range(1, a[0]): if printFlag: print a[:a[0]+1] n = deleteMax(a) a.insert(a[0] + 1, n) if printFlag: print a[:a[0]+1] a.pop(0) print a def percolateDown(a, start): root = start while root * 2 <= a[0]: child = root * 2 swap = root if a[swap] < a[child]: swap = child if (child + 1) <= a[0]: if a[swap] < a[child + 1]: swap = child + 1 if swap != root: a[root], a[swap] = a[swap], a[root] root = swap else: return def heapify(a): start = a[0] // 2 while start > 0: percolateDown(a, start) start -= 1 def deleteMax(a): a[0] -= 1 n = a.pop(1) heapify(a) return n def heapSortFile(filename, printFlag=False): f = open(filename) l = f.readline() l = l.split() a = [] for i in l: a.append(int(i)) return heapSort(a, printFlag) Commands heapSort([1,6,2,7,3,8,4,9,5,0]) print '' heapSort([1,6,2,7,3,8,4,9,5,0], True) Run Output