-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAIFBS_BTree.cpp
77 lines (66 loc) · 1.65 KB
/
AIFBS_BTree.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
#include "AIFBS_BTreeNode.hpp"
#include "AIFBS_BTree.hpp"
namespace AIFBS {
template <class T>
void AIFBS_BTree<T>::insert(T k)
{
// If tree is empty
if (root == NULL)
{
// Allocate memory for root
root = new AIFBS_BTreeNode<T>(t, true);
root->keys[0] = k; // Insert key
root->n = 1; // Update number of keys in root
}
else // If tree is not empty
{
// If root is full, then tree grows in height
if (root->n == 2*t-1)
{
// Allocate memory for new root
AIFBS_BTreeNode<T> *s = new AIFBS_BTreeNode<T>(t, false);
// Make old root as child of new root
s->C[0] = root;
// Split the old root and move 1 key to the new root
s->splitChild(0, root);
// New root has two children now. Decide which of the
// two children is going to have new key
int i = 0;
if (s->keys[0] < k)
i++;
s->C[i]->insertNonFull(k);
// Change root
root = s;
}
else // If root is not full, call insertNonFull for root
root->insertNonFull(k);
}
}
//************************************************************//
// REMOVE ADDED //
//************************************************************//
template <class T>
void AIFBS_BTree<T>::remove(T k)
{
if (!root)
{
std::cout << "The tree is empty\n";
return;
}
// Call the remove function for root
root->remove(k);
// If the root node has 0 keys, make its first child as the new root
// if it has a child, otherwise set root as NULL
if (root->n==0)
{
AIFBS_BTreeNode<T> *tmp = root;
if (root->leaf)
root = NULL;
else
root = root->C[0];
// Free the old root
delete tmp;
}
return;
}
}