def printTree(tree): setup(node_init_func=lambda v: TreeNode(v), node_class=TreeNode, null_value=None, value_attr='data', left_attr='left', right_attr='right') pprint(tree) return
def insert(self, k): if self.root is None: self.root = AVLNode(k) else: self.root = self.root.insert(k) def remove(self, k): if self.root is None: return else: self.root = self.root.remove(k, None) setup(node_init_func=lambda v: AVLNode(v), node_class=AVLNode, null_value=None, value_attr='key', left_attr='left', right_attr='right') if __name__ == '__main__': avl = AVL() avl.insert(70) avl.insert(60) avl.insert(50) avl.insert(40) avl.insert(30) avl.insert(20) avl.insert(10) avl.insert(25) avl.insert(31) avl.remove(40)
print " --- new root --- " print self print " --- old root --- " print old_root old_root.left = new_left_sub new_root.right = old_root def rotate_left(self): """ Left rotation set self as the left subtree of right subree """ print "rotate left" new_root = copy.deepcopy(self.right) new_left_sub = copy.deepcopy(new_root.left) old_root = self self = new_root old_root.right = new_left_sub new_root.left = old_root # Call setup in the beginning to apply your specification setup(node_init_func=lambda v: MyNode(v), node_class=MyNode, null_value=my_null, value_attr='value', left_attr='left', right_attr='right') my_custom_tree = tree()
if node.right != None: print('successor set to below right') successor = node.right else: print('searching for parent') return findParent(node, node.parent) if successor != None and successor.left: print('traverse down left') return findSuccessor(node, successor.left) else: print('found successor', successor.data) return successor if __name__ == "__main__": root = produceTree(list(range(1, 30))) createParents(root) # traverse(root) # print(findSuccessor(root.right).data) setup(node_init_func=lambda v: TreeNode(v), node_class=TreeNode, null_value=None, value_attr='data', left_attr='left', right_attr='right') pprint(root) print(findSuccessor(root.left.left.right.left))