Skip to content
Snippets Groups Projects
Commit b4f49d51 authored by Florian Burgener's avatar Florian Burgener
Browse files

Duplicates should no work

parent b4337e59
Branches
No related tags found
No related merge requests found
......@@ -39,12 +39,14 @@ def array_insert_sorted(array, value):
def insert(root, key):
leaf_node = root
parents = []
while not leaf_node.is_leaf:
parents.append(leaf_node)
index = array_binary_search(leaf_node.keys, key)
leaf_node = leaf_node.children[index]
if node_is_full(leaf_node):
insert_full(root, leaf_node, key, None)
insert_full(root, parents, leaf_node, key, None)
else:
insert_non_full(leaf_node, key, None)
......@@ -56,7 +58,7 @@ def insert_non_full(node, key, right_child_node):
node.children.insert(inserted_at_index + 1, right_child_node)
def insert_full(root, node, key, right_child_node):
def insert_full(root, parents, node, key, right_child_node):
if node.is_leaf:
abc, split_right = split_leaf(node, key)
else:
......@@ -65,10 +67,10 @@ def insert_full(root, node, key, right_child_node):
if node == root:
increase_height(root, abc, split_right)
else:
parent = find_parent(root, node)
parent = parents.pop()
if node_is_full(parent):
insert_full(root, parent, abc, split_right)
insert_full(root, parents, parent, abc, split_right)
else:
insert_non_full(parent, abc, split_right)
......@@ -110,7 +112,6 @@ def split_internal(node, key, right_child_node):
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
......@@ -127,6 +128,10 @@ def split_leaf(node, key):
else:
abc = key
if key == node.keys[len(node.keys) // 2]:
# I don't like it but without it the duplicates can't work.
split_index = len(node.keys) // 2
split_right = Node(node.order)
split_right.is_leaf = node.is_leaf
split_right.keys = node.keys[split_index:]
......@@ -136,6 +141,7 @@ def split_leaf(node, key):
array_insert_sorted(node.keys, key)
else:
array_insert_sorted(split_right.keys, key)
return abc, split_right
......@@ -149,15 +155,6 @@ def increase_height(root, key, right_child_node):
root.children = [left_child_node, right_child_node]
def find_parent(root, node):
while not root.is_leaf:
for child in root.children:
if node == child:
return root
root = root.children[array_binary_search(root.keys, node.keys[0])]
return None
def tree_print(root, depth=0):
if len(root.keys) == 0:
return
......@@ -171,10 +168,20 @@ def tree_print(root, depth=0):
def main():
order = 2
root = Node(order)
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]
# keys = [10, 20, 50, 70, 1, 11, 40, 30, 90, 60, 110, 80, 15, 54, 42, 41, 12, 14, 16, 19, 20, 17, 18]
keys = [10, 20, 50, 70, 1, 11, 40, 30, 90, 60, 110]
for key in keys:
insert(root, key)
insert(root, 80)
# insert(root, 42)
# insert(root, 42)
# insert(root, 42)
insert(root, 20)
insert(root, 20)
insert(root, 20)
# insert(root, 20)
tree_print(root)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment