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 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 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_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 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()}')
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 :(")