diff --git a/__main__.py b/__main__.py index 6bb6bba85b2f6dd5430c42050ecf840ffb9b0fe3..8a2042cae7260896a6e4b3aebce415f93d184081 100644 --- a/__main__.py +++ b/__main__.py @@ -41,9 +41,6 @@ def insert(root, key): leaf_node = root while not leaf_node.is_leaf: index = array_binary_search(leaf_node.keys, key) - - if len(leaf_node.keys) > index and leaf_node.keys[index] == key: - index += 1 leaf_node = leaf_node.children[index] if node_is_full(leaf_node): @@ -71,19 +68,55 @@ def insert_full(root, node, key, right_child_node): parent = find_parent(root, node) if node_is_full(parent): - print("adsfasdf") - pass + insert_full(root, parent, abc, split_right) else: insert_non_full(parent, abc, split_right) def split_internal(node, key, right_child_node): - pass + index = array_binary_search(node.keys, key) + split_index = len(node.keys) // 2 + left_index = split_index + right_index = split_index + + if index < split_index: + abc = node.keys[split_index - 1] + left_index -= 1 + elif index > split_index: + abc = node.keys[split_index] + right_index += 1 + else: + abc = key + + split_right = Node(node.order) + split_right.is_leaf = node.is_leaf + split_right.keys = node.keys[right_index:] + node.keys = node.keys[:left_index] + + if key < abc: + inserted_at_index = array_insert_sorted(node.keys, key) + elif key > abc: + inserted_at_index = array_insert_sorted(split_right.keys, key) + + if index < split_index: + split_right.children = node.children[split_index:] + node.children = node.children[:split_index] + node.children.insert(inserted_at_index + 1, right_child_node) + elif index > split_index: + split_right.children = node.children[split_index + 1 :] + node.children = node.children[: split_index + 1] + split_right.children.insert(inserted_at_index + 1, right_child_node) + else: + split_right.children = node.children[split_index + 1 :] + node.children = node.children[: split_index + 1] + split_right.children.insert(0, right_child_node) + pass + + return abc, split_right def split_leaf(node, key): index = array_binary_search(node.keys, key) - split_index = len(node.keys) // 2 if index < split_index: @@ -92,14 +125,14 @@ def split_leaf(node, key): elif index > split_index: abc = node.keys[split_index] else: - abc = node.keys[split_index] + abc = key split_right = Node(node.order) split_right.is_leaf = node.is_leaf split_right.keys = node.keys[split_index:] node.keys = node.keys[:split_index] - if index < split_index: + if key < abc: array_insert_sorted(node.keys, key) else: array_insert_sorted(split_right.keys, key) @@ -125,30 +158,25 @@ def find_parent(root, node): return None +def tree_print(root, depth=0): + if len(root.keys) == 0: + return + print(" " * depth, end="") + print(root.keys) + + for child in root.children: + tree_print(child, depth + 1) + + def main(): order = 2 root = Node(order) - insert(root, 10) - insert(root, 20) - insert(root, 30) - insert(root, 40) - insert(root, 41) - # insert(root, 17) - # insert(root, 15) - # insert(root, 13) - # insert(root, 22) - # insert(root, 45) - # insert(root, 27) - # insert(root, 29) - # insert(root, 14) - # insert(root, 16) + keys = [10, 20, 50, 70, 1, 11, 40, 30, 90, 60, 110, 80, 15, 54, 42, 41, 12, 14, 16, 19, 20, 120, 130, 140, 150, 160, 161] - print(root.keys) - print(root.children[0].keys) - print(root.children[1].keys) - # print(root.children[2].keys) - # print(root.children[3].keys) - # print(root.children[4].keys) + for key in keys: + insert(root, key) + + tree_print(root) if __name__ == "__main__":