Skip to content

Commit 1578595

Browse files
author
fuli
committed
add comment for btree
1 parent 2ea1ea3 commit 1578595

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

include/btree.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ namespace alg {
106106
int32_t i = 0;
107107
while (i<x->n && k > x->key[i]) i++;
108108

109-
if (i<x->n && k == x->key[i]) {
109+
if (i<x->n && k == x->key[i]) { // search in [0,n-1]
110110
return i;
111-
} else if (x->flag & LEAF) {
111+
} else if (x->flag & LEAF) { // leaf, no more childs
112112
return -1;
113113
} else {
114-
std::auto_ptr<node_t> xi(READ(x, i));
114+
std::auto_ptr<node_t> xi(READ(x, i)); // in last child
115115
return search(xi.get(), k);
116116
}
117117
}
@@ -121,8 +121,8 @@ namespace alg {
121121
*/
122122
void insert_nonfull(node x, int32_t k) {
123123
int32_t i = x->n-1;
124-
if (x->flag & LEAF) {
125-
while (i>=0 && k <x->key[i]) {
124+
if (x->flag & LEAF) { // insert into leaf
125+
while (i>=0 && k < x->key[i]) { // shift from right to left, when k < key[i]
126126
x->key[i+1] = x->key[i];
127127
i = i - 1;
128128
}
@@ -173,28 +173,26 @@ namespace alg {
173173
z->n = T - 1;
174174

175175
int32_t j;
176-
for (j=0;j<T-1;j++) { // init z
176+
for (j=0;j<T-1;j++) { // init z, t-1 keys
177177
z->key[j] = y->key[j+T];
178178
}
179179

180-
if (!(y->flag & LEAF)) {
180+
if (!(y->flag & LEAF)) { // if not leaf, copy childs too.
181181
for (j=0;j<T;j++) {
182182
z->c[j] = y->c[j+T];
183183
}
184184
}
185185

186-
y->n = T-1; // splited y
186+
y->n = T-1; // shrink y to t-1 elements
187187
WRITE(y.get());
188188
WRITE(z.get());
189189

190-
for (j=x->n;j>=i+1;j--) {
191-
x->c[j+1] = x->c[j]; // shift
190+
for (j=x->n;j>=i+1;j--) { // make place for the new child in x
191+
x->c[j+1] = x->c[j];
192192
}
193193

194-
// save z
195-
x->c[i+1] = z->offset;
196-
197-
for (j=x->n-1;j>=i;j--) {
194+
x->c[i+1] = z->offset; // make z the child of x
195+
for (j=x->n-1;j>=i;j--) { // move keys in x
198196
x->key[j+1] = x->key[j];
199197
}
200198
x->key[i] = y->key[T-1];

0 commit comments

Comments
 (0)