Data Structures/B-Tree
treebalancedmultiway
Press play to start
1void btree_insert(BTree* t, int val) {
2 if (t->root->num_keys == MAX_KEYS) {
3 BNode* s = new_node();
4 s->children[0] = t->root;
5 split_child(s, 0); t->root = s;
6 }
7 BNode* curr = t->root;
8 while (!curr->is_leaf) {
9 int i = find_index(curr, val);
10 if (curr->children[i]->num_keys == MAX_KEYS)
11 { split_child(curr, i); if (val > curr->keys[i]) i++; }
12 curr = curr->children[i];
13 }
14 insert_key(curr, val);
15}
Step 1/0

Practice

LeetCode·#700 Search in a BSTEasyLeetCode·#938 Range Sum of BSTEasy
OperationBestAverageWorst
searchO(log n)O(log n)O(log n)
insertO(log n)O(log n)O(log n)
SpaceO(n)