From b11c60f896a941c055f05c81b088a0135f2adf38 Mon Sep 17 00:00:00 2001
From: Florian Burgener <florian.brgnr@gmail.com>
Date: Wed, 11 May 2022 18:25:03 +0200
Subject: [PATCH] Save

---
 __main__.py | 117 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 103 insertions(+), 14 deletions(-)

diff --git a/__main__.py b/__main__.py
index 9716e4b..6bb6bba 100644
--- a/__main__.py
+++ b/__main__.py
@@ -6,9 +6,9 @@
 class Node:
     def __init__(self, order):
         self.order = order
+        self.is_leaf = True
         self.keys = []
         self.children = []
-        self.is_leaf = True
 
 
 def node_is_full(node):
@@ -34,32 +34,121 @@ def array_binary_search(array, value):
 def array_insert_sorted(array, value):
     index = array_binary_search(array, value)
     array.insert(index, value)
+    return index
 
 
 def insert(root, key):
-    cursor = root
-    while not cursor.is_leaf:
-        # for i in range(len(cursor.keys)):
-        pass
+    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):
+        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 node_is_full(cursor):
+    if right_child_node is not None:
+        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
         else:
-            array_insert_sorted(cursor.keys, key)
-            break
-            pass
+            insert_non_full(parent, abc, split_right)
+
+
+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():
     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)
+
     print(root.keys)
-    # insert(root, 40)
-    # insert(root, 5)
-    # insert(root, 3)
-    # insert(root, 25)
+    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)
 
 
 if __name__ == "__main__":
-- 
GitLab