@@ -48,6 +48,12 @@ namespace alg {
48
48
} __attribute__ ((packed));
49
49
typedef struct node_t *node;
50
50
51
+ public:
52
+ // node and index
53
+ struct nr {
54
+ uint32_t offset;
55
+ int32_t idx;
56
+ };
51
57
private:
52
58
node m_root;
53
59
int fd;
@@ -72,7 +78,7 @@ namespace alg {
72
78
close (fd);
73
79
}
74
80
75
- int32_t Search (int32_t x) {
81
+ nr Search (int32_t x) {
76
82
return search (m_root, x);
77
83
}
78
84
@@ -99,18 +105,28 @@ namespace alg {
99
105
}
100
106
}
101
107
108
+ void DeleteKey (int32_t k) {
109
+ node x = m_root;
110
+ delete_op (m_root, k);
111
+ }
112
+
102
113
private:
103
114
/* *
104
115
* search a key, returns node and index
105
116
*/
106
- int32_t search (node x, int32_t k) {
117
+ nr search (node x, int32_t k) {
107
118
int32_t i = 0 ;
119
+ nr ret;
108
120
while (i<x->n && k > x->key [i]) i++;
109
121
110
122
if (i<x->n && k == x->key [i]) { // search in [0,n-1]
111
- return i;
123
+ ret.offset = x->offset ;
124
+ ret.idx = i;
125
+ return ret;
112
126
} else if (x->flag & LEAF) { // leaf, no more childs
113
- return -1 ;
127
+ ret.offset = 0 ;
128
+ ret.idx = -1 ;
129
+ return ret;
114
130
} else {
115
131
std::auto_ptr<node_t > xi (READ (x, i)); // in last child
116
132
return search (xi.get (), k);
@@ -200,6 +216,57 @@ namespace alg {
200
216
WRITE (x);
201
217
}
202
218
219
+ /* *
220
+ * recursion deletion
221
+ */
222
+ void delete_op (node x, int32_t k) {
223
+ int32_t i;
224
+ for (i=0 ;i<x->n ;i++) {
225
+ if (x->key [i] == k) { // leaf node, case1
226
+ break ;
227
+ }
228
+ }
229
+
230
+ if (i<x->n && (x->flag & LEAF)) {
231
+ int j;
232
+ for (j = i;j<x->n -1 ;j++) { // shift copy
233
+ x->key [j] = x->key [j+1 ];
234
+ }
235
+ WRITE (x);
236
+ } else if (x->key [i] == k) { // non-leaf
237
+ if (i = x->n -1 || i == 0 ) { // outside, case 2c
238
+ delete_case3 (x);
239
+ } else {
240
+ node y= READ (x, i-1 );
241
+ if (y->n >= T) { // case 2a
242
+ x->key [i] = y->key [y->n -1 ]; // subsitute the key with predecessor
243
+ delete_op (y, x->key [i]);
244
+ free (y);
245
+ return ;
246
+ }
247
+
248
+ node z = READ (x, i+1 );
249
+ if (z->n >= T) { // case 2b
250
+ x->key [i] = z->key [0 ];
251
+ delete_op (z, x->key [i]);
252
+ return ;
253
+ }
254
+
255
+ // case 2c:
256
+ if (y->n == T-1 && z->n == T-1 ) {
257
+
258
+ }
259
+ delete_case3 (x);
260
+ }
261
+ }
262
+ }
263
+
264
+ /* *
265
+ * delete case3
266
+ */
267
+ void delete_case3 (node x) {
268
+ }
269
+
203
270
/* *
204
271
* Read a 4K-block from disk, and returns the node struct.
205
272
*/
0 commit comments