-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB_Tree.cpp
80 lines (64 loc) · 1023 Bytes
/
B_Tree.cpp
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
#include <iostream>
using namespace std;
class BTreeNode {
int* keys;
int t;
BTreeNode** C;
int n;
bool leaf;
public:
BTreeNode(int _t, bool _leaf);
void traverse();
BTreeNode*
search(int k); // returns NULL if k is not present.
friend class BTree;
};
class BTree {
BTreeNode* root;
int t;
public:
BTree(int _t)
{
root = NULL;
t = _t;
}
void traverse()
{
if (root != NULL)
root->traverse();
}
BTreeNode* search(int k)
{
return (root == NULL) ? NULL : root->search(k);
}
};
BTreeNode::BTreeNode(int _t, bool _leaf)
{
t = _t;
leaf = _leaf;
keys = new int[2 * t - 1];
C = new BTreeNode*[2 * t];
n = 0;
}
void BTreeNode::traverse()
{
int i;
for (i = 0; i < n; i++) {
if (leaf == false)
C[i]->traverse();
cout << " " << keys[i];
}
if (leaf == false)
C[i]->traverse();
}
BTreeNode* BTreeNode::search(int k)
{
int i = 0;
while (i < n && k > keys[i])
i++;
if (keys[i] == k)
return this;
if (leaf == true)
return NULL;
return C[i]->search(k);
}