def test_insertion_happens_as_expected(self): # k = 5 and input values 1..10 k = 5 tree = BTree(k) for i in range(1, 18): tree.insert(i, None) print(f'Inserted {i} into tree. root: {tree.get_root()}') print('---') result = tree.get_root() self.assertTrue(result.keys == [9]) first_child_layer = result.children self.assertTrue(len(first_child_layer) == 2) self.assertTrue(first_child_layer[0].keys == [3, 6]) self.assertTrue(first_child_layer[1].keys == [12, 15]) for i in first_child_layer: self.assertFalse(i.is_leaf) # first 3 leafs self.assertEquals([1, 2], first_child_layer[0].children[0].keys) self.assertEquals([4, 5], first_child_layer[0].children[1].keys) self.assertEquals([7, 8], first_child_layer[0].children[2].keys) self.assertEquals(True, first_child_layer[0].children[0].is_leaf) self.assertEquals(True, first_child_layer[0].children[1].is_leaf) self.assertEquals(True, first_child_layer[0].children[2].is_leaf) # last 3 leafs self.assertEquals([10, 11], first_child_layer[1].children[0].keys) self.assertEquals([13, 14], first_child_layer[1].children[1].keys) self.assertEquals([16, 17], first_child_layer[1].children[2].keys) self.assertEquals(True, first_child_layer[1].children[0].is_leaf) self.assertEquals(True, first_child_layer[1].children[1].is_leaf) self.assertEquals(True, first_child_layer[1].children[2].is_leaf)
def teste_pesquisa(self): keys = [ 20, 10, 40, 50, 30, 55, 3, 11, 4, 28, 36, 33, 52, 17, 25, 13, 54, 9, 43, 8, 48 ] chaves_procuradas = [36, 20, 100, 48, 200, 3, 300] arr_esperado = [True, True, False, True, False, True, False] arvore = BTree(2) for i in keys: arvore.insert_key(i) existe_chave: bool = False for i in range(len(chaves_procuradas)): no_procura, posicao = arvore.search_tree(chaves_procuradas[i]) if no_procura is not None and posicao is not None: existe_chave = True else: existe_chave = False self.assertEqual( existe_chave, arr_esperado[i], f"Erro na árvore de teste: o numero {chaves_procuradas[i]} não foi encontrada" )
def loadBtree(lista): if not lista is None: MyTree = BTree() for item in lista: MyTree.insert(item) return MyTree return None
def OP2BTree(lista, BTree): if not (lista is None or BTree is None): for item in lista: aux = BTree.get(item) if aux == None: BTree.insert(item) return str(BTree)
def mini_tree(data:list[int]) -> BTree: '''Create a mini depth binary search tree from the data of a sorted list''' if len(data) < 1: return None if len(data) == 1: return BTree(data[0]) mid = len(data)//2 #print(mid) node = BTree(data[mid]) node.left = mini_tree(data[:mid]) node.right = mini_tree(data[mid+1:]) return node
def test_search(self): # k = 5 and input values 1..10 k = 195 n = 1500000 tree = BTree(k) for i in range(0, n): tree.insert(i, None) if i % 100000 == 0: print(f'{i} inserted') print(f"root after execution. Height: {tree.height}") for i in range(0, n): result = tree.search(i) self.assertTrue(i in result.keys)
def test_buggy_split(self): # this didn't work before tree = BTree(k=5) root = BNode([3, 6]) root.add_child(BNode([1, 2]), BNode([4, 5]), BNode([7, 8, 9, 10])) tree.root = root print(f'result: {tree._split(11, root.children[2])}') new_root = tree.get_root() self.assertTrue([3, 6, 9] == new_root.keys) self.assertTrue(len(new_root.children) == 4) self.assertTrue([1, 2] == new_root.children[0].keys) self.assertTrue([4, 5] == new_root.children[1].keys) self.assertTrue([7, 8] == new_root.children[2].keys) self.assertTrue([10, 11] == new_root.children[3].keys)
def OP1BTree(lista, BTree): if not (lista is None or BTree is None): inter = [] for item in lista: aux = BTree.get(item) if aux == item: inter.append(item) return inter
def OP3BTree(lista, BTree): if not (lista is None or BTree is None): for item in lista: aux = BTree.get(item) if aux != None: lista.remove(item) return str(lista)
def test_split_balances_parents_parent(self): # k = 5 and input values 1..5 k = 5 input_node = BNode([3, 6, 9, 12]) a = BNode([1, 2]) b = BNode([4, 5]) c = BNode([7, 8]) d = BNode([10, 11]) e = BNode([13, 14, 15, 16]) a.is_leaf = True b.is_leaf = True b.is_leaf = True c.is_leaf = True d.is_leaf = True e.is_leaf = True input_node.add_child(a, b, c, d, e) tree = BTree(k) tree._split(17, e) result = tree.get_root() self.assertTrue(len(result.keys) == 1) self.assertTrue(result.keys == [9]) first_child_layer = result.children self.assertTrue(len(first_child_layer) == 2) self.assertTrue(first_child_layer[0].keys == [3, 6]) self.assertTrue(first_child_layer[1].keys == [12, 15]) for i in first_child_layer: self.assertFalse(i.is_leaf) # first 3 leafs self.assertEquals([1, 2], first_child_layer[0].children[0].keys) self.assertEquals([4, 5], first_child_layer[0].children[1].keys) self.assertEquals([7, 8], first_child_layer[0].children[2].keys) self.assertEquals(True, first_child_layer[0].children[0].is_leaf) self.assertEquals(True, first_child_layer[0].children[1].is_leaf) self.assertEquals(True, first_child_layer[0].children[2].is_leaf) # last 3 leafs self.assertEquals([10, 11], first_child_layer[1].children[0].keys) self.assertEquals([13, 14], first_child_layer[1].children[1].keys) self.assertEquals([16, 17], first_child_layer[1].children[2].keys) self.assertEquals(True, first_child_layer[1].children[0].is_leaf) self.assertEquals(True, first_child_layer[1].children[1].is_leaf) self.assertEquals(True, first_child_layer[1].children[2].is_leaf)
def teste_insercao(self): keys = [ 20, 10, 40, 50, 30, 55, 3, 11, 4, 28, 36, 33, 52, 17, 25, 13, 54, 9, 43, 8, 48 ] arr_esperado = [[20], [4, 11], [3], [8, 9, 10], [13, 17], [30, 40, 52], [25, 28], [33, 36], [43, 48, 50], [54, 55]] arvore = BTree(2) for i in keys: arvore.insert_key(i) arr_recebido = arvore.get_tree_as_array() for i in range(len(arr_esperado)): self.assertEqual( arr_recebido[i], arr_esperado[i], f"Erro! Arvore esta diferente do esperado no node {arr_recebido[i]}, o esperado seria {arr_esperado[i]}" )
def test_pick_second_child_as_smallest(self): node = BNode(data=[125, 250, 375, 500]) a = BNode([25, 50, 75, 100]) b = BNode([150, 175, 200, 225]) c = BNode([275, 300, 320, 350]) d = BNode([400, 425, 450, 475]) e = BNode([525, 550, 575, 600]) node.add_child(a) node.add_child(b) node.add_child(c) node.add_child(d) node.add_child(e) k = 5 tree = BTree(k) result = tree._get_next_biggest_smallest_child(128, node) self.assertEqual(b, result)
def test_split_happens_as_expected(self): # k = 3 and input values 1..3 k = 3 expected_output = BNode([1]) a = BNode([0]) b = BNode([2]) a.is_leaf = True b.is_leaf = True expected_output.add_child(a) expected_output.add_child(b) input_node = BNode([0, 1]) tree = BTree(k) result = tree._split(2, input_node) self.assertTrue(result.keys, [1]) self.assertEqual(len(result.children), 2) self.assertEqual(result.children[0].keys, [0]) self.assertEqual(result.children[1].keys, [2])
def teste_pesquisa(self): print("TESTE DE PESQUISA") keys = [ 20, 10, 40, 50, 30, 55, 3, 11, 4, 28, 36, 33, 52, 17, 25, 13, 54, 9, 43, 8, 48 ] arvore_b_ordem_2 = BTree(2) for i in keys: arvore_b_ordem_2.insert_key(i) chaves_procuradas = [36, 20, 100, 48] for i in chaves_procuradas: print("Key:", i) no_procura, posicao = arvore_b_ordem_2.search_tree(i) if (no_procura is not None and posicao is not None): print("Encontrada key no node:", no_procura.keys, " Na posicao:", posicao) else: print("Nao ha essa chave na arvore") print()
def test_split_happens_as_expected_part_2(self): # k = 5 and input values 1..5 k = 5 expected_output = BNode([3]) a = BNode([1, 2]) b = BNode([4, 5]) a.is_leaf = True b.is_leaf = True expected_output.add_child(a) expected_output.add_child(b) tree = BTree(k) input_node = BNode([1, 2, 3, 4]) result = tree._split(5, input_node) print(f'5 Values: {result}') self.assertTrue(result.keys, [3]) self.assertEqual(len(result.children), 2) self.assertEqual(result.children[0].keys, [1, 2]) self.assertEqual(result.children[1].keys, [4, 5]) tree.insert(6, result) print(f'{tree.get_root()}')
def test_finds_proper_node_that_holds_our_key(self): # k = 5 and input values 1..5 k = 3 tree = BTree(k) for i in range(1, 32): tree.insert(i, None) root = tree.get_root() self.assertTrue( 8 in tree._get_next_biggest_smallest_child(3, root).keys) self.assertTrue(4 in tree._get_next_biggest_smallest_child( 3, root.children[0]).keys) self.assertTrue(2 in tree._get_next_biggest_smallest_child( 3, root.children[0].children[0]).keys) self.assertTrue(3 in tree._get_next_biggest_smallest_child( 3, root.children[0].children[0].children[0]).keys)
def teste_delete(self): print("TESTE DE DELECAO NA ARVORE B") keys = [ 20, 10, 40, 50, 30, 55, 3, 11, 4, 28, 36, 33, 52, 17, 25, 13, 54, 9, 43, 8, 48 ] arvore_b_ordem_2 = BTree(2) for i in keys: arvore_b_ordem_2.insert_key(i) chave_deletada = [36, 33, 3, 10, 25] for i in chave_deletada: print("Key retirada:", i) arvore_b_ordem_2.delete_key(i) arvore_b_ordem_2.print_tree() print()
def teste_insercao(self): print("TESTE DE INSERCAO NA ARVORE B") keys = [ 20, 10, 40, 50, 30, 55, 3, 11, 4, 28, 36, 33, 52, 17, 25, 13, 54, 9, 43, 8, 48 ] arvore_b_ordem_2 = BTree(2) for i in keys: arvore_b_ordem_2.insert_key(i) arvore_b_ordem_2.print_tree() print()
from tree import BTree # k defines the key capacity for each node. Note that the actual # key capacity is k-1 k = 10195 # the amount of nodes you wanna enter n = 450005 tree = BTree(k) for i in range(0, n): tree.insert(i, None) if i % 100000 == 0: print(f'{i} inserted') print(f"root after execution. Height: {tree.height}") for i in range(0, n): result = tree.search(i) if i not in result.keys: # this code will never run :-) At least it shouldn't... raise Exception(f"{i} not contained. The tree is broken :(")
#### driver a = TreeNode(1, None, None) b = TreeNode(2, None, None) c = TreeNode(5, None, None) d = TreeNode(3, None, None) e = TreeNode(4, None, None) f = TreeNode(6, None, None) g = TreeNode(7, None, None) h = TreeNode(8, None, None) a.left_child, a.right_child = b, c b.left_child, b.right_child = d, e c.left_child, c.right_child = f, g f.left_child = h ''' 1 / \ 2 5 / \ / \ 3 4 6 7 / 8 ''' btree = BTree(a)
def test(): data = [1,2,3,4,5,6,7,8,9,10] node = mini_tree(data) print("tree hight:", BTree.hight(node)) BTree.print_tree(node) # list of depths lst = list_of_depths(node) for i in range(len(lst)): node = lst[i] s = '' while node: s = s + '{},'.format(node.data.value) node = node.next #print("level {}: {}".format(i, s)) # check balance print('-- Check balance --') node = mini_tree([1]) print(check_balance(node)) # True node = mini_tree([1,2,3]) print(check_balance(node)) # True node = mini_tree([1,2,3,4,5,6]) print(check_balance(node)) # False node = mini_tree([1,2,3,4,5,6,7]) print(check_balance(node)) # True node = mini_tree([1,2,3,4,5,6,7,8,9]) print(check_balance(node)) # False node = mini_tree([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]) print(check_balance(node)) # True # validate BST print('-- Validate BST --') node = mini_tree([1,2,3,4,5,6]) print(validate_bst(node)) # True node = mini_tree([10, 1,2,3,4,5,6]) print(validate_bst(node)) # False node = BTree.generate([20,10,30,5,15,3,7,15,17]) print(validate_bst(node)) # True # build dependency print("-- Build dependency --") projects = ['a','b','c','d','e','f'] dependencies = [('a', 'd'), ('f', 'b'), ('b', 'd'), ('f', 'a'), ('d', 'c')] print(build_dependency(projects, dependencies)) # f, e, a, b, d, c # first_common_ancestor print('first_common_ancestor') data = [7,5,15,2,6,13,3,4,9,1,11,8,10,0,14,12] tree = BDTree.generate(data) print(tree) node1 = tree.get_node(data=13) node2 = tree.get_node(data=9) node = first_common_ancestor(node1, node2) print(node.data) # check_subtree print('check_subtree: ', end='') data1 = [20,10,50,7,13,36,98,4,8,12,17,28,44,80,127,2,5,9,6,60,79,1] data2 = [4,5,2,6,1] t1 = BTree.generate(data1) t2 = BTree.generate(data2) print(check_subtree(t1, t2)) t3 = BTree.generate(data2, sorted=False) print(check_subtree(t1, t3)) # path_with_sum print('path_with_sum') t = BTree.generate([10,5,15,3,8,12,19,2,4,6,9,0,1,7]) print(path_with_sum(t, 20)) t2 = BTree.generate([2,1,3,1,2,1,3,1,1,3,1,2,1,3,1,1,1,1,2,1]) print(path_with_sum(t2, 5)) # tree_min_max_depth print('tree_min_max_depth') t = BTree.generate([10,5,15,3,8,12,19,2,4,6,9,0,1,7]) print(t) print(tree_min_max_depth(t)) t = BTree.generate([7,4,10,2,5,8,9]) print(t) print(tree_min_max_depth(t))
def check_subtree(t1:BTree, t2:BTree) -> bool: if not t1 or not t2: raise InvalidInputException() r1 = t1.find_node(t2.value) return compare_tree(r1, t2)