Skip to content

Commit 573773c

Browse files
authored
Search key in B-Tree
1 parent 3690ea3 commit 573773c

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

Search B-Tree

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Searching a key on a B-tree in C++
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
class TreeNode {
7+
int *keys;
8+
int t;
9+
TreeNode **C;
10+
int n;
11+
bool leaf;
12+
13+
public:
14+
TreeNode(int temp, bool bool_leaf);
15+
16+
void insertNonFull(int k);
17+
void splitChild(int i, TreeNode *y);
18+
void traverse();
19+
20+
TreeNode *search(int k);
21+
22+
friend class BTree;
23+
};
24+
25+
class BTree {
26+
TreeNode *root;
27+
int t;
28+
29+
public:
30+
BTree(int temp) {
31+
root = NULL;
32+
t = temp;
33+
}
34+
35+
void traverse() {
36+
if (root != NULL)
37+
root->traverse();
38+
}
39+
40+
TreeNode *search(int k) {
41+
return (root == NULL) ? NULL : root->search(k);
42+
}
43+
44+
void insert(int k);
45+
};
46+
47+
TreeNode::TreeNode(int t1, bool leaf1) {
48+
t = t1;
49+
leaf = leaf1;
50+
51+
keys = new int[2 * t - 1];
52+
C = new TreeNode *[2 * t];
53+
54+
n = 0;
55+
}
56+
57+
void TreeNode::traverse() {
58+
int i;
59+
for (i = 0; i < n; i++) {
60+
if (leaf == false)
61+
C[i]->traverse();
62+
cout << " " << keys[i];
63+
}
64+
65+
if (leaf == false)
66+
C[i]->traverse();
67+
}
68+
69+
TreeNode *TreeNode::search(int k) {
70+
int i = 0;
71+
while (i < n && k > keys[i])
72+
i++;
73+
74+
if (keys[i] == k)
75+
return this;
76+
77+
if (leaf == true)
78+
return NULL;
79+
80+
return C[i]->search(k);
81+
}
82+
83+
void BTree::insert(int k) {
84+
if (root == NULL) {
85+
root = new TreeNode(t, true);
86+
root->keys[0] = k;
87+
root->n = 1;
88+
} else {
89+
if (root->n == 2 * t - 1) {
90+
TreeNode *s = new TreeNode(t, false);
91+
92+
s->C[0] = root;
93+
94+
s->splitChild(0, root);
95+
96+
int i = 0;
97+
if (s->keys[0] < k)
98+
i++;
99+
s->C[i]->insertNonFull(k);
100+
101+
root = s;
102+
} else
103+
root->insertNonFull(k);
104+
}
105+
}
106+
107+
void TreeNode::insertNonFull(int k) {
108+
int i = n - 1;
109+
110+
if (leaf == true) {
111+
while (i >= 0 && keys[i] > k) {
112+
keys[i + 1] = keys[i];
113+
i--;
114+
}
115+
116+
keys[i + 1] = k;
117+
n = n + 1;
118+
} else {
119+
while (i >= 0 && keys[i] > k)
120+
i--;
121+
122+
if (C[i + 1]->n == 2 * t - 1) {
123+
splitChild(i + 1, C[i + 1]);
124+
125+
if (keys[i + 1] < k)
126+
i++;
127+
}
128+
C[i + 1]->insertNonFull(k);
129+
}
130+
}
131+
132+
void TreeNode::splitChild(int i, TreeNode *y) {
133+
TreeNode *z = new TreeNode(y->t, y->leaf);
134+
z->n = t - 1;
135+
136+
for (int j = 0; j < t - 1; j++)
137+
z->keys[j] = y->keys[j + t];
138+
139+
if (y->leaf == false) {
140+
for (int j = 0; j < t; j++)
141+
z->C[j] = y->C[j + t];
142+
}
143+
144+
y->n = t - 1;
145+
for (int j = n; j >= i + 1; j--)
146+
C[j + 1] = C[j];
147+
148+
C[i + 1] = z;
149+
150+
for (int j = n - 1; j >= i; j--)
151+
keys[j + 1] = keys[j];
152+
153+
keys[i] = y->keys[t - 1];
154+
n = n + 1;
155+
}
156+
157+
int main() {
158+
BTree t(3);
159+
t.insert(8);
160+
t.insert(9);
161+
t.insert(10);
162+
t.insert(11);
163+
t.insert(15);
164+
t.insert(16);
165+
t.insert(17);
166+
t.insert(18);
167+
t.insert(20);
168+
t.insert(23);
169+
170+
cout << "The B-tree is: ";
171+
t.traverse();
172+
173+
int k = 10;
174+
(t.search(k) != NULL) ? cout << endl
175+
<< k << " is found"
176+
: cout << endl
177+
<< k << " is not Found";
178+
179+
k = 2;
180+
(t.search(k) != NULL) ? cout << endl
181+
<< k << " is found"
182+
: cout << endl
183+
<< k << " is not Found\n";
184+
}

0 commit comments

Comments
 (0)