-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeletion-from-a-b-tree.html
More file actions
2073 lines (1644 loc) · 101 KB
/
Copy pathdeletion-from-a-b-tree.html
File metadata and controls
2073 lines (1644 loc) · 101 KB
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>Delete from B Tree</title>
<!-- Google tag (gtag.js) -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=G-HCERBDV76D"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-HCERBDV76D');
</script>
<meta content="B0487B46A104E90209E8A3BEA24ECA0E" name="msvalidate.01"/>
<meta content="f044b3a12c7918f1" name="yandex-verification"/>
<!--end-->
<meta content="learn about data types, variables, lists, tuples, dictionaries,if else,DSA,loops,user-defined functions, oop, threading and scripting." name="description"/>
<meta content="learnPython" name="author"/>
<meta content="Learn Python for free,learn python for beginners,Core Python,Web frameworks,Multiprocess architecture,Serverside templating language,python tutorials,python4" name="keywords"/>
<meta content="website" property="og:type"/>
<meta content="US-CA" name="”geo.region”"/>
<meta content="353 Jane Stanford Way, Stanford, CA 94305, United States" name="”geo.placename”"/>
<meta content="37.430089898615456;-122.17332683124829" name="”geo.position”"/>
<meta content="37.430089898615456, -122.17332683124829" name="”ICBM”"/>
<link href="https://pythonread.github.io/?m=1" rel="alternate"/>
<link href="/favicon.png" rel="icon"/>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link href="/style1.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style2.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style0.css" media="all" rel="stylesheet" type="text/css"/>
<!--<![endif]-->
</head>
<body>
<div class="nav-wrapper">
<div class="container">
<nav>
<div class="logo wave">
<a href="/" id="logo">
Python Tutorial
</a>
</div>
<div class="nav-toggle-icon" id="nav-toggle-icon" onclick="mobileMenu()">
<div class="material-hamburger">
<span>
</span>
<span>
</span>
<span>
</span>
</div>
</div>
<div class="menu-wrapper" id="menu-wrapper">
<div class="nav-indicator">
</div>
<ul class="menus">
<li>
<a class="wave" href="/">
Home
</a>
</li>
<li>
<a class="wave" href="/projects.html">
Projects
</a>
</li>
<li>
<a class="wave" href="/free-course.html" target="_blank">
Free Course
</a>
</li>
<li>
<a class="wave" href="/dsa.html">
DSA
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="contents contents--neg" style="margin-top: 60px">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="d-flex">
<div class="left-bar d-none d-lg-block">
<div class="card-alt mb-10x">
<h3>Page Index</h3>
<div class="list">
<ul>
<li><a href="/dsa.html#data-structure-1" title="Data Structures (I)">Data Structures
(I)</a></li>
<li><a href="/dsa.html#data-structure-2" title="Data Structures (II)">Data Structures
(II)</a></li>
<li><a href="/dsa.html#tree-1" title="Tree based DSA (I)">Tree based DSA (I)</a></li>
<li><a href="/dsa.html#tree-2" title="Tree based DSA (II)">Tree based DSA (II)</a></li>
<li><a href="/dsa.html#graph" title="Graph Data Structures and Algorithm">Graph based
DSA</a></li>
<li><a href="/dsa.html#sorting-searching" title="Sorting and Searching">Sorting and
Searching</a></li>
<li><a href="/dsa.html#greedy-algorithm" title="Greedy Algorithms">Greedy Algorithms</a>
</li>
<li><a href="/dsa.html#dynamic-programming" title="Dynamic Programming">Dynamic
Programming</a></li>
<li><a href="/dsa.html#other-algorithms" title="Other Algorithms">Other Algorithms</a>
</li>
</ul>
</div>
</div>
</div>
<div class="right-bar">
<!--first part end-------------------------------------------->
<iframe width="560" height="315" src="https://www.youtube.com/embed/tKZmepQxWuQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<div class="editor-contents">
<h1>Deletion from a B-tree</h1>
<p class="editor-contents__short-description">In this tutorial, you will learn how to delete a key from a b-tree. Also, you will find working examples of deleting keys from a B-tree in C, C++, Java and Python.</p>
<div id="node-1653" class="node node-algorithm clearfix" about="/dsa/deletion-from-a-b-tree" typeof="sioc:Item foaf:Document">
<span property="dc:title" content="Deletion from a B-tree" class="rdf-meta element-hidden"></span>
<div class="content">
<p id="definition">Deleting an element on a B-tree consists of three main events: <strong>searching the node where the key to be deleted exists</strong>, deleting the key and balancing the tree if required.</p>
<p>While deleting a tree, a condition called <strong>underflow</strong> may occur. Underflow occurs when a node contains less than the minimum number of keys it should hold.</p>
<p>The terms to be understood before studying deletion operation are:</p>
<ol><li><strong>Inorder Predecessor</strong><br>
The largest key on the left child of a node is called its inorder predecessor.</li>
<li><strong>Inorder Successor</strong><br>
The smallest key on the right child of a node is called its inorder successor.</li>
</ol><hr><h2 id="deletion">Deletion Operation</h2>
<p>Before going through the steps below, one must know these facts about a B tree of degree <strong>m</strong>.</p>
<ol><li>A node can have a maximum of m children. (i.e. 3)</li>
<li>A node can contain a maximum of <code>m - 1</code> keys. (i.e. 2)</li>
<li>A node should have a minimum of <code>⌈m/2⌉</code> children. (i.e. 2)</li>
<li>A node (except root node) should contain a minimum of <code>⌈m/2⌉ - 1</code> keys. (i.e. 1)</li>
</ol><p>There are three main cases for deletion operation in a B tree.</p>
<h3>Case I</h3>
<p>The key to be deleted lies in the leaf. There are two cases for it.</p>
<ol><li>The deletion of the key does not violate the property of the minimum number of keys a node should hold.<br><br>
In the tree below, deleting 32 does not violate the above properties.
<figure><img alt="Delete a key from a B-tree" src="//cdn.programiz.com/sites/tutorial2program/files/delete-leaf-1.png" title="Delete 32 from B-tree" width="588" height="465"><figcaption>Deleting a leaf key (32) from B-tree</figcaption></figure></li>
<li>The deletion of the key violates the property of the minimum number of keys a node should hold. In this case, we borrow a key from its immediate neighboring sibling node in the order of left to right.<br><br>
First, visit the immediate left sibling. If the left sibling node has more than a minimum number of keys, then borrow a key from this node.<br><br>
Else, check to borrow from the immediate right sibling node.<br><br>
In the tree below, deleting 31 results in the above condition. Let us borrow a key from the left sibling node.
<figure><img alt="Delete a key from a B-tree" src="//cdn.programiz.com/sites/tutorial2program/files/delete-leaf-2.png" title="Delete 31 from a B-tree" width="588" height="714"><figcaption>Deleting a leaf key (31)</figcaption></figure>
If both the immediate sibling nodes already have a minimum number of keys, then merge the node with either the left sibling node or the right sibling node. <strong>This merging is done through the parent node.</strong><br><br>
Deleting 30 results in the above case.<br>
<figure><img alt="Delete a key from a B-tree" src="//cdn.programiz.com/sites/tutorial2program/files/delete-leaf-3.png" title="Delete 30 from a B-tree" width="588" height="714"><figcaption>Delete a leaf key (30)</figcaption></figure></li>
</ol><h3>Case II</h3>
<p>If the key to be deleted lies in the internal node, the following cases occur.</p>
<ol><li>The internal node, which is deleted, is replaced by an inorder predecessor if the left child has more than the minimum number of keys.
<figure><img alt="Deleting an internal node" src="//cdn.programiz.com/sites/tutorial2program/files/delete-internal-1.png" title="Deleting an internal node" width="588" height="714"><figcaption>Deleting an internal node (33)</figcaption></figure></li>
<li>The internal node, which is deleted, is replaced by an inorder successor if the right child has more than the minimum number of keys.</li>
<li>If either child has exactly a minimum number of keys then, merge the left and the right children.<br>
<figure><img alt="Deleting an internal node" src="//cdn.programiz.com/sites/tutorial2program/files/delete-internal-2.png" title="Deleting an internal node" width="588" height="714"><figcaption>Deleting an internal node (30)</figcaption></figure>
After merging if the parent node has less than the minimum number of keys then, look for the siblings as in Case I.</li>
</ol><h3>Case III</h3>
<p></p><div class="clearfix"></div><p>In this case, the height of the tree shrinks. If the target key lies in an internal node, and the deletion of the key leads to a fewer number of keys in the node (i.e. less than the minimum required), then look for the inorder predecessor and the inorder successor. If both the children contain a minimum number of keys then, borrowing cannot take place. This leads to Case II(3) i.e. merging the children.</p>
<p>Again, look for the sibling to borrow a key. But, if the sibling also has only a minimum number of keys then, merge the node with the sibling along with the parent. Arrange the children accordingly (increasing order).</p>
<figure><img alt="Deleting an internal node" src="//cdn.programiz.com/sites/tutorial2program/files/delete-internal_3.png" title="Deleting an internal node" width="584" height="465"><figcaption>Deleting an internal node (10)</figcaption></figure><hr><h2 id="code">Python, Java and C/C++ Examples</h2>
<div class="tabbed-editor">
<div id="py1" onclick="changepy()" class="tabbed-editor__node tabbed-editor__node--active"><a href="#python-code">Python</a></div>
<div id="java1" onclick="changejava()" class="tabbed-editor__node"><a href="#java-code">Java</a></div>
<div id="c1" onclick="changec()" class="tabbed-editor__node"><a href="#c-code">C</a></div>
<div id="cpp1" onclick="changecpp()" class="tabbed-editor__node"><a href="#cpp-code">C++</a></div>
</div>
<div class="code-editor code-editor--tabbed">
<div class="code-editor__area code-editor__area--active" id="python-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="python hljs"><span class="hljs-comment"># Deleting a key on a B-tree in Python</span>
<span class="hljs-comment"># Btree node</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BTreeNode</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, leaf=False)</span>:</span>
self.leaf = leaf
self.keys = []
self.child = []
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BTree</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, t)</span>:</span>
self.root = BTreeNode(<span class="hljs-literal">True</span>)
self.t = t
<span class="hljs-comment"># Insert a key</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert</span><span class="hljs-params">(self, k)</span>:</span>
root = self.root
<span class="hljs-keyword">if</span> len(root.keys) == (<span class="hljs-number">2</span> * self.t) - <span class="hljs-number">1</span>:
temp = BTreeNode()
self.root = temp
temp.child.insert(<span class="hljs-number">0</span>, root)
self.split_child(temp, <span class="hljs-number">0</span>)
self.insert_non_full(temp, k)
<span class="hljs-keyword">else</span>:
self.insert_non_full(root, k)
<span class="hljs-comment"># Insert non full</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert_non_full</span><span class="hljs-params">(self, x, k)</span>:</span>
i = len(x.keys) - <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> x.leaf:
x.keys.append((<span class="hljs-literal">None</span>, <span class="hljs-literal">None</span>))
<span class="hljs-keyword">while</span> i >= <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> k[<span class="hljs-number">0</span>] < x.keys[i][<span class="hljs-number">0</span>]:
x.keys[i + <span class="hljs-number">1</span>] = x.keys[i]
i -= <span class="hljs-number">1</span>
x.keys[i + <span class="hljs-number">1</span>] = k
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">while</span> i >= <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> k[<span class="hljs-number">0</span>] < x.keys[i][<span class="hljs-number">0</span>]:
i -= <span class="hljs-number">1</span>
i += <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> len(x.child[i].keys) == (<span class="hljs-number">2</span> * self.t) - <span class="hljs-number">1</span>:
self.split_child(x, i)
<span class="hljs-keyword">if</span> k[<span class="hljs-number">0</span>] > x.keys[i][<span class="hljs-number">0</span>]:
i += <span class="hljs-number">1</span>
self.insert_non_full(x.child[i], k)
<span class="hljs-comment"># Split the child</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">split_child</span><span class="hljs-params">(self, x, i)</span>:</span>
t = self.t
y = x.child[i]
z = BTreeNode(y.leaf)
x.child.insert(i + <span class="hljs-number">1</span>, z)
x.keys.insert(i, y.keys[t - <span class="hljs-number">1</span>])
z.keys = y.keys[t: (<span class="hljs-number">2</span> * t) - <span class="hljs-number">1</span>]
y.keys = y.keys[<span class="hljs-number">0</span>: t - <span class="hljs-number">1</span>]
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> y.leaf:
z.child = y.child[t: <span class="hljs-number">2</span> * t]
y.child = y.child[<span class="hljs-number">0</span>: t - <span class="hljs-number">1</span>]
<span class="hljs-comment"># Delete a node</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete</span><span class="hljs-params">(self, x, k)</span>:</span>
t = self.t
i = <span class="hljs-number">0</span>
<span class="hljs-keyword">while</span> i < len(x.keys) <span class="hljs-keyword">and</span> k[<span class="hljs-number">0</span>] > x.keys[i][<span class="hljs-number">0</span>]:
i += <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> x.leaf:
<span class="hljs-keyword">if</span> i < len(x.keys) <span class="hljs-keyword">and</span> x.keys[i][<span class="hljs-number">0</span>] == k[<span class="hljs-number">0</span>]:
x.keys.pop(i)
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">if</span> i < len(x.keys) <span class="hljs-keyword">and</span> x.keys[i][<span class="hljs-number">0</span>] == k[<span class="hljs-number">0</span>]:
<span class="hljs-keyword">return</span> self.delete_internal_node(x, k, i)
<span class="hljs-keyword">elif</span> len(x.child[i].keys) >= t:
self.delete(x.child[i], k)
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">if</span> i != <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> i + <span class="hljs-number">2</span> < len(x.child):
<span class="hljs-keyword">if</span> len(x.child[i - <span class="hljs-number">1</span>].keys) >= t:
self.delete_sibling(x, i, i - <span class="hljs-number">1</span>)
<span class="hljs-keyword">elif</span> len(x.child[i + <span class="hljs-number">1</span>].keys) >= t:
self.delete_sibling(x, i, i + <span class="hljs-number">1</span>)
<span class="hljs-keyword">else</span>:
self.delete_merge(x, i, i + <span class="hljs-number">1</span>)
<span class="hljs-keyword">elif</span> i == <span class="hljs-number">0</span>:
<span class="hljs-keyword">if</span> len(x.child[i + <span class="hljs-number">1</span>].keys) >= t:
self.delete_sibling(x, i, i + <span class="hljs-number">1</span>)
<span class="hljs-keyword">else</span>:
self.delete_merge(x, i, i + <span class="hljs-number">1</span>)
<span class="hljs-keyword">elif</span> i + <span class="hljs-number">1</span> == len(x.child):
<span class="hljs-keyword">if</span> len(x.child[i - <span class="hljs-number">1</span>].keys) >= t:
self.delete_sibling(x, i, i - <span class="hljs-number">1</span>)
<span class="hljs-keyword">else</span>:
self.delete_merge(x, i, i - <span class="hljs-number">1</span>)
self.delete(x.child[i], k)
<span class="hljs-comment"># Delete internal node</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_internal_node</span><span class="hljs-params">(self, x, k, i)</span>:</span>
t = self.t
<span class="hljs-keyword">if</span> x.leaf:
<span class="hljs-keyword">if</span> x.keys[i][<span class="hljs-number">0</span>] == k[<span class="hljs-number">0</span>]:
x.keys.pop(i)
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">if</span> len(x.child[i].keys) >= t:
x.keys[i] = self.delete_predecessor(x.child[i])
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">elif</span> len(x.child[i + <span class="hljs-number">1</span>].keys) >= t:
x.keys[i] = self.delete_successor(x.child[i + <span class="hljs-number">1</span>])
<span class="hljs-keyword">return</span>
<span class="hljs-keyword">else</span>:
self.delete_merge(x, i, i + <span class="hljs-number">1</span>)
self.delete_internal_node(x.child[i], k, self.t - <span class="hljs-number">1</span>)
<span class="hljs-comment"># Delete the predecessor</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_predecessor</span><span class="hljs-params">(self, x)</span>:</span>
<span class="hljs-keyword">if</span> x.leaf:
<span class="hljs-keyword">return</span> x.pop()
n = len(x.keys) - <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> len(x.child[n].keys) >= self.t:
self.delete_sibling(x, n + <span class="hljs-number">1</span>, n)
<span class="hljs-keyword">else</span>:
self.delete_merge(x, n, n + <span class="hljs-number">1</span>)
self.delete_predecessor(x.child[n])
<span class="hljs-comment"># Delete the successor</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_successor</span><span class="hljs-params">(self, x)</span>:</span>
<span class="hljs-keyword">if</span> x.leaf:
<span class="hljs-keyword">return</span> x.keys.pop(<span class="hljs-number">0</span>)
<span class="hljs-keyword">if</span> len(x.child[<span class="hljs-number">1</span>].keys) >= self.t:
self.delete_sibling(x, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>)
<span class="hljs-keyword">else</span>:
self.delete_merge(x, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>)
self.delete_successor(x.child[<span class="hljs-number">0</span>])
<span class="hljs-comment"># Delete resolution</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_merge</span><span class="hljs-params">(self, x, i, j)</span>:</span>
cnode = x.child[i]
<span class="hljs-keyword">if</span> j > i:
rsnode = x.child[j]
cnode.keys.append(x.keys[i])
<span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> range(len(rsnode.keys)):
cnode.keys.append(rsnode.keys[k])
<span class="hljs-keyword">if</span> len(rsnode.child) > <span class="hljs-number">0</span>:
cnode.child.append(rsnode.child[k])
<span class="hljs-keyword">if</span> len(rsnode.child) > <span class="hljs-number">0</span>:
cnode.child.append(rsnode.child.pop())
new = cnode
x.keys.pop(i)
x.child.pop(j)
<span class="hljs-keyword">else</span>:
lsnode = x.child[j]
lsnode.keys.append(x.keys[j])
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(len(cnode.keys)):
lsnode.keys.append(cnode.keys[i])
<span class="hljs-keyword">if</span> len(lsnode.child) > <span class="hljs-number">0</span>:
lsnode.child.append(cnode.child[i])
<span class="hljs-keyword">if</span> len(lsnode.child) > <span class="hljs-number">0</span>:
lsnode.child.append(cnode.child.pop())
new = lsnode
x.keys.pop(j)
x.child.pop(i)
<span class="hljs-keyword">if</span> x == self.root <span class="hljs-keyword">and</span> len(x.keys) == <span class="hljs-number">0</span>:
self.root = new
<span class="hljs-comment"># Delete the sibling</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete_sibling</span><span class="hljs-params">(self, x, i, j)</span>:</span>
cnode = x.child[i]
<span class="hljs-keyword">if</span> i < j:
rsnode = x.child[j]
cnode.keys.append(x.keys[i])
x.keys[i] = rsnode.keys[<span class="hljs-number">0</span>]
<span class="hljs-keyword">if</span> len(rsnode.child) > <span class="hljs-number">0</span>:
cnode.child.append(rsnode.child[<span class="hljs-number">0</span>])
rsnode.child.pop(<span class="hljs-number">0</span>)
rsnode.keys.pop(<span class="hljs-number">0</span>)
<span class="hljs-keyword">else</span>:
lsnode = x.child[j]
cnode.keys.insert(<span class="hljs-number">0</span>, x.keys[i - <span class="hljs-number">1</span>])
x.keys[i - <span class="hljs-number">1</span>] = lsnode.keys.pop()
<span class="hljs-keyword">if</span> len(lsnode.child) > <span class="hljs-number">0</span>:
cnode.child.insert(<span class="hljs-number">0</span>, lsnode.child.pop())
<span class="hljs-comment"># Print the tree</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">print_tree</span><span class="hljs-params">(self, x, l=<span class="hljs-number">0</span>)</span>:</span>
<span class="hljs-keyword">print</span>(<span class="hljs-string">"Level "</span>, l, <span class="hljs-string">" "</span>, len(x.keys), end=<span class="hljs-string">":"</span>)
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> x.keys:
<span class="hljs-keyword">print</span>(i, end=<span class="hljs-string">" "</span>)
<span class="hljs-keyword">print</span>()
l += <span class="hljs-number">1</span>
<span class="hljs-keyword">if</span> len(x.child) > <span class="hljs-number">0</span>:
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> x.child:
self.print_tree(i, l)
B = BTree(<span class="hljs-number">3</span>)
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>):
B.insert((i, <span class="hljs-number">2</span> * i))
B.print_tree(B.root)
B.delete(B.root, (<span class="hljs-number">8</span>,))
<span class="hljs-keyword">print</span>(<span class="hljs-string">"\n"</span>)
B.print_tree(B.root)</code></pre></div>
</div>
<div class="code-editor__area" id="java-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="java hljs"><span class="hljs-comment">// Inserting a key on a B-tree in Java</span>
<span class="hljs-keyword">import</span> java.util.Stack;
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BTree</span> </span>{
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> T;
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span> </span>{
<span class="hljs-keyword">int</span> n;
<span class="hljs-keyword">int</span> key[] = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">2</span> * T - <span class="hljs-number">1</span>];
Node child[] = <span class="hljs-keyword">new</span> Node[<span class="hljs-number">2</span> * T];
<span class="hljs-keyword">boolean</span> leaf = <span class="hljs-keyword">true</span>;
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">Find</span><span class="hljs-params">(<span class="hljs-keyword">int</span> k)</span> </span>{
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-keyword">this</span>.n; i++) {
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.key[i] == k) {
<span class="hljs-keyword">return</span> i;
}
}
<span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
};
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">BTree</span><span class="hljs-params">(<span class="hljs-keyword">int</span> t)</span> </span>{
T = t;
root = <span class="hljs-keyword">new</span> Node();
root.n = <span class="hljs-number">0</span>;
root.leaf = <span class="hljs-keyword">true</span>;
}
<span class="hljs-keyword">private</span> Node root;
<span class="hljs-comment">// Search the key</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> Node <span class="hljs-title">Search</span><span class="hljs-params">(Node x, <span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">if</span> (x == <span class="hljs-keyword">null</span>)
<span class="hljs-keyword">return</span> x;
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < x.n; i++) {
<span class="hljs-keyword">if</span> (key < x.key[i]) {
<span class="hljs-keyword">break</span>;
}
<span class="hljs-keyword">if</span> (key == x.key[i]) {
<span class="hljs-keyword">return</span> x;
}
}
<span class="hljs-keyword">if</span> (x.leaf) {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">return</span> Search(x.child[i], key);
}
}
<span class="hljs-comment">// Split function</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Split</span><span class="hljs-params">(Node x, <span class="hljs-keyword">int</span> pos, Node y)</span> </span>{
Node z = <span class="hljs-keyword">new</span> Node();
z.leaf = y.leaf;
z.n = T - <span class="hljs-number">1</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < T - <span class="hljs-number">1</span>; j++) {
z.key[j] = y.key[j + T];
}
<span class="hljs-keyword">if</span> (!y.leaf) {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < T; j++) {
z.child[j] = y.child[j + T];
}
}
y.n = T - <span class="hljs-number">1</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = x.n; j >= pos + <span class="hljs-number">1</span>; j--) {
x.child[j + <span class="hljs-number">1</span>] = x.child[j];
}
x.child[pos + <span class="hljs-number">1</span>] = z;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = x.n - <span class="hljs-number">1</span>; j >= pos; j--) {
x.key[j + <span class="hljs-number">1</span>] = x.key[j];
}
x.key[pos] = y.key[T - <span class="hljs-number">1</span>];
x.n = x.n + <span class="hljs-number">1</span>;
}
<span class="hljs-comment">// Insert the key</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Insert</span><span class="hljs-params">(<span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> key)</span> </span>{
Node r = root;
<span class="hljs-keyword">if</span> (r.n == <span class="hljs-number">2</span> * T - <span class="hljs-number">1</span>) {
Node s = <span class="hljs-keyword">new</span> Node();
root = s;
s.leaf = <span class="hljs-keyword">false</span>;
s.n = <span class="hljs-number">0</span>;
s.child[<span class="hljs-number">0</span>] = r;
Split(s, <span class="hljs-number">0</span>, r);
_Insert(s, key);
} <span class="hljs-keyword">else</span> {
_Insert(r, key);
}
}
<span class="hljs-comment">// Insert the node</span>
<span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">_Insert</span><span class="hljs-params">(Node x, <span class="hljs-keyword">int</span> k)</span> </span>{
<span class="hljs-keyword">if</span> (x.leaf) {
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">for</span> (i = x.n - <span class="hljs-number">1</span>; i >= <span class="hljs-number">0</span> && k < x.key[i]; i--) {
x.key[i + <span class="hljs-number">1</span>] = x.key[i];
}
x.key[i + <span class="hljs-number">1</span>] = k;
x.n = x.n + <span class="hljs-number">1</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">for</span> (i = x.n - <span class="hljs-number">1</span>; i >= <span class="hljs-number">0</span> && k < x.key[i]; i--) {
}
;
i++;
Node tmp = x.child[i];
<span class="hljs-keyword">if</span> (tmp.n == <span class="hljs-number">2</span> * T - <span class="hljs-number">1</span>) {
Split(x, i, tmp);
<span class="hljs-keyword">if</span> (k > x.key[i]) {
i++;
}
}
_Insert(x.child[i], k);
}
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Show</span><span class="hljs-params">()</span> </span>{
Show(root);
}
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Remove</span><span class="hljs-params">(Node x, <span class="hljs-keyword">int</span> key)</span> </span>{
<span class="hljs-keyword">int</span> pos = x.Find(key);
<span class="hljs-keyword">if</span> (pos != -<span class="hljs-number">1</span>) {
<span class="hljs-keyword">if</span> (x.leaf) {
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < x.n && x.key[i] != key; i++) {
}
;
<span class="hljs-keyword">for</span> (; i < x.n; i++) {
<span class="hljs-keyword">if</span> (i != <span class="hljs-number">2</span> * T - <span class="hljs-number">2</span>) {
x.key[i] = x.key[i + <span class="hljs-number">1</span>];
}
}
x.n--;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (!x.leaf) {
Node pred = x.child[pos];
<span class="hljs-keyword">int</span> predKey = <span class="hljs-number">0</span>;
<span class="hljs-keyword">if</span> (pred.n >= T) {
<span class="hljs-keyword">for</span> (;;) {
<span class="hljs-keyword">if</span> (pred.leaf) {
System.out.println(pred.n);
predKey = pred.key[pred.n - <span class="hljs-number">1</span>];
<span class="hljs-keyword">break</span>;
} <span class="hljs-keyword">else</span> {
pred = pred.child[pred.n];
}
}
Remove(pred, predKey);
x.key[pos] = predKey;
<span class="hljs-keyword">return</span>;
}
Node nextNode = x.child[pos + <span class="hljs-number">1</span>];
<span class="hljs-keyword">if</span> (nextNode.n >= T) {
<span class="hljs-keyword">int</span> nextKey = nextNode.key[<span class="hljs-number">0</span>];
<span class="hljs-keyword">if</span> (!nextNode.leaf) {
nextNode = nextNode.child[<span class="hljs-number">0</span>];
<span class="hljs-keyword">for</span> (;;) {
<span class="hljs-keyword">if</span> (nextNode.leaf) {
nextKey = nextNode.key[nextNode.n - <span class="hljs-number">1</span>];
<span class="hljs-keyword">break</span>;
} <span class="hljs-keyword">else</span> {
nextNode = nextNode.child[nextNode.n];
}
}
}
Remove(nextNode, nextKey);
x.key[pos] = nextKey;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">int</span> temp = pred.n + <span class="hljs-number">1</span>;
pred.key[pred.n++] = x.key[pos];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>, j = pred.n; i < nextNode.n; i++) {
pred.key[j++] = nextNode.key[i];
pred.n++;
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < nextNode.n + <span class="hljs-number">1</span>; i++) {
pred.child[temp++] = nextNode.child[i];
}
x.child[pos] = pred;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = pos; i < x.n; i++) {
<span class="hljs-keyword">if</span> (i != <span class="hljs-number">2</span> * T - <span class="hljs-number">2</span>) {
x.key[i] = x.key[i + <span class="hljs-number">1</span>];
}
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = pos + <span class="hljs-number">1</span>; i < x.n + <span class="hljs-number">1</span>; i++) {
<span class="hljs-keyword">if</span> (i != <span class="hljs-number">2</span> * T - <span class="hljs-number">1</span>) {
x.child[i] = x.child[i + <span class="hljs-number">1</span>];
}
}
x.n--;
<span class="hljs-keyword">if</span> (x.n == <span class="hljs-number">0</span>) {
<span class="hljs-keyword">if</span> (x == root) {
root = x.child[<span class="hljs-number">0</span>];
}
x = x.child[<span class="hljs-number">0</span>];
}
Remove(pred, key);
<span class="hljs-keyword">return</span>;
}
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">for</span> (pos = <span class="hljs-number">0</span>; pos < x.n; pos++) {
<span class="hljs-keyword">if</span> (x.key[pos] > key) {
<span class="hljs-keyword">break</span>;
}
}
Node tmp = x.child[pos];
<span class="hljs-keyword">if</span> (tmp.n >= T) {
Remove(tmp, key);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">true</span>) {
Node nb = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">int</span> devider = -<span class="hljs-number">1</span>;
<span class="hljs-keyword">if</span> (pos != x.n && x.child[pos + <span class="hljs-number">1</span>].n >= T) {
devider = x.key[pos];
nb = x.child[pos + <span class="hljs-number">1</span>];
x.key[pos] = nb.key[<span class="hljs-number">0</span>];
tmp.key[tmp.n++] = devider;
tmp.child[tmp.n] = nb.child[<span class="hljs-number">0</span>];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i < nb.n; i++) {
nb.key[i - <span class="hljs-number">1</span>] = nb.key[i];
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i <= nb.n; i++) {
nb.child[i - <span class="hljs-number">1</span>] = nb.child[i];
}
nb.n--;
Remove(tmp, key);
<span class="hljs-keyword">return</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (pos != <span class="hljs-number">0</span> && x.child[pos - <span class="hljs-number">1</span>].n >= T) {
devider = x.key[pos - <span class="hljs-number">1</span>];
nb = x.child[pos - <span class="hljs-number">1</span>];
x.key[pos - <span class="hljs-number">1</span>] = nb.key[nb.n - <span class="hljs-number">1</span>];
Node child = nb.child[nb.n];
nb.n--;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = tmp.n; i > <span class="hljs-number">0</span>; i--) {
tmp.key[i] = tmp.key[i - <span class="hljs-number">1</span>];
}
tmp.key[<span class="hljs-number">0</span>] = devider;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = tmp.n + <span class="hljs-number">1</span>; i > <span class="hljs-number">0</span>; i--) {
tmp.child[i] = tmp.child[i - <span class="hljs-number">1</span>];
}
tmp.child[<span class="hljs-number">0</span>] = child;
tmp.n++;
Remove(tmp, key);
<span class="hljs-keyword">return</span>;
} <span class="hljs-keyword">else</span> {
Node lt = <span class="hljs-keyword">null</span>;
Node rt = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">boolean</span> last = <span class="hljs-keyword">false</span>;
<span class="hljs-keyword">if</span> (pos != x.n) {
devider = x.key[pos];
lt = x.child[pos];
rt = x.child[pos + <span class="hljs-number">1</span>];
} <span class="hljs-keyword">else</span> {
devider = x.key[pos - <span class="hljs-number">1</span>];
rt = x.child[pos];
lt = x.child[pos - <span class="hljs-number">1</span>];
last = <span class="hljs-keyword">true</span>;
pos--;
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = pos; i < x.n - <span class="hljs-number">1</span>; i++) {
x.key[i] = x.key[i + <span class="hljs-number">1</span>];
}
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = pos + <span class="hljs-number">1</span>; i < x.n; i++) {
x.child[i] = x.child[i + <span class="hljs-number">1</span>];
}
x.n--;
lt.key[lt.n++] = devider;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>, j = lt.n; i < rt.n + <span class="hljs-number">1</span>; i++, j++) {
<span class="hljs-keyword">if</span> (i < rt.n) {
lt.key[j] = rt.key[i];
}
lt.child[j] = rt.child[i];
}
lt.n += rt.n;
<span class="hljs-keyword">if</span> (x.n == <span class="hljs-number">0</span>) {
<span class="hljs-keyword">if</span> (x == root) {
root = x.child[<span class="hljs-number">0</span>];
}
x = x.child[<span class="hljs-number">0</span>];
}
Remove(lt, key);
<span class="hljs-keyword">return</span>;
}
}
}
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Remove</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key)</span> </span>{
Node x = Search(root, key);
<span class="hljs-keyword">if</span> (x == <span class="hljs-keyword">null</span>) {
<span class="hljs-keyword">return</span>;
}
Remove(root, key);
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Task</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b)</span> </span>{
Stack<Integer> st = <span class="hljs-keyword">new</span> Stack<>();
FindKeys(a, b, root, st);
<span class="hljs-keyword">while</span> (st.isEmpty() == <span class="hljs-keyword">false</span>) {
<span class="hljs-keyword">this</span>.Remove(root, st.pop());
}
}
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">FindKeys</span><span class="hljs-params">(<span class="hljs-keyword">int</span> a, <span class="hljs-keyword">int</span> b, Node x, Stack<Integer> st)</span> </span>{
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < x.n && x.key[i] < b; i++) {
<span class="hljs-keyword">if</span> (x.key[i] > a) {
st.push(x.key[i]);
}
}
<span class="hljs-keyword">if</span> (!x.leaf) {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < i + <span class="hljs-number">1</span>; j++) {
FindKeys(a, b, x.child[j], st);
}
}
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">Contain</span><span class="hljs-params">(<span class="hljs-keyword">int</span> k)</span> </span>{
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.Search(root, k) != <span class="hljs-keyword">null</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
}
}
<span class="hljs-comment">// Show the node</span>
<span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Show</span><span class="hljs-params">(Node x)</span> </span>{
<span class="hljs-keyword">assert</span> (x == <span class="hljs-keyword">null</span>);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < x.n; i++) {
System.out.print(x.key[i] + <span class="hljs-string">" "</span>);
}
<span class="hljs-keyword">if</span> (!x.leaf) {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < x.n + <span class="hljs-number">1</span>; i++) {
Show(x.child[i]);
}
}
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
BTree b = <span class="hljs-keyword">new</span> BTree(<span class="hljs-number">3</span>);
b.Insert(<span class="hljs-number">8</span>);
b.Insert(<span class="hljs-number">9</span>);
b.Insert(<span class="hljs-number">10</span>);
b.Insert(<span class="hljs-number">11</span>);
b.Insert(<span class="hljs-number">15</span>);
b.Insert(<span class="hljs-number">20</span>);
b.Insert(<span class="hljs-number">17</span>);
b.Show();
b.Remove(<span class="hljs-number">10</span>);
System.out.println();
b.Show();
}
}</code></pre></div>
</div>
<div class="code-editor__area" id="c-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="c hljs cpp"><span class="hljs-comment">// Deleting a key from a B-tree in C</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdlib.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MAX 3</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MIN 2</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BTreeNode</span> {</span>
<span class="hljs-keyword">int</span> item[MAX + <span class="hljs-number">1</span>], count;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BTreeNode</span> *<span class="hljs-title">linker</span>[<span class="hljs-title">MAX</span> + 1];</span>
};
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BTreeNode</span> *<span class="hljs-title">root</span>;</span>
<span class="hljs-comment">// Node creation</span>
<span class="hljs-function">struct BTreeNode *<span class="hljs-title">createNode</span><span class="hljs-params">(<span class="hljs-keyword">int</span> item, struct BTreeNode *child)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BTreeNode</span> *<span class="hljs-title">newNode</span>;</span>
newNode = (struct BTreeNode *)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(struct BTreeNode));
newNode->item[<span class="hljs-number">1</span>] = item;
newNode->count = <span class="hljs-number">1</span>;
newNode->linker[<span class="hljs-number">0</span>] = root;
newNode->linker[<span class="hljs-number">1</span>] = child;
<span class="hljs-keyword">return</span> newNode;
}
<span class="hljs-comment">// Add value to the node</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addValToNode</span><span class="hljs-params">(<span class="hljs-keyword">int</span> item, <span class="hljs-keyword">int</span> pos, struct BTreeNode *node,
struct BTreeNode *child)</span> </span>{
<span class="hljs-keyword">int</span> j = node->count;
<span class="hljs-keyword">while</span> (j > pos) {
node->item[j + <span class="hljs-number">1</span>] = node->item[j];
node->linker[j + <span class="hljs-number">1</span>] = node->linker[j];
j--;
}
node->item[j + <span class="hljs-number">1</span>] = item;
node->linker[j + <span class="hljs-number">1</span>] = child;
node->count++;
}
<span class="hljs-comment">// Split the node</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">splitNode</span><span class="hljs-params">(<span class="hljs-keyword">int</span> item, <span class="hljs-keyword">int</span> *pval, <span class="hljs-keyword">int</span> pos, struct BTreeNode *node,
struct BTreeNode *child, struct BTreeNode **newNode)</span> </span>{
<span class="hljs-keyword">int</span> median, j;
<span class="hljs-keyword">if</span> (pos > MIN)
median = MIN + <span class="hljs-number">1</span>;
<span class="hljs-keyword">else</span>
median = MIN;
*newNode = (struct BTreeNode *)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(struct BTreeNode));
j = median + <span class="hljs-number">1</span>;
<span class="hljs-keyword">while</span> (j <= MAX) {
(*newNode)->item[j - median] = node->item[j];
(*newNode)->linker[j - median] = node->linker[j];
j++;
}
node->count = median;
(*newNode)->count = MAX - median;
<span class="hljs-keyword">if</span> (pos <= MIN) {
addValToNode(item, pos, node, child);
} <span class="hljs-keyword">else</span> {
addValToNode(item, pos - median, *newNode, child);
}
*pval = node->item[node->count];
(*newNode)->linker[<span class="hljs-number">0</span>] = node->linker[node->count];
node->count--;
}
<span class="hljs-comment">// Set the value in the node</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">setValueInNode</span><span class="hljs-params">(<span class="hljs-keyword">int</span> item, <span class="hljs-keyword">int</span> *pval,
struct BTreeNode *node, struct BTreeNode **child)</span> </span>{
<span class="hljs-keyword">int</span> pos;
<span class="hljs-keyword">if</span> (!node) {
*pval = item;
*child = <span class="hljs-literal">NULL</span>;
<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
}
<span class="hljs-keyword">if</span> (item < node->item[<span class="hljs-number">1</span>]) {
pos = <span class="hljs-number">0</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">for</span> (pos = node->count;
(item < node->item[pos] && pos > <span class="hljs-number">1</span>); pos--)
;
<span class="hljs-keyword">if</span> (item == node->item[pos]) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"Duplicates not allowed\n"</span>);
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
}
<span class="hljs-keyword">if</span> (setValueInNode(item, pval, node->linker[pos], child)) {
<span class="hljs-keyword">if</span> (node->count < MAX) {
addValToNode(*pval, pos, node, *child);
} <span class="hljs-keyword">else</span> {
splitNode(*pval, pval, pos, node, *child, child);
<span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
}
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
<span class="hljs-comment">// Insertion operation</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">insertion</span><span class="hljs-params">(<span class="hljs-keyword">int</span> item)</span> </span>{
<span class="hljs-keyword">int</span> flag, i;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BTreeNode</span> *<span class="hljs-title">child</span>;</span>
flag = setValueInNode(item, &i, root, &child);
<span class="hljs-keyword">if</span> (flag)
root = createNode(i, child);
}
<span class="hljs-comment">// Copy the successor</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">copySuccessor</span><span class="hljs-params">(struct BTreeNode *myNode, <span class="hljs-keyword">int</span> pos)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BTreeNode</span> *<span class="hljs-title">dummy</span>;</span>
dummy = myNode->linker[pos];
<span class="hljs-keyword">for</span> (; dummy->linker[<span class="hljs-number">0</span>] != <span class="hljs-literal">NULL</span>;)
dummy = dummy->linker[<span class="hljs-number">0</span>];
myNode->item[pos] = dummy->item[<span class="hljs-number">1</span>];
}
<span class="hljs-comment">// Remove the value</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">removeVal</span><span class="hljs-params">(struct BTreeNode *myNode, <span class="hljs-keyword">int</span> pos)</span> </span>{
<span class="hljs-keyword">int</span> i = pos + <span class="hljs-number">1</span>;
<span class="hljs-keyword">while</span> (i <= myNode->count) {
myNode->item[i - <span class="hljs-number">1</span>] = myNode->item[i];
myNode->linker[i - <span class="hljs-number">1</span>] = myNode->linker[i];
i++;
}
myNode->count--;
}
<span class="hljs-comment">// Do right shift</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">rightShift</span><span class="hljs-params">(struct BTreeNode *myNode, <span class="hljs-keyword">int</span> pos)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BTreeNode</span> *<span class="hljs-title">x</span> = <span class="hljs-title">myNode</span>-><span class="hljs-title">linker</span>[<span class="hljs-title">pos</span>];</span>
<span class="hljs-keyword">int</span> j = x->count;
<span class="hljs-keyword">while</span> (j > <span class="hljs-number">0</span>) {
x->item[j + <span class="hljs-number">1</span>] = x->item[j];
x->linker[j + <span class="hljs-number">1</span>] = x->linker[j];
}
x->item[<span class="hljs-number">1</span>] = myNode->item[pos];
x->linker[<span class="hljs-number">1</span>] = x->linker[<span class="hljs-number">0</span>];
x->count++;
x = myNode->linker[pos - <span class="hljs-number">1</span>];
myNode->item[pos] = x->item[x->count];
myNode->linker[pos] = x->linker[x->count];
x->count--;
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Do left shift</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">leftShift</span><span class="hljs-params">(struct BTreeNode *myNode, <span class="hljs-keyword">int</span> pos)</span> </span>{
<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BTreeNode</span> *<span class="hljs-title">x</span> = <span class="hljs-title">myNode</span>-><span class="hljs-title">linker</span>[<span class="hljs-title">pos</span> - 1];</span>
x->count++;
x->item[x->count] = myNode->item[pos];
x->linker[x->count] = myNode->linker[pos]->linker[<span class="hljs-number">0</span>];
x = myNode->linker[pos];
myNode->item[pos] = x->item[<span class="hljs-number">1</span>];
x->linker[<span class="hljs-number">0</span>] = x->linker[<span class="hljs-number">1</span>];
x->count--;
<span class="hljs-keyword">while</span> (j <= x->count) {
x->item[j] = x->item[j + <span class="hljs-number">1</span>];
x->linker[j] = x->linker[j + <span class="hljs-number">1</span>];
j++;
}
<span class="hljs-keyword">return</span>;
}
<span class="hljs-comment">// Merge the nodes</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">mergeNodes</span><span class="hljs-params">(struct BTreeNode *myNode, <span class="hljs-keyword">int</span> pos)</span> </span>{
<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BTreeNode</span> *<span class="hljs-title">x1</span> = <span class="hljs-title">myNode</span>-><span class="hljs-title">linker</span>[<span class="hljs-title">pos</span>], *<span class="hljs-title">x2</span> = <span class="hljs-title">myNode</span>-><span class="hljs-title">linker</span>[<span class="hljs-title">pos</span> - 1];</span>
x2->count++;
x2->item[x2->count] = myNode->item[pos];
x2->linker[x2->count] = myNode->linker[<span class="hljs-number">0</span>];
<span class="hljs-keyword">while</span> (j <= x1->count) {
x2->count++;
x2->item[x2->count] = x1->item[j];
x2->linker[x2->count] = x1->linker[j];
j++;
}