Skip to content
Snippets Groups Projects
Commit 94870ad9 authored by florian.burgener's avatar florian.burgener
Browse files

Refactoring

parent b4f49d51
No related branches found
No related tags found
No related merge requests found
...@@ -37,18 +37,23 @@ def array_insert_sorted(array, value): ...@@ -37,18 +37,23 @@ def array_insert_sorted(array, value):
return index return index
def insert(root, key): def find_leaf(root, key):
leaf_node = root
parents = [] parents = []
while not leaf_node.is_leaf: current = root
parents.append(leaf_node) while not current.is_leaf:
index = array_binary_search(leaf_node.keys, key) parents.append(current)
leaf_node = leaf_node.children[index] children_index = array_binary_search(current.keys, key)
current = current.children[children_index]
return parents, current
def insert(root, key):
parents, leaf = find_leaf(root, key)
if node_is_full(leaf_node): if node_is_full(leaf):
insert_full(root, parents, leaf_node, key, None) insert_full(root, parents, leaf, key, None)
else: else:
insert_non_full(leaf_node, key, None) insert_non_full(leaf, key, None)
def insert_non_full(node, key, right_child_node): def insert_non_full(node, key, right_child_node):
...@@ -156,8 +161,6 @@ def increase_height(root, key, right_child_node): ...@@ -156,8 +161,6 @@ def increase_height(root, key, right_child_node):
def tree_print(root, depth=0): def tree_print(root, depth=0):
if len(root.keys) == 0:
return
print(" " * depth, end="") print(" " * depth, end="")
print(root.keys) print(root.keys)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment