B_trees

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// here all b tree nodes allocated in heap, not disk.
namespace CLRS
{
  template <typename T, std::size_t D> class BTree;
  template <typename T, std::size_t D> class BTreeNode;

  template <typename T, std::size_t D>
  std::pair<std::shared_ptr<BTreeNode<T, D>>, std::size_t>
  b_tree_search(const std::shared_ptr<BTreeNode<T, D>> &x, T k);
  template <typename T, std::size_t D>
  void b_tree_create(BTree<T, D> *t);
  template <typename T, std::size_t D>
  void b_tree_split_child(const std::shared_ptr<BTreeNode<T, D>> &x,
			  std::size_t i);
  template <typename T, std::size_t D>
  void b_tree_insert(BTree<T, D> *t, T k);
  template <typename T, std::size_t D>
  void b_tree_insert_nonfull(const std::shared_ptr<BTreeNode<T, D>> &x,
			     T k);
  template <typename T, std::size_t D>
  void b_tree_delete(BTree<T, D> *t,
		     const std::shared_ptr<BTreeNode<T, D>> &x,
		     T k);

  template <typename T, std::size_t D>
  class BTree
  {
    std::shared_ptr<BTreeNode<T, D>> root;
  public:
    BTree() = default;
    std::shared_ptr<BTreeNode<T, D>> get_root() {return root;}
    void set_root(std::shared_ptr<BTreeNode<T, D>> r) {root = r;}
  };

  template <typename T, std::size_t D>
  class BTreeNode
  {
    std::size_t n = 0;
    // default minimum degree specified by template parameter D
    std::size_t t = D;
    bool leaf = true;
    std::vector<T> keys;
    std::vector<std::shared_ptr<BTreeNode>> childs;
  public:
    BTreeNode() = default;
    // constructor only for unittest use
    BTreeNode(const std::vector<T> &vk): n(vk.size()), keys(vk) {}
    std::size_t get_n() const {return n;}
    void set_n(std::size_t m) {n = m;}
    // minimum D is set in constructor once and for all
    std::size_t get_t() const {return t;}
    bool get_leaf() const {return leaf;}
    void set_leaf(bool l) {leaf = l;}
    T get_key(std::size_t i) const {return keys[i];}
    void push_back_key(T k) {keys.push_back(k);}
    void insert_key(T k, std::size_t i)
    {auto it = keys.begin() + i; keys.insert(it, k);}
    void delete_key(std::size_t i)
    {auto it = keys.begin() + i; keys.erase(it);}
    void replace_key(T k, std::size_t i) {keys[i] = k;}
    std::shared_ptr<BTreeNode> get_child(std::size_t i) const
    {return childs[i];}
    void push_back_child(std::shared_ptr<BTreeNode> n)
    {childs.push_back(n);}
    void insert_child(std::shared_ptr<BTreeNode> n, std::size_t i)
    {auto it = childs.begin() + i; childs.insert(it, n);}
    void delete_child(std::size_t i)
    {auto it = childs.begin() + i; childs.erase(it);}
  };

  template <typename T, std::size_t D>
  void b_tree_create(BTree<T, D> *t)
  {
    std::shared_ptr<BTreeNode<T, D>> x =
      std::make_shared<BTreeNode<T, D>>();
    t->set_root(x);
  }

  template <typename T, std::size_t D>
  std::pair<std::shared_ptr<BTreeNode<T, D>>, std::size_t>
  b_tree_search(const std::shared_ptr<BTreeNode<T, D>> &x, T k)
  {
    std::size_t i = 1;
    while(i <= x->get_n() && k > x->get_key(i - 1))
      ++i;
    if(i <= x->get_n() && k == x->get_key(i - 1))
      return std::make_pair(x, i - 1);
    else if(x->get_leaf())
      return std::make_pair(nullptr, 0);
    else
      return b_tree_search(x->get_child(i - 1), k);
  }

  template <typename T, std::size_t D>
  void b_tree_split_child(const std::shared_ptr<BTreeNode<T, D>> &x,
			  std::size_t i)
  {
    auto z = std::make_shared<BTreeNode<T, D>>();
    auto y = x->get_child(i);
    z->set_leaf(y->get_leaf());
    z->set_n(D - 1);
    for(std::size_t j = 1; j <= D - 1; ++j)
      z->push_back_key(y->get_key(j + D - 1));
    if(!y->get_leaf())
      for(std::size_t j = 1; j <= D; ++j)
	z->push_back_child(y->get_child(j + D - 1));
    x->insert_child(z, i + 1);
    x->insert_key(y->get_key(D - 1), i);
    y->delete_key(D - 1);
    y->set_n(D - 1);
    x->set_n(x->get_n() + 1);
  }

  template <typename T, std::size_t D>
  void b_tree_insert(BTree<T, D> *t, T k)
  {
    auto r = t->get_root();
    if(r->get_n() == 2 * D - 1)
      {
	auto s = std::make_shared<BTreeNode<T, D>>();
	t->set_root(s);
	s->set_leaf(false);
	s->push_back_child(r);
	b_tree_split_child(s, 0);
	b_tree_insert_nonfull(s, k);
      }
    else
      b_tree_insert_nonfull(r, k);
  }

