-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathBTree.cs
751 lines (597 loc) · 21.3 KB
/
BTree.cs
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Advanced.Algorithms.DataStructures;
/// <summary>
/// A B-tree implementation.
/// </summary>
public class BTree<T> : IEnumerable<T> where T : IComparable
{
private readonly int maxKeysPerNode;
private readonly int minKeysPerNode;
internal BTreeNode<T> Root;
public BTree(int maxKeysPerNode)
{
if (maxKeysPerNode < 3) throw new Exception("Max keys per node should be atleast 3.");
this.maxKeysPerNode = maxKeysPerNode;
minKeysPerNode = maxKeysPerNode / 2;
}
public int Count { get; private set; }
/// <summary>
/// Time complexity: O(log(n)).
/// </summary>
public T Max
{
get
{
if (Root == null) return default;
var maxNode = FindMaxNode(Root);
return maxNode.Keys[maxNode.KeyCount - 1];
}
}
/// <summary>
/// Time complexity: O(log(n)).
/// </summary>
public T Min
{
get
{
if (Root == null) return default;
var minNode = FindMinNode(Root);
return minNode.Keys[0];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return new BTreeEnumerator<T>(Root);
}
/// <summary>
/// Time complexity: O(log(n)).
/// </summary>
public bool HasItem(T value)
{
return Find(Root, value) != null;
}
/// <summary>
/// Find the value node under given node.
/// </summary>
private BTreeNode<T> Find(BTreeNode<T> node, T value)
{
//if leaf then its time to insert
if (node.IsLeaf)
{
for (var i = 0; i < node.KeyCount; i++)
if (value.CompareTo(node.Keys[i]) == 0)
return node;
}
else
{
//if not leaf then drill down to leaf
for (var i = 0; i < node.KeyCount; i++)
{
if (value.CompareTo(node.Keys[i]) == 0) return node;
//current value is less than new value
//drill down to left child of current value
if (value.CompareTo(node.Keys[i]) < 0) return Find(node.Children[i], value);
//current value is grearer than new value
//and current value is last element
if (node.KeyCount == i + 1) return Find(node.Children[i + 1], value);
}
}
return null;
}
/// <summary>
/// Time complexity: O(log(n)).
/// </summary>
public void Insert(T newValue)
{
if (Root == null)
{
Root = new BTreeNode<T>(maxKeysPerNode, null) { Keys = { [0] = newValue } };
Root.KeyCount++;
Count++;
return;
}
var leafToInsert = FindInsertionLeaf(Root, newValue);
InsertAndSplit(ref leafToInsert, newValue, null, null);
Count++;
}
/// <summary>
/// Find the leaf node to start initial insertion
/// </summary>
private BTreeNode<T> FindInsertionLeaf(BTreeNode<T> node, T newValue)
{
//if leaf then its time to insert
if (node.IsLeaf) return node;
//if not leaf then drill down to leaf
for (var i = 0; i < node.KeyCount; i++)
{
//current value is less than new value
//drill down to left child of current value
if (newValue.CompareTo(node.Keys[i]) < 0) return FindInsertionLeaf(node.Children[i], newValue);
//current value is grearer than new value
//and current value is last element
if (node.KeyCount == i + 1) return FindInsertionLeaf(node.Children[i + 1], newValue);
}
return node;
}
/// <summary>
/// Insert and split recursively up until no split is required
/// </summary>
private void InsertAndSplit(ref BTreeNode<T> node, T newValue,
BTreeNode<T> newValueLeft, BTreeNode<T> newValueRight)
{
//add new item to current node
if (node == null)
{
node = new BTreeNode<T>(maxKeysPerNode, null);
Root = node;
}
//newValue have room to fit in this node
//so just insert in right spot in asc order of keys
if (node.KeyCount != maxKeysPerNode)
{
InsertToNotFullNode(ref node, newValue, newValueLeft, newValueRight);
return;
}
//if node is full then split node
//and then insert new median to parent.
//divide the current node values + new Node as left and right sub nodes
var left = new BTreeNode<T>(maxKeysPerNode, null);
var right = new BTreeNode<T>(maxKeysPerNode, null);
//median of current Node
var currentMedianIndex = node.GetMedianIndex();
//init currentNode under consideration to left
var currentNode = left;
var currentNodeIndex = 0;
//new Median also takes new Value in to Account
var newMedian = default(T);
var newMedianSet = false;
var newValueInserted = false;
//keep track of each insertion
var insertionCount = 0;
//insert newValue and existing values in sorted order
//to left and right nodes
//set new median during sorting
for (var i = 0; i < node.KeyCount; i++)
{
//if insertion count reached new median
//set the new median by picking the next smallest value
if (!newMedianSet && insertionCount == currentMedianIndex)
{
newMedianSet = true;
//median can be the new value or node.keys[i] (next node key)
//whichever is smaller
if (!newValueInserted && newValue.CompareTo(node.Keys[i]) < 0)
{
//median is new value
newMedian = newValue;
newValueInserted = true;
if (newValueLeft != null) SetChild(currentNode, currentNode.KeyCount, newValueLeft);
//now fill right node
currentNode = right;
currentNodeIndex = 0;
if (newValueRight != null) SetChild(currentNode, 0, newValueRight);
i--;
insertionCount++;
continue;
}
//median is next node
newMedian = node.Keys[i];
//now fill right node
currentNode = right;
currentNodeIndex = 0;
continue;
}
//pick the smaller among newValue and node.Keys[i]
//and insert in to currentNode (left and right nodes)
//if new Value was already inserted then just copy from node.Keys in sequence
//since node.Keys is already in sorted order it should be fine
if (newValueInserted || node.Keys[i].CompareTo(newValue) < 0)
{
currentNode.Keys[currentNodeIndex] = node.Keys[i];
currentNode.KeyCount++;
//if child is set don't set again
//the child was already set by last newValueRight or last node
if (currentNode.Children[currentNodeIndex] == null)
SetChild(currentNode, currentNodeIndex, node.Children[i]);
SetChild(currentNode, currentNodeIndex + 1, node.Children[i + 1]);
}
else
{
currentNode.Keys[currentNodeIndex] = newValue;
currentNode.KeyCount++;
SetChild(currentNode, currentNodeIndex, newValueLeft);
SetChild(currentNode, currentNodeIndex + 1, newValueRight);
i--;
newValueInserted = true;
}
currentNodeIndex++;
insertionCount++;
}
//could be that thew newKey is the greatest
//so insert at end
if (!newValueInserted)
{
currentNode.Keys[currentNodeIndex] = newValue;
currentNode.KeyCount++;
SetChild(currentNode, currentNodeIndex, newValueLeft);
SetChild(currentNode, currentNodeIndex + 1, newValueRight);
}
//insert overflow element (newMedian) to parent
var parent = node.Parent;
InsertAndSplit(ref parent, newMedian, left, right);
}
/// <summary>
/// Insert to a node that is not full
/// </summary>
private void InsertToNotFullNode(ref BTreeNode<T> node, T newValue,
BTreeNode<T> newValueLeft, BTreeNode<T> newValueRight)
{
var inserted = false;
//insert in sorted order
for (var i = 0; i < node.KeyCount; i++)
{
if (newValue.CompareTo(node.Keys[i]) >= 0) continue;
InsertAt(node.Keys, i, newValue);
node.KeyCount++;
//Insert children if any
SetChild(node, i, newValueLeft);
InsertChild(node, i + 1, newValueRight);
inserted = true;
break;
}
//newValue is the greatest
//element should be inserted at the end then
if (inserted) return;
node.Keys[node.KeyCount] = newValue;
node.KeyCount++;
SetChild(node, node.KeyCount - 1, newValueLeft);
SetChild(node, node.KeyCount, newValueRight);
}
/// <summary>
/// Time complexity: O(log(n)).
/// </summary>
public void Delete(T value)
{
var node = FindDeletionNode(Root, value);
if (node == null) throw new Exception("Item do not exist in this tree.");
for (var i = 0; i < node.KeyCount; i++)
{
if (value.CompareTo(node.Keys[i]) != 0) continue;
//if node is leaf and no underflow
//then just remove the node
if (node.IsLeaf)
{
RemoveAt(node.Keys, i);
node.KeyCount--;
Balance(node);
}
else
{
//replace with max node of left tree
var maxNode = FindMaxNode(node.Children[i]);
node.Keys[i] = maxNode.Keys[maxNode.KeyCount - 1];
RemoveAt(maxNode.Keys, maxNode.KeyCount - 1);
maxNode.KeyCount--;
Balance(maxNode);
}
Count--;
return;
}
}
/// <summary>
/// return the node containing max value which will be a leaf at the right most
/// </summary>
private BTreeNode<T> FindMinNode(BTreeNode<T> node)
{
//if leaf return node
return node.IsLeaf ? node : FindMinNode(node.Children[0]);
}
/// <summary>
/// return the node containing max value which will be a leaf at the right most
/// </summary>
private BTreeNode<T> FindMaxNode(BTreeNode<T> node)
{
//if leaf return node
return node.IsLeaf ? node : FindMaxNode(node.Children[node.KeyCount]);
}
/// <summary>
/// Balance a node which is short of Keys by rotations or merge
/// </summary>
private void Balance(BTreeNode<T> node)
{
if (node == Root || node.KeyCount >= minKeysPerNode) return;
var rightSibling = GetRightSibling(node);
if (rightSibling != null
&& rightSibling.KeyCount > minKeysPerNode)
{
LeftRotate(node, rightSibling);
return;
}
var leftSibling = GetLeftSibling(node);
if (leftSibling != null
&& leftSibling.KeyCount > minKeysPerNode)
{
RightRotate(leftSibling, node);
return;
}
if (rightSibling != null)
Sandwich(node, rightSibling);
else
Sandwich(leftSibling, node);
}
/// <summary>
/// merge two adjacent siblings to one node
/// </summary>
private void Sandwich(BTreeNode<T> leftSibling, BTreeNode<T> rightSibling)
{
var separatorIndex = GetNextSeparatorIndex(leftSibling);
var parent = leftSibling.Parent;
var newNode = new BTreeNode<T>(maxKeysPerNode, leftSibling.Parent);
var newIndex = 0;
for (var i = 0; i < leftSibling.KeyCount; i++)
{
newNode.Keys[newIndex] = leftSibling.Keys[i];
if (leftSibling.Children[i] != null) SetChild(newNode, newIndex, leftSibling.Children[i]);
if (leftSibling.Children[i + 1] != null) SetChild(newNode, newIndex + 1, leftSibling.Children[i + 1]);
newIndex++;
}
//special case when left sibling is empty
if (leftSibling.KeyCount == 0 && leftSibling.Children[0] != null)
SetChild(newNode, newIndex, leftSibling.Children[0]);
newNode.Keys[newIndex] = parent.Keys[separatorIndex];
newIndex++;
for (var i = 0; i < rightSibling.KeyCount; i++)
{
newNode.Keys[newIndex] = rightSibling.Keys[i];
if (rightSibling.Children[i] != null) SetChild(newNode, newIndex, rightSibling.Children[i]);
if (rightSibling.Children[i + 1] != null) SetChild(newNode, newIndex + 1, rightSibling.Children[i + 1]);
newIndex++;
}
//special case when left sibling is empty
if (rightSibling.KeyCount == 0 && rightSibling.Children[0] != null)
SetChild(newNode, newIndex, rightSibling.Children[0]);
newNode.KeyCount = newIndex;
SetChild(parent, separatorIndex, newNode);
RemoveAt(parent.Keys, separatorIndex);
parent.KeyCount--;
RemoveChild(parent, separatorIndex + 1);
if (parent.KeyCount == 0
&& parent == Root)
{
Root = newNode;
Root.Parent = null;
if (Root.KeyCount == 0) Root = null;
return;
}
if (parent.KeyCount < minKeysPerNode) Balance(parent);
}
/// <summary>
/// do a right rotation
/// </summary>
private void RightRotate(BTreeNode<T> leftSibling, BTreeNode<T> rightSibling)
{
var parentIndex = GetNextSeparatorIndex(leftSibling);
InsertAt(rightSibling.Keys, 0, rightSibling.Parent.Keys[parentIndex]);
rightSibling.KeyCount++;
InsertChild(rightSibling, 0, leftSibling.Children[leftSibling.KeyCount]);
rightSibling.Parent.Keys[parentIndex] = leftSibling.Keys[leftSibling.KeyCount - 1];
RemoveAt(leftSibling.Keys, leftSibling.KeyCount - 1);
leftSibling.KeyCount--;
RemoveChild(leftSibling, leftSibling.KeyCount + 1);
}
/// <summary>
/// do a left rotation
/// </summary>
private void LeftRotate(BTreeNode<T> leftSibling, BTreeNode<T> rightSibling)
{
var parentIndex = GetNextSeparatorIndex(leftSibling);
leftSibling.Keys[leftSibling.KeyCount] = leftSibling.Parent.Keys[parentIndex];
leftSibling.KeyCount++;
SetChild(leftSibling, leftSibling.KeyCount, rightSibling.Children[0]);
leftSibling.Parent.Keys[parentIndex] = rightSibling.Keys[0];
RemoveAt(rightSibling.Keys, 0);
rightSibling.KeyCount--;
RemoveChild(rightSibling, 0);
}
/// <summary>
/// Locate the node in which the item to delete exist
/// </summary>
private BTreeNode<T> FindDeletionNode(BTreeNode<T> node, T value)
{
//if leaf then its time to insert
if (node.IsLeaf)
{
for (var i = 0; i < node.KeyCount; i++)
if (value.CompareTo(node.Keys[i]) == 0)
return node;
}
else
{
//if not leaf then drill down to leaf
for (var i = 0; i < node.KeyCount; i++)
{
if (value.CompareTo(node.Keys[i]) == 0) return node;
//current value is less than new value
//drill down to left child of current value
if (value.CompareTo(node.Keys[i]) < 0) return FindDeletionNode(node.Children[i], value);
//current value is grearer than new value
//and current value is last element
if (node.KeyCount == i + 1) return FindDeletionNode(node.Children[i + 1], value);
}
}
return null;
}
/// <summary>
/// Get next key separator index after this child Node in parent
/// </summary>
private int GetNextSeparatorIndex(BTreeNode<T> node)
{
var parent = node.Parent;
if (node.Index == 0) return 0;
if (node.Index == parent.KeyCount) return node.Index - 1;
return node.Index;
}
/// <summary>
/// get the right sibling node
/// </summary>
private BTreeNode<T> GetRightSibling(BTreeNode<T> node)
{
var parent = node.Parent;
return node.Index == parent.KeyCount ? null : parent.Children[node.Index + 1];
}
/// <summary>
/// get left sibling node
/// </summary>
private BTreeNode<T> GetLeftSibling(BTreeNode<T> node)
{
return node.Index == 0 ? null : node.Parent.Children[node.Index - 1];
}
private void SetChild(BTreeNode<T> parent, int childIndex, BTreeNode<T> child)
{
parent.Children[childIndex] = child;
if (child == null) return;
child.Parent = parent;
child.Index = childIndex;
}
private void InsertChild(BTreeNode<T> parent, int childIndex, BTreeNode<T> child)
{
InsertAt(parent.Children, childIndex, child);
if (child != null) child.Parent = parent;
//update indices
for (var i = childIndex; i <= parent.KeyCount; i++)
if (parent.Children[i] != null)
parent.Children[i].Index = i;
}
private void RemoveChild(BTreeNode<T> parent, int childIndex)
{
RemoveAt(parent.Children, childIndex);
//update indices
for (var i = childIndex; i <= parent.KeyCount; i++)
if (parent.Children[i] != null)
parent.Children[i].Index = i;
}
/// <summary>
/// Shift array right at index to make room for new insertion
/// And then insert at index
/// Assumes array have atleast one empty index at end
/// </summary>
private void InsertAt<TS>(TS[] array, int index, TS newValue)
{
//shift elements right by one indice from index
Array.Copy(array, index, array, index + 1, array.Length - index - 1);
//now set the value
array[index] = newValue;
}
/// <summary>
/// Shift array left at index
/// </summary>
private void RemoveAt<TS>(TS[] array, int index)
{
//shift elements right by one indice from index
Array.Copy(array, index + 1, array, index, array.Length - index - 1);
}
}
/// <summary>
/// abstract node shared by both B and B+ tree nodes
/// so that we can use this for common tests across B and B+ tree
/// </summary>
internal abstract class BNode<T> where T : IComparable
{
/// <summary>
/// Array Index of this node in parent's Children array
/// </summary>
internal int Index;
internal int KeyCount;
internal BNode(int maxKeysPerNode)
{
Keys = new T[maxKeysPerNode];
}
internal T[] Keys { get; set; }
//for common unit testing across B and B+ tree
internal abstract BNode<T> GetParent();
internal abstract BNode<T>[] GetChildren();
internal int GetMedianIndex()
{
return KeyCount / 2 + 1;
}
}
internal class BTreeNode<T> : BNode<T> where T : IComparable
{
internal BTreeNode(int maxKeysPerNode, BTreeNode<T> parent)
: base(maxKeysPerNode)
{
Parent = parent;
Children = new BTreeNode<T>[maxKeysPerNode + 1];
}
internal BTreeNode<T> Parent { get; set; }
internal BTreeNode<T>[] Children { get; set; }
internal bool IsLeaf => Children[0] == null;
/// <summary>
/// For shared test method accross B and B+ tree
/// </summary>
internal override BNode<T> GetParent()
{
return Parent;
}
/// <summary>
/// For shared test method accross B and B+ tree
/// </summary>
internal override BNode<T>[] GetChildren()
{
return Children;
}
}
internal class BTreeEnumerator<T> : IEnumerator<T> where T : IComparable
{
private readonly BTreeNode<T> root;
private BTreeNode<T> current;
private int index;
private Stack<BTreeNode<T>> progress;
internal BTreeEnumerator(BTreeNode<T> root)
{
this.root = root;
}
public bool MoveNext()
{
if (root == null) return false;
if (progress == null)
{
current = root;
progress = new Stack<BTreeNode<T>>(root.Children.Take(root.KeyCount + 1).Where(x => x != null));
return current.KeyCount > 0;
}
if (current != null && index + 1 < current.KeyCount)
{
index++;
return true;
}
if (progress.Count > 0)
{
index = 0;
current = progress.Pop();
foreach (var child in current.Children.Take(current.KeyCount + 1).Where(x => x != null))
progress.Push(child);
return true;
}
return false;
}
public void Reset()
{
progress = null;
current = null;
index = 0;
}
object IEnumerator.Current => Current;
public T Current => current.Keys[index];
public void Dispose()
{
progress = null;
}
}