-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeafNode.cpp
155 lines (116 loc) · 3.33 KB
/
LeafNode.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
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
#include <iostream>
#include "LeafNode.h"
#include "InternalNode.h"
#include "QueueAr.h"
using namespace std;
LeafNode::LeafNode(int LSize, InternalNode *p,
BTreeNode *left, BTreeNode *right) : BTreeNode(LSize, p, left, right)
{
values = new int[LSize];
} // LeafNode()
int LeafNode::getMinimum()const
{
if(count > 0) // should always be the case
return values[0];
else
return 0;
} // LeafNode::getMinimum()
LeafNode* LeafNode::insert(int value)
{
int pos1, pos2;
int first, last;
int temp;
// if count is smaller than leafSize, add
if (count < leafSize)
{
for(pos1 = 0; pos1 < count-1; pos1++)
{
if(value < values[pos1])
{
temp = values[pos1];
values[pos1] = value;
count++;
for(pos2 = count-1; pos2 > pos1; pos2--)
values[pos2+1] = values[pos2];
// shift up all elements
values[pos2] = temp; // everything in its right place
if(parent)
parent->setKeys(this);
return NULL;
}
}
}
// find first and last
if(value < values[0])
{
first = value;
}
for(pos1 = 0; pos1 < count-1; pos1++)
{
if(value < values[pos1])
{
temp = values[pos1];
values[pos1] = value; // do not increment count
for(pos2 = count-1; pos2 > pos1; pos2--)
values[pos2+1] = values[pos2];
// shift up all elements
values[pos2] = temp; // everything in its right place
last = values[count];
}
}
// give left, give right, else split
if(leftSibling->getCount() < leafSize)
{
leftSibling->insert(last);
for(pos1 = 0; pos1 < count-1; pos1++)
{
values[pos1] = values[pos1+1];
} // shift down all elements
values[count-1] = last;
if(parent)
parent->setKeys(this);
return NULL;
// add to left
} else if(rightSibling->getCount() < leafSize)
{
rightSibling->insert(last);
if(parent)
parent->setKeys(this);
return NULL;
// add to right
} else {
return this->split(value);
// left and right both full. split.
}
return NULL; // to avoid warnings for now.
} // LeafNode::insert()
void LeafNode::print(Queue <BTreeNode*> &queue)
{
cout << "Leaf: ";
for (int i = 0; i < count; i++)
cout << values[i] << ' ';
cout << endl;
} // LeafNode::print()
LeafNode* LeafNode::split(int value) //(int value, int last)?
{
int i, pos = 0;
LeafNode *ptr = new LeafNode(leafSize, parent, this, rightSibling);
if(rightSibling)
rightSibling->setLeftSibling(ptr);
rightSibling = ptr;
for(i = (leafSize+1) / 2; i < leafSize; i++)
{
ptr->values[pos] = values[i];
ptr->count++;
pos++;
}
ptr->values[i+1] = value;
ptr->count++;
if(parent)
parent->setKeys(this); //check?
return ptr;
}
int LeafNode::getCount()
{
return count;
}