  template <typename T, std::size_t D>
  void b_tree_insert_nonfull(const std::shared_ptr<BTreeNode<T, D>> &x, T k)
  {
    std::size_t i = x->get_n();
    if(x->get_leaf())
      {
	while(i >= 0 && k < x->get_key(i - 1))
	  --i;
	x->insert_key(k, i);
	x->set_n(x->get_n() + 1);
      }
    else
      {
	while(i >= 0 && k < x->get_key(i - 1))
	  --i;
	if(x->get_child(i)->get_n() == 2 * D - 1)
	  {
	    b_tree_split_child(x, i);
	    if(k > x->get_key(i))
	      ++i;
	  }
	b_tree_insert_nonfull(x->get_child(i), k);
      }
  }

  template <typename T, std::size_t D>
  void b_tree_delete(BTree<T, D> *t, const std::shared_ptr<BTreeNode<T, D>> &x, T k)
  {
    std::size_t i = 1;
    while(i <= x->get_n() && k > x->get_key(i - 1))
      ++i;
    if(i <= x->get_n() && k == x->get_key(i - 1))
      {
	// case 1: leaf node
	if(x->get_leaf())
	  {
	    x->delete_key(i - 1);
	    x->set_n(x->get_n() - 1);
	  }
	else
	  {
	    // case 2a
	    if(x->get_child(i - 1)->get_n() >= D)
	      {
		auto y = x->get_child(i - 1);
		T j = y->get_key(y->get_n() - 1);
		b_tree_delete(t, y, j);
		x->replace_key(j, i - 1);
	      }
	    // case 2b which is opposite to 2a
	    else if(x->get_child(i)->get_n() >= D)
	      {
		auto z = x->get_child(i);
		T j = z->get_key(0);
		b_tree_delete(t, z, j);
		x->replace_key(j, i - 1);
	      }
	    // case 2c
	    else
	      {
		auto y = x->get_child(i - 1);
		auto z = x->get_child(i);
		y->push_back_key(k);
		y->set_n(y->get_n() + 1);
		x->delete_key(i - 1);
		x->delete_child(i);
		x->set_n(x->get_n() - 1);
		for(std::size_t n = 0; n < z->get_n(); ++n)
		  y->push_back_key(z->get_key(n));
		y->set_n(z->get_n() + y->get_n());
		if(!y->get_leaf())
		  for(std::size_t n = 0; n <= z->get_n(); ++n)
		    y->push_back_child(z->get_child(n));
		b_tree_delete(t, y, k);
	      }
	  }
      }
    else
      {
	auto self = x->get_child(i - 1);
	std::shared_ptr<BTreeNode<T, D>> brother;
	// case 3a
	if(self->get_n() == D - 1)
	  {
	    if(i - 1 > 0 && x->get_child(i - 2)->get_n() >= D)
	      {
		brother = x->get_child(i - 2);
		std::size_t bl = brother->get_n();
		T down = x->get_key(i - 2);
		T up = brother->get_key(bl - 1);
		self->insert_key(down, 0);
		self->set_n(self->get_n() + 1);
		x->replace_key(up, i - 2);
		if(!brother->get_leaf())
		  {
		    self->insert_child(brother->get_child(bl), 0);
		    brother->delete_child(bl);
		  }
		brother->delete_key(bl - 1);
		brother->set_n(bl - 1);
	      }
	    else if(i <= x->get_n() && x->get_child(i)->get_n() >= D)
	      {
		brother = x->get_child(i);
		std::size_t bl = brother->get_n();
		T down = x->get_key(i - 1);
		T up = brother->get_key(0);
		self->push_back_key(down);
		self->set_n(x->get_n() + 1);
		x->replace_key(up, i - 1);
		if(!brother->get_leaf())
		  {
		    self->push_back_child(brother->get_child(0));
		    brother->delete_child(0);
		  }
		brother->delete_key(0);
		brother->set_n(bl - 1);
	      }
	    // case 3b
	    else
	      {
		if(i - 1 == 0)
		  {
		    brother = x->get_child(i);
		    T down = x->get_key(i - 1);
		    self->push_back_key(down);
		    self->set_n(self->get_n() + 1);
		    for(std::size_t i = 0; i < brother->get_n(); ++i)
		      self->push_back_key(brother->get_key(i));
		    if(!self->get_leaf())
		      for(std::size_t i = 0; i <= brother->get_n(); ++i)
			self->push_back_child(brother->get_child(i));
		    self->set_n(self->get_n() + brother->get_n());
		    x->delete_key(i - 1);
		    x->delete_child(i);
		    x->set_n(x->get_n() - 1);
		    if(x->get_n() == 0)
		      t->set_root(self);
		  }
		else
		  {
		    brother = x->get_child(i - 2);
		    T down = x->get_key(i - 2);
		    brother->push_back_key(down);
		    brother->set_n(brother->get_n() + 1);
		    for(std::size_t i = 0; i < self->get_n(); ++i)
		      brother->push_back_key(self->get_key(i));
		    if(!brother->get_leaf())
		      for(std::size_t i = 0; i <= self->get_n(); ++i)
			brother->push_back_child(self->get_child(i));
		    brother->set_n(self->get_n() + brother->get_n());
		    x->delete_key(i - 2);
		    x->delete_child(i - 1);
		    x->set_n(x->get_n() - 1);
		    if(x->get_n() == 0)
		      t->set_root(brother);
		    self = brother;
		  }
	      }
	  }
	b_tree_delete(t, self, k);
      }
  }
}