96 std::vector<std::shared_ptr<node>> null_children(4,
nullptr);
97 if (root ==
nullptr) {
98 std::vector<int> keys = {key};
99 root = std::make_shared<node>(keys, null_children, 2);
101 parent[root] =
nullptr;
104 std::shared_ptr<node> head = root;
105 while (head !=
nullptr) {
106 if (head->numChildren == 2) {
107 if (head->children[0] ==
nullptr && head->children[1] ==
nullptr) {
108 head->keys.push_back(key);
109 std::sort(head->keys.begin(), head->keys.end());
112 }
else if (key == head->keys[0]) {
114 }
else if (key < head->keys[0]) {
115 head = head->children[0];
116 }
else if (key > head->keys[0]) {
117 head = head->children[1];
119 }
else if (head->numChildren == 3) {
120 if (head->children[0] ==
nullptr && head->children[1] ==
nullptr &&
121 head->children[2] ==
nullptr) {
122 head->keys.push_back(key);
123 std::sort(head->keys.begin(), head->keys.end());
126 }
else if (key == head->keys[0] || key == head->keys[1]) {
128 }
else if (key < head->keys[0]) {
129 head = head->children[0];
130 }
else if (key > head->keys[0] && key < head->keys[1]) {
131 head = head->children[1];
132 }
else if (key > head->keys[1]) {
133 head = head->children[2];
136 std::shared_ptr<node> parent_node = parent[head];
137 if (parent_node ==
nullptr) {
138 if (head->numChildren == 2 || head->numChildren == 3) {
139 std::shared_ptr<node> saved_root = head;
140 std::vector<T> passed_keys = {saved_root->keys[1]};
141 root = std::make_shared<node>(passed_keys, null_children, 2);
142 passed_keys = {saved_root->keys[0]};
143 root->children[0] = std::make_shared<node>(passed_keys, null_children, 2);
144 root->children[0]->index = 0;
145 parent[root->children[0]] = root;
146 passed_keys = {saved_root->keys[2]};
147 root->children[1] = std::make_shared<node>(passed_keys, null_children, 2);
148 root->children[1]->index = 1;
149 parent[root->children[1]] = root;
152 std::shared_ptr<node> saved_root = head;
153 std::vector<T> passed_keys = {saved_root->keys[1]};
154 root = std::make_shared<node>(passed_keys, null_children, 2);
155 passed_keys = {saved_root->keys[0]};
156 root->children[0] = std::make_shared<node>(passed_keys, null_children, 2);
157 parent[root->children[0]] = root;
158 passed_keys = {saved_root->keys[2]};
159 root->children[1] = std::make_shared<node>(passed_keys, null_children, 2);
160 parent[root->children[1]] = root;
161 root->children[0]->children[0] = saved_root->children[0];
162 parent[root->children[0]->children[0]] = root->children[0];
163 root->children[0]->children[1] = saved_root->children[1];
164 parent[root->children[0]->children[1]] = root->children[0];
165 root->children[1]->children[0] = saved_root->children[2];
166 parent[root->children[1]->children[0]] = root->children[1];
167 root->children[1]->children[1] = saved_root->children[3];
168 parent[root->children[1]->children[1]] = root->children[1];
171 }
else if (parent_node->numChildren == 2) {
172 int curr_index = head->index;
173 std::vector<T> saved_keys = head->keys;
174 parent_node->keys.push_back(saved_keys[1]);
175 parent_node->numChildren++;
176 std::sort(parent_node->keys.begin(), parent_node->keys.end());
177 if (curr_index == 0) {
178 parent_node->children[curr_index + 2] =
179 parent_node->children[curr_index + 1];
181 std::vector<T> passed_keys = {saved_keys[0]};
182 parent_node->children[curr_index] =
183 std::make_shared<node>(passed_keys, null_children, 2);
184 parent[parent_node->children[curr_index]] = parent_node;
185 parent_node->children[curr_index]->index = curr_index;
186 passed_keys = {saved_keys[2]};
187 parent_node->children[curr_index + 1] =
188 std::make_shared<node>(passed_keys, null_children, 2);
189 parent[parent_node->children[curr_index + 1]] = parent_node;
190 parent_node->children[curr_index + 1]->index = curr_index + 1;
192 }
else if (parent_node->numChildren == 3) {
193 int curr_index = head->index;
194 std::vector<T> saved_keys = head->keys;
195 parent_node->keys.push_back(saved_keys[1]);
196 parent_node->numChildren++;
197 std::sort(parent_node->keys.begin(), parent_node->keys.end());
198 if (curr_index == 0) {
199 parent_node->children[curr_index + 3] =
200 parent_node->children[curr_index + 2];
201 parent_node->children[curr_index + 2] =
202 parent_node->children[curr_index + 1];
203 }
else if (curr_index == 1) {
204 parent_node->children[curr_index + 2] =
205 parent_node->children[curr_index + 1];
207 std::vector<T> passed_keys = {saved_keys[0]};
208 parent_node->children[curr_index] =
209 std::make_shared<node>(passed_keys, null_children, 2);
210 parent_node->children[curr_index]->index = curr_index;
211 parent[parent_node->children[curr_index]] = parent_node;
212 passed_keys = {saved_keys[2]};
213 parent_node->children[curr_index + 1] =
214 std::make_shared<node>(passed_keys, null_children, 2);
215 parent[parent_node->children[curr_index + 1]] = parent_node;
216 parent_node->children[curr_index + 1]->index = curr_index + 1;
217 if (key == parent_node->keys[0] || key == parent_node->keys[1] ||
218 key == parent_node->keys[2]) {
220 }
else if (key < parent_node->keys[0]) {
221 head = parent_node->children[0];
222 }
else if (key > parent_node->keys[0] && key < parent_node->keys[1]) {
223 head = parent_node->children[1];
224 }
else if (key > parent_node->keys[1] && key < parent_node->keys[2]) {
225 head = parent_node->children[2];
226 }
else if (key > parent_node->keys[2]) {
227 head = parent_node->children[3];