def setUp(self): # Original tree rootitem = avltree.AVLNode(10.0) self.t = avltree.AVLTree(root=rootitem) items = [3.0, 18.0, 2.0, 4.0, 13.0, 40.0] for newItem in items: self.t.insert(newItem)
def setUp(self): a = avltree.AVLNode(8, -1) b = avltree.AVLNode(18, -1) c = avltree.AVLNode(3) d = avltree.AVLNode(20) self.t = avltree.AVLTree() self.t.root = b b.left = a a.left = c b.right = d self.t.count = 4
import avltree from draw import plot_tree import pickle import time from Heap import Heap if __name__ == '__main__': avl = avltree.AVLTree() n1 = avltree.AVLNode(143,12,time.time()) n2 = avltree.AVLNode(18,4,time.time()) n3 = avltree.AVLNode(1432,512,time.time()) n4 = avltree.AVLNode(173,112,time.time()) n5 = avltree.AVLNode(1,1,time.time()) n6 = avltree.AVLNode(3,120,time.time()) avl.insert_node(n1) avl.insert_node(n2) avl.insert_node(n3) avl.insert_node(n4) avl.insert_node(n5) avl.insert_node(n6) """ avl.insert(143,12,time.time()) avl.insert(18,4,time.time()) avl.insert(1432,512,time.time()) avl.insert(173,112,time.time()) avl.insert(1,1,time.time()) avl.insert(3,120,time.time()) """ pickle.dump(avl,open("lj.txt","w")) avl_read = pickle.load(open("lj.txt","r"))
def draw_tree(self): clock = pygame.time.Clock() value = None command = None tree_type = None while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == pygame.KEYUP: if (256 <= event.key <= 266) or (48 <= event.key <= 57) or event.key == 46 or event.key == 45 or event.key == 269: pressed = self.key_dict[event.key] self.enter_value += pressed elif (97 <= event.key <= 122) or (65 <= event.key <= 90) or event.key == 61: pressed = str.lower(self.key_dict[event.key]) self.enter_value += pressed elif event.key == 13 or event.key == 271: value = self.type_is_numeric(self.enter_value) if value: self.input_values(value, command) else: command = self.enter_value[0:3] if command == 'bin': self.bt = binarytree.BinaryTree() tree_type = 'bin' elif command == 'avl': self.bt = avltree.AVLTree() tree_type = 'avl' elif command == 'rbt': self.bt = rbtree.RBTree() tree_type = 'rbt' elif command == 'cls': self.points_dict = {} self.lines_dict = {} self.bt.nodes_dict = {} self.bt = None self.make_points_lines() elif command == 'rm=': _, value = self.enter_value.split('=') num = self.type_is_numeric(value) remove_key, successor = self.bt.remove(num) if tree_type == 'rbt': self.make_points_lines_rbt() else: self.remove(remove_key, successor) self.make_points_lines() self.enter_value = "" self.enter_value = "" elif event.key == 27: pygame.quit() exit() elif event.key == 8: # backspace if self.enter_value: x = len(self.enter_value) self.enter_value = self.enter_value[0:x - 1] self.screen.fill(self.WHITE) self.draw_input() if tree_type == 'bin' or tree_type == 'avl': self.draw_nodes(value) elif tree_type == 'rbt': self.draw_nodes_rbt(value) elif command == 'rm=': if tree_type == 'bin' or tree_type == 'avl': self.draw_nodes_rbt(value) else: self.draw_nodes_rbt(value) elif command == 'cls': self.draw_nodes(None) self.screen.blit(self.image, (283, 200)) pygame.draw.aaline(self.screen, self.RED, [110, 40], [424, 200], 2) else: self.screen.blit(self.image, (283, 200)) pygame.draw.aaline(self.screen, self.RED, [110, 40], [424, 200], 2) clock.tick(30) pygame.display.update()
def handle_trees(bt=None): uteis.clear() if not bt: print('***************************') print('1 - Create binary tree') print('2 - Create avl tree') print('0 - Exit') print('***************************') op = input('Enter the option: ') try: op = int(op) except TypeError: print('Invalid option.') input('Press enter to return for options.\n') handle_trees() if op == 1: bt = binarytree.BinaryTree() elif op == 2: bt = avltree.AVLTree() elif op == 0: exit(0) else: print('Invalid option.') input('Press enter to continue.') handle_trees() uteis.clear() while op != 0: print('Options:') print('***************************') print('1 - Enter nodes') print('2 - Walk in order') print('3 - Walk pos order') print('4 - Remove node') print('5 - Successor') print('6 - Predecessor') print('0 - Exit') print('***************************') print() op = input('Enter the option: ') try: op = int(op) except TypeError: print('Invalid option.') input('Press enter to return for options.\n') handle_trees() uteis.clear() if op == 1: print('Enter the nodes (enter none to end):') print('*********************************************') key = input('node: ') while key: key = int(key) bt.insert(key) key = input('node: ') print('*********************************************\n') elif op == 2: print('Walk In Order:') print('node\tparent\tleft\tright\theight\tfb') print('***********************************************') bt.walk_in_order() print('***********************************************\n') elif op == 3: print('Walk In Order:') print('node\tparent\tleft\tright\theight\tfb') print('***********************************************') bt.walk_pos_order() print('***********************************************\n') elif op == 4: print('***********************************************') key = int(input("To remove node enter it's key: ")) if bt.remove(key): print('Successfully removed {0}'.format(key)) else: print('Failed to remove {0}. Make sure it exists on the tree'. format(key)) print('***********************************************\n') elif op == 5: print('*********************************************************') key = int( input("Enter the key of the node you want the successor: ")) successor = bt.successor(key) if successor: print('Sucessor is: {0}'.format(successor.key)) else: print('Not found.') print('*********************************************************') elif op == 6: print('*********************************************************') key = int( input("Enter the key of the node you want the predecessor: ")) predecessor = bt.predecessor(key) if predecessor: print('Predecessor is: {0}'.format(predecessor.key)) else: print('Not found.') print( '*********************************************************' ) elif op == 0: sys.exit(0) else: print('Invalid option.') input('Press enter to return for options.\n')