def _edit_tree_to_contain_new_nodes(self) -> [AVLNode]:
     """Fills places for children in all external nodes - nodes which have none or only one child"""
     new_child_nodes = []
     for node in self._external_nodes:
         if node.left is None:
             node.left = AVLNode(node, -1)
             new_child_nodes.append(node.left)
         if node.right is None:
             node.right = AVLNode(node, -1)
             new_child_nodes.append(node.right)
     return new_child_nodes
 def testallNodesLeftAreSmallerPositive(self):
     root_node = AVLNode(5, None, None)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(avl_tree.root, 4)
     avl_tree.insert(avl_tree.root, 3)
     avl_tree.insert(avl_tree.root, 2)
     self.assertTrue(avl_tree.nodes_left_are_smaller())
 def testisRightChild(self):
     root_node = AVLNode(5, None, None)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(root_node, 4)
     avl_tree.insert(root_node, 2)
     avl_tree.insert(root_node, 6)
     self.assertTrue(root_node.right.is_right_child())
 def testParentNode(self):
     root_node = AVLNode(5, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(avl_tree.root, 4)
     avl_tree.insert(avl_tree.root, 2)
     avl_tree.insert(avl_tree.root, 6)
     self.assertEquals(avl_tree.root.left.parent, avl_tree.root)
 def testBalancedAVLTree(self):
     root_node = AVLNode(5, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(avl_tree.root, 6)
     avl_tree.insert(avl_tree.root, 7)
     avl_tree.insert(avl_tree.root, 8)
     avl_tree.insert(avl_tree.root, 1)
 def testIsLeftChild(self):
     root_node = AVLNode(5, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(avl_tree.root, 4)
     avl_tree.insert(avl_tree.root, 2)
     avl_tree.insert(avl_tree.root, 6)
     self.assertTrue(avl_tree.root.left.is_left_child())
 def testRightRotationv2(self):
     root_node = AVLNode(5, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(avl_tree.root, 4)
     avl_tree.insert(avl_tree.root, 2)
     avl_tree.insert(avl_tree.root, 6)
     self.assertEquals(avl_tree.root.left.data, 2)
 def testRightRotateRoot(self):
     root_node = AVLNode(5, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(avl_tree.root, 4)
     avl_tree.insert(avl_tree.root, 3)
     self.assertTrue(avl_tree.allNodesRightAreLarger())
     self.assertTrue(avl_tree.nodes_left_are_smaller())
 def testRightLeftRotate(self):
     root_node = AVLNode(4, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(root_node, 6)
     avl_tree.insert(root_node, 5)
     self.assertEquals(avl_tree.root.data, 5)
     self.assertEquals(avl_tree.root.left.data, 4)
     self.assertEquals(avl_tree.root.right.data, 6)
 def testLeftRotation(self):
     root_node = AVLNode(5, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(root_node, 6)
     avl_tree.insert(root_node, 7)
     self.assertEquals(avl_tree.root.data, 6)
     self.assertEquals(avl_tree.root.left.data, 5)
     self.assertEquals(avl_tree.root.right.data, 7)
     self.assertEquals(avl_tree.root.left.parent, avl_tree.root)
     self.assertEquals(avl_tree.root.right.parent, avl_tree.root)
     self.assertEquals(avl_tree.root.parent, None)
 def testrightRotateTestV3(self):
     root_node = AVLNode(5, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(avl_tree.root, 4)
     avl_tree.insert(avl_tree.root, 3)
     avl_tree.insert(avl_tree.root, 2)
     avl_tree.insert(avl_tree.root, 1)
     avl_tree.insert(avl_tree.root, 6)
     avl_tree.printAllNodes()
     self.assertTrue(avl_tree.allNodesRightAreLarger())
     self.assertTrue(avl_tree.nodes_left_are_smaller())
     self.assertEquals(avl_tree.root.left.data, 2)
     self.assertEquals(avl_tree.root.left.left.data, 1)
     self.assertEquals(avl_tree.root.left.right.data, 3)
     self.assertEquals(avl_tree.root.right.data, 5)
     self.assertEquals(avl_tree.root.right.right.data, 6)
 def handle_avl_tree(self):
     print "How many nodes would you like to include in the AVL tree?"
     num_nodes = raw_input(">")
     print "Please enter the root node"
     root_node_data = raw_input(">")
     root_node = AVLNode(data=root_node_data,
                         left=None,
                         right=None,
                         is_root=True)
     avl_tree = AVLTree(root_node)
     for i in range(int(num_nodes) - 1):
         print "Please enter node val"
         val = raw_input(">")
         avl_tree.insert(avl_tree.root, val)
     print "Your final avl_tree is, in the form [Current, Left Node, Right Node]\n", avl_tree.printAllNodes(
     )
Beispiel #13
0
def insert_into_AVL(text_file):
    avlTree = AVLTree()

    original_file = open(text_file, "r")
    for line in original_file:
        l = line.split("\n")
        node = AVLNode(l[0])
        avlTree.insert(node)

    print("Tree successfully populated!")
    anagrams(avlTree, "spot")  # search and print anagrams

    list_words = []
    demo = open(test_file, "r")
    for line in demo:
        l = line.split("\n")
        list_words.append(l[0])
    # count and find the topmost anagram
    count_anagrams(list_words, avlTree)
 def testAVLNode(self):
     avl_node = AVLNode(9, None, None)
     self.assertEquals(0, avl_node.balance_factor)
     avl_node.balance_factor = 10
     self.assertEquals(10, avl_node.balance_factor)
 def testAllNodesRightAreLargerNegative(self):
     node_1 = AVLNode(1, None, None)
     root_node = AVLNode(5, None, node_1, True)
     avl_tree = AVLTree(root_node)
     self.assertFalse(avl_tree.allNodesRightAreLarger())
 def testAllNodesRightAreLargerPositive(self):
     root_node = AVLNode(5, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(avl_tree.root, 4)
     avl_tree.insert(avl_tree.root, 3)
     self.assertTrue(avl_tree.allNodesRightAreLarger())
 def testAVLTreeInherited(self):
     avl_node = AVLNode(5, None, None)
     avl_tree = AVLTree(avl_node)
     avl_tree.insert(avl_node, 6)
     self.assertTrue(avl_tree.search(avl_node, 6))
     self.assertEquals(1, avl_tree.get_height(avl_node))
 def testLeftRotateRoot(self):
     root_node = AVLNode(5, None, None, True)
     avl_tree = AVLTree(root_node)
     avl_tree.insert(avl_tree.root, 6)
     avl_tree.insert(avl_tree.root, 7)
 def testallNodesLeftAreSmallerNegative(self):
     node_1 = AVLNode(7, None, None)
     root_node = AVLNode(5, node_1, None)
     avl_tree = AVLTree(root_node)
     self.assertFalse(avl_tree.nodes_left_are_smaller())
Beispiel #20
0
		size = int(input('Defina o tamanho do Hash. '))
		size = size if size>MAX_TAM_HASH else 1
		tabela = HashTable(size,'duplo')
		
		#Fazer a contagem e armazenar na tabela e/ou lista-encadeada
		for word in wordList:
			tabela.addCounter(word)
		
		#Exibir Saida
		print('\'{}\' really works!'.format(optionStr.lower()))
	
	elif optionStr.lower() == 'avl':# Árvore AVL
		# CHAMAR OBJETOS E SUBROTINAS ESPECÍFICAS(1/3)
		#Criar Árvore AVL
		key = wordList[0]
		arvore = AVLNode(key)
		#Fazer a contagem e armazenar na árvore (wordList[1:])
		for word in wordList[1:]:
			occur = arvore.addCounter(word)
			if occur <= 0:
				print('ERRO: Chave \'{}\' não foi inserida.\n'.format(word))
		
		#Exibir Saída
		print('\'{}\' really works!'.format(optionStr.lower()))
	
	elif optionStr.lower() == 'redblack':# Árvore Rubro-Negra
		# CHAMAR OBJETOS E SUBROTINAS ESPECÍFICAS(1/3)
		#Criar Árvore Rubro-Negra
		key = wordList[0]
		arvore = RBNode(key)
		#Fazer a contagem e armazenar na árvore (wordList[1:len(wordList)])