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

Save

parent 210d6230
No related branches found
No related tags found
No related merge requests found
...@@ -6,9 +6,9 @@ ...@@ -6,9 +6,9 @@
class Node: class Node:
def __init__(self, order): def __init__(self, order):
self.order = order self.order = order
self.is_leaf = True
self.keys = [] self.keys = []
self.children = [] self.children = []
self.is_leaf = True
def node_is_full(node): def node_is_full(node):
...@@ -34,32 +34,121 @@ def array_binary_search(array, value): ...@@ -34,32 +34,121 @@ def array_binary_search(array, value):
def array_insert_sorted(array, value): def array_insert_sorted(array, value):
index = array_binary_search(array, value) index = array_binary_search(array, value)
array.insert(index, value) array.insert(index, value)
return index
def insert(root, key): def insert(root, key):
cursor = root leaf_node = root
while not cursor.is_leaf: while not leaf_node.is_leaf:
# for i in range(len(cursor.keys)): index = array_binary_search(leaf_node.keys, key)
pass
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):
insert_full(root, leaf_node, key, None)
else:
insert_non_full(leaf_node, key, None)
def insert_non_full(node, key, right_child_node):
inserted_at_index = array_insert_sorted(node.keys, key)
while True: if right_child_node is not None:
if node_is_full(cursor): node.children.insert(inserted_at_index + 1, right_child_node)
def insert_full(root, node, key, right_child_node):
if node.is_leaf:
abc, split_right = split_leaf(node, key)
else:
abc, split_right = split_internal(node, key, right_child_node)
if node == root:
increase_height(root, abc, split_right)
else:
parent = find_parent(root, node)
if node_is_full(parent):
print("adsfasdf")
pass pass
else: else:
array_insert_sorted(cursor.keys, key) insert_non_full(parent, abc, split_right)
break
pass
def split_internal(node, key, right_child_node):
pass
def split_leaf(node, key):
index = array_binary_search(node.keys, key)
split_index = len(node.keys) // 2
if index < split_index:
split_index -= 1
abc = node.keys[split_index]
elif index > split_index:
abc = node.keys[split_index]
else:
abc = node.keys[split_index]
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:
array_insert_sorted(node.keys, key)
else:
array_insert_sorted(split_right.keys, key)
return abc, split_right
def increase_height(root, key, right_child_node):
left_child_node = Node(root.order)
left_child_node.is_leaf = right_child_node.is_leaf
left_child_node.keys = root.keys
left_child_node.children = root.children
root.is_leaf = False
root.keys = [key]
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 main(): def main():
order = 2 order = 2
root = Node(order) root = Node(order)
insert(root, 10)
insert(root, 20) 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)
print(root.keys) print(root.keys)
# insert(root, 40) print(root.children[0].keys)
# insert(root, 5) print(root.children[1].keys)
# insert(root, 3) # print(root.children[2].keys)
# insert(root, 25) # print(root.children[3].keys)
# print(root.children[4].keys)
if __name__ == "__main__": if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment