Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
    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)