diff --git a/A.txt b/A.txt index 76d8daadef93daad04fd2da4ae66382cf11b5f2a..137e470567da1e02f27ef5d69a44315c3d091284 100644 --- a/A.txt +++ b/A.txt @@ -2,20 +2,21 @@ function insert(root, key) cursor = traverse until found leaf right_split = null - do + while True if cursor is full if cursor is a leaf - key, right_split = split_leaf(cursor) + key, right_split = split_leaf(cursor, key) else - key, right_split = split_internal(cursor) + key, right_split = split_internal(cursor, key, right_split) + if cursor is root: - increase_height(cursor, key, right_split) + increase_height(root, key, right_split) + exit loop else insert key in cursor.keys - if right_split not null + if cursor is not a leaf insert right_split in cursor.children exit loop - while cursor != root end @@ -24,5 +25,7 @@ function increase_height(root, key, right_split) left.is_leaf = right_split.is_leaf left.keys = root.keys left.children = root.children + if left is a leaf + fix right_split left link to left root.keys = [key] root.children = [left, right_split] diff --git a/B.txt b/B.txt new file mode 100644 index 0000000000000000000000000000000000000000..c1a128dff21e077ba652cb75051fa30a327fc3de --- /dev/null +++ b/B.txt @@ -0,0 +1,14 @@ +function insert(root, key) + leaf_node = find_leaf_node(root, key) + + if length(keys of leaf_node) > 2 * order of lead_node then + insert_full(root, lead_node, key, null) + else + insert_non_full(leaf_node, key, null) + +function insert_non_full(node, key, right_child_node) + +function insert_full(root, node, key, right_child_node) + +function find_parent(root, child_node) + diff --git a/__main__.py b/__main__.py index ee02184493a7a09cc5a7d13aeb7dd3122c1d46c9..9716e4be1c3672e5f87a8e3ae243cbd4113b9aa7 100644 --- a/__main__.py +++ b/__main__.py @@ -4,14 +4,36 @@ class Node: - def __init__(self, t): - self.t = t + def __init__(self, order): + self.order = order self.keys = [] self.children = [] self.is_leaf = True -d = 2 +def node_is_full(node): + return len(node.keys) == 2 * node.order + + +# lower_bound +def array_binary_search(array, value): + low = 0 + high = len(array) - 1 + + while low <= high: + m = (low + high) // 2 + if array[m] < value: + low = m + 1 + elif array[m] > value: + high = m - 1 + else: + return m + return low + + +def array_insert_sorted(array, value): + index = array_binary_search(array, value) + array.insert(index, value) def insert(root, key): @@ -19,79 +41,25 @@ def insert(root, key): while not cursor.is_leaf: # for i in range(len(cursor.keys)): pass - key_to_insert = key - free_node = None while True: - if len(cursor.keys) == 2 * d: - index = -1 - for i in range(len(cursor.keys)): - if key_to_insert < cursor.keys[i]: - index = i - break - if i == len(cursor.keys) - 1: - index = i + 1 - break - split_index = len(cursor.keys) // 2 - tmp = key_to_insert - if index != split_index: - key_to_insert = cursor.keys[split_index] - right = Node(cursor.t) - right.keys = cursor.keys[split_index:] - cursor.keys = cursor.keys[:split_index] - free_node = right - - if tmp >= free_node.keys[0]: - index = -1 - for i in range(len(free_node.keys)): - if tmp < free_node.keys[i]: - index = i - break - if i == len(free_node.keys) - 1: - index = i + 1 - break - print(index) - free_node.keys.insert(index, tmp) - else: - pass - # insert in cursor - - if cursor == root: - left = Node(cursor.t) - left.keys = root.keys - root.keys = [key_to_insert] - root.children = [left, free_node] + if node_is_full(cursor): + pass else: - if not cursor.keys: - cursor.keys = [key_to_insert] - else: - index = -1 - for i in range(len(cursor.keys)): - if key_to_insert < cursor.keys[i]: - index = i - break - if i == len(cursor.keys) - 1: - index = i + 1 - break - cursor.keys.insert(index, key_to_insert) - break - - if cursor == root: + array_insert_sorted(cursor.keys, key) break + pass def main(): - t = 2 - root = Node(t) + order = 2 + root = Node(order) insert(root, 20) - insert(root, 40) - insert(root, 5) - insert(root, 3) - insert(root, 25) - print(root.keys) - print(root.children[0].keys) - print(root.children[1].keys) + # insert(root, 40) + # insert(root, 5) + # insert(root, 3) + # insert(root, 25) if __name__ == "__main__":