-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cpp
221 lines (188 loc) · 5.57 KB
/
Node.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
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
/**
@file Node.cpp
@brief Implementation of base Node Class
@author Brandon Theisen, Jason Pederson, Kelvin Shultz, Chris, Jared
*/
#include "Node.h"
#include <vector>
#include <iostream>
using namespace std;
//-----CONSTRUCTOR-----\\
/**Default Constructor for Node
@post Creates a base Node for either a leaf node or a interior node*/
template<class T>
Node<T>::Node()
{
capacity = 6; //6 is default
//Default Constructor
}
//-----CONSTRUCTOR WITH CAPACITY-----\\
/**Default Constructor for Node with capacity
@pre Accepts a capacity for a node, depending on what type of node is being created
@param cap: capacity of the size in the node
@post Creates a base Node for either a leaf node or a interior node with a capacity*/
template<class T>
Node<T>::Node(int cap)
{
capacity = cap;
}
//-----COPY CONSTRUCTOR-----\\
/**Copy constructor that copies the node that calls it
@pre Accepts a node to be copied
@param Node to be copied
@post Copies the node that called the constructor*/
template<class T>
Node<T>::Node(const Node& newCopy)
//: keys(newCopy.keys), capacity(newCopy.capacity), parentPtr(newCopy.parentPtr)
{}
//-----GET KEY AT-----\\
/**Returns the key at its position
@pre Position must be less than keys.size()
@param position: position of key that is being called
@post Returns the position of the key being searched*/
template<class T>
int Node<T>::getKeyAt(int position)
{
return keys.at(position);
}
//-----SEARCH KEYS-----\\
/**Search Keys
@param key: key that will be searched for
@post Return the position of the key in the vector of keys*/
template<class T>
int Node<T>::searchKey(int key)
{
for(int i = 0; i < keys.size(); i++)
{
if(key < keys.at(i))
{
return i;
}
}
return keys.size();
}
//-----ADD NEW KEY-----\\
/**Add a key
@pre Node must have room for the new key. size() <= capacity
@param newKey: key that will be entered
@post Return true or false on if the key was successfully added*/
template<class T>
int Node<T>::addKey(int newKey)
{
for(int i = 0; i < keys.size(); i++)
{
if(newKey < keys.at(i))
{
keys.insert(keys.begin() + i, newKey);
return i;
}
else if(newKey == keys.at(i))
return -1;
}
//We add the newKey to the end, since it was larger than all existing.
//We checked to make sure there is room to add a key, otherwise
//push_back would be overextending our keys <vector>.
//With the precondition met, a push_back() is OK.
int position = keys.size();
keys.push_back(newKey);
return position;
}
//-----GET PARENT-----\\
/**Get Parent of Node
@post Return the parent of the node, otherwise will return NULL*/
template<class T>
Node<T>* Node<T>::getParent()
{
return parentPtr;
}
//-----SET PARENT-----\\
/**Set Parent to a Node
@param newParentPtr: pointer to be set as parent to a node
@post Sets the parent to the node to newParentPtr*/
template<class T>
void Node<T>::setParent(Node<T>* newParentPtr)
{
parentPtr = newParentPtr;
}
/**Get the size of the nodes child
@post Return the size the child node*/
template<class T>
int Node<T>::getChildSize()
{
return 0;
}
//-----GET SIZE-----\\
/**Get the size of the node
@post Return the number of items in the node*/
template<class T>
int Node<T>::getSize()
{
return keys.size();
}
//-----CONTAINS-----\\
/**Checks to see if a key is contained in the node
@param key: key that will be searched for
@post Return true or false depending if the key was found*/
template<class T>
bool Node<T>::contains(int key)
{
for(int i = 0; i < keys.size(); i++)
{
if(key < keys.at(i))
{ //Exit early because we are checking a sorted list
//If the previous comparison was greater than, and now it is less than,
//it clearly is not in the list and would be between these two comparisons.
//As in, key > 5 but then key < 7 on the next loop, so it does not exist.
return false;
}
else if(key == keys.at(i))
{ //We found a match
return true;
}
}
//No matches were found
return false;
}
//-----SPLIT-----\\
/**Split Node
@param newNodePtr: pointer to node that will be split, provided by the B+ Tree class
@post Creates a new node and places half the keys into the new node*/
template<class T>
void Node<T>::split(Node<T>* &newNodePtr)
{
int value = getSize();
for(int i = (keys.size()/2); i < keys.size(); i++)
{
newNodePtr->addKey(getKeyAt(i));
}
//Clear out the keys that were just moved from the existing node
keys.erase(keys.begin() + (keys.size()/2), keys.end());
//Check parent for an open pointer in children <vector>.
//If none are available, a split will be required.
}
//-----MERGE-----\\
/**Merge two nodes together
@param otherNodePtr: Pointer of the node the be merged with
@post Merges the two nodes together, placing all keys into the orinigal node*/
template<class T>
void Node<T>::mergeNodes(Node<T>* &otherNodePtr)
{
for(int i = 0; i < otherNodePtr->getSize(); i++)
{
//Since the keys will be in order and otherNode is to the right,
//we can simply use push_back() to insert.
keys.push_back(otherNodePtr->getKeyAt(i));
}
//otherNode.getParent() and set it to NULL, since we are deleting its child.
//Check parent to see if we should be merging it with a sibling, after
//deleting one of its children.
delete otherNodePtr;
otherNodePtr = NULL;
}
//-----DESTRUCTOR-----\\
//
template<class T>
Node<T>::~Node()
{
//Destructor
}