-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory_tracker-AVLTree.cpp
More file actions
475 lines (412 loc) · 13.1 KB
/
inventory_tracker-AVLTree.cpp
File metadata and controls
475 lines (412 loc) · 13.1 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//This program acts as an inventory tracker, using an AVL tree
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class AVLTree {
private:
struct Node { //represent a part in inventory
int partnum; //part number; key value that uniquely identifies the part
string description; //describes the part
int qty; //number of the part on hand
double price; //price of part
Node* left, *right; //address of left/right child node
};
Node* root; //address of root node
void clear(Node*&);
int height(Node*);
void rotateRight(Node*&);
void rotateLeft(Node*&);
void rotateRightLeft(Node*&);
void rotateLeftRight(Node*&);
int difference(Node*);
void balance(Node*&);
void append(Node*&, int, int, double, string);
void remove(Node*&, int);
string getOutOfStock(Node*);
string getPart(Node*, int);
void print(Node*) const;
public:
AVLTree(); //constructor
~AVLTree(); //destructor
void clear(); //clears tree
void append(int, int, double, string); //adds nodes to tree
void remove(int); //remove node from tree
string getOutOfStock();
string getPart(int);
void print() const;
};
/**************************************************************************************
* ************ MAIN() ***********************
* ************************************************************************************/
int main() {
char choice; //user option selection in menu
AVLTree inventory;
string description;
double price;
int partNumber, quantity;
//menu
do
{
cout << "(A)dd a part\n"
<< "(R)emove a part\n"
<< "(S)earch for a part\n"
<< "(O)ut of stock\n"
<< "(Q)uit\n"
<< "[Enter your choice] >> ";
cin >> choice;
switch(choice)
{
//add a part to the database; provides an interface for adding new parts to a database;
// parts have a description, a part ID number, a price, and a quantity on hand
case 'a':
case 'A':
{
//add
cout << "Enter part description: ";
cin.ignore();
getline(cin,description);
cout << "Enter the price: ";
cin >> price;
cout << "Enter the part number: ";
cin >> partNumber;
cout << "Enter the quantity on hand: ";
cin >> quantity;
inventory.append(partNumber,quantity,price,description);
cout << "Update of inventory:\n";
//verify append occurred
inventory.print();
break;
}
//remove a part from the database; provides an interface for removing a part from the inventory database
case 'r':
case 'R':
{
//remove
cout << "Enter part number for part to remove: ";
cin >> partNumber;
inventory.remove(partNumber);
cout << "Update of inventory:\n";
//verify removal
inventory.print();
break;
}
//search for a part in the database; searches the database for a specific part by ID number, meaning
// the ID number is used for the search key; if found, the part's description, price, and quantity
// on hand is displayed; if not, an appropriate message is displayed
case 's':
case 'S':
{
//search
cout << "Enter part number to search for: ";
cin >> partNumber;
cout << inventory.getPart(partNumber) << endl;
break;
}
//out of stock; displays a list of all parts in the database with a quantity of 0
case 'o':
case 'O':
{
//getOutOfStock
cout << "The following items are out of stock:\n";
cout << inventory.getOutOfStock();
break;
}
//quit; terminate the program
case 'q':
case 'Q':
{
return 0;
}
//invalid option
default:
cout << "Please select a valid option.\n";
}
} while (choice != 'q' || choice != 'Q');
return 0;
}
/**************************************************************************************
* ************ FUNCTIONS ***********************
* ************************************************************************************/
//AVLTree function: constructor; sets root to null
AVLTree::AVLTree()
{
root = nullptr;
}
//~AVLTree function: destructor; frees all memory used by calling public clear() method
AVLTree::~AVLTree()
{
clear();
}
//clear private function: frees all memory used by the object
void AVLTree::clear(Node*& r)
{
if (r != nullptr)
{
clear(r->left);
clear(r->right);
delete r;
r = nullptr;
}
}
//clear public function: calls the private version, passing the root pointer as an argument
void AVLTree::clear()
{
clear(root);
root = nullptr;
}
//height function: returns the height of the (sub)tree; accepts a node pointer as an argument
int AVLTree::height(Node* r)
{
int lh = 0, rh = 0;
if (r == nullptr)
return 0;
lh = height(r->left);
rh = height(r->right);
if (lh > rh)
return(1+lh);
else
return(1+rh);
}
//rotateRight function: performs a right rotation; accepts a node pointer as an argument
void AVLTree::rotateRight(Node*& r)
{
Node *p = r->left;
r->left = p->right;
p->right = r;
r = p;
}
//rotateLeft function: performs a left rotation; accepts a node pointer as an argument
void AVLTree::rotateLeft(Node*& r)
{
Node *p = r->right;
r->right = p->left;
p->left = r;
r = p;
}
//rotateRightLeft function: performs a right-left rotation; accepts a node pointer as an argument
void AVLTree::rotateRightLeft(Node*& r)
{
rotateRight(r->right);
rotateLeft(r);
}
//rotateLeftRight function: performs a left-right rotation; accepts a node pointer as an argument
void AVLTree::rotateLeftRight(Node*& r)
{
rotateLeft(r->left);
rotateRight(r);
}
//difference function: returns the difference in height between two subtrees by calling height on each subtree
// accepts a Node pointer as an argument
int AVLTree::difference(Node* r)
{
//check if difference is working
int diff = 0;
diff = (height(r->right) - height(r->left));
cout << "difference: " << diff << endl;
return diff;
// return (height(r->right) - height(r->left));
}
//balance function: determines what, if any, rotation(s) should be applied to subtrees by calling difference
// and rotate methods; accepts a Node pointer by reference
void AVLTree::balance(Node*& r)
{
//cout << "balance called\n";
if (difference(r) == -2)
{
//if nodes are positioned as ">"
if (difference(r->left) == -1)
rotateRight(r);
//if nodes are positioned as "\"
else
rotateLeftRight(r);
//verify balance was done
//cout << "balance done\n.";
}
else if (difference(r) == 2)
{
//if nodes are positioned as "<"
if (difference(r->right) == -1)
rotateRightLeft(r);
//if nodes are positioned as "/"
else
{
rotateLeft(r);
}
//cout << "balance done\n.";
}
}
//append private function: adds part to tree
void AVLTree::append(Node*& r, int num, int qty, double price, string desc)
{
//case: tree is empty
if (r == nullptr)
{
r = new Node;
r->partnum = num;
r->qty = qty;
r->price = price;
r->description = desc;
r->left = nullptr;
r->right = nullptr;
}
//case: element to add requires movement to the left
else if (num < r->partnum)
{
append(r->left, num, qty, price, desc);
balance(r);
}
//case: element to add requires movement to the right
else
{
append(r->right, num, qty, price, desc);
balance(r);
}
}
//append public function: passes info to private append method; accepts a part's number, qty, description, and price as arguments
void AVLTree::append(int num, int qty, double price, string desc)
{
append(root, num, qty, price, desc);
}
//remove private function: causes the matching part to be removed from the tree
void AVLTree::remove(Node*& r, int num)
{
//case: tree is empty
if (r == nullptr)
{
return;
}
//case: move left to reach element to remove
else if (num < r->partnum)
{
remove(r->left, num);
balance(r);
}
//case: move right to reach element to remove
else if (num > r->partnum)
{
remove(r->right, num);
balance(r);
}
//case: remove node currently at
else
{
Node* temp;
//case: node has no children
if (r->left == nullptr && r->right == nullptr)
{
delete r;
r = nullptr;
}
//case: node has left child only
else if (r->left != nullptr && r->right == nullptr)
{
temp = r;
r = r->left;
delete temp;
}
//case: node has right child only
else if (r->left == nullptr && r->right != nullptr)
{
temp = r;
r = r->right;
delete temp;
}
//case: node has left and right children
else
{
temp = r->right;
while (temp->left)
{
temp = temp->left;
}
temp->left = r->left;
temp = r;
r = r->right;
delete temp;
}
}
}
//remove public function: passes info to the private remove method; accepts a part's number
void AVLTree::remove(int num)
{
remove(root, num);
}
//getOutOfStock private function: searches the tree for all parts with qty 0, building a string of parts as it goes;
// for each part, the description, qty, price, and number is added to the string
string AVLTree::getOutOfStock(Node* r)
{
//case: tree is empty
if (r == nullptr)
return {};
string str;
//add elements in tree in order
str += getOutOfStock(r->left);
if(r->qty <= 0)
{
ostringstream ss;
ss << r->partnum << ", "
<< r->description << ", "
<< r->price << ", "
<< r->qty << endl;
str += ss.str();
ss.clear();
}
str += getOutOfStock(r->right);
return str;
}
//getOutOfStock public function: returns the string returned by the private getOutOfStock method;
// calls the private version, passing it the root pointer as an argument
string AVLTree::getOutOfStock()
{
return getOutOfStock(root);
}
//getPart private function: if it finds the part, returns a string consisting of the part description, price, qty,
// and number; if it doesn't find it, it returns an appropriate error message such as
// "OUT OF STOCK"
string AVLTree::getPart(Node* r, int num)
{
//case: tree is empty
if(r == nullptr)
{
return "OUT OF STOCK";
}
//case: element is found at current position
else if(r->partnum == num)
{
ostringstream ss;
ss << r->partnum << endl
<< r->description << endl
<< r->price << endl
<< r->qty << endl;
return ss.str();
}
//case: require movement to the left to find element
else if(num < r->partnum)
return getPart(r->left, num);
//case: require movement to the right to find element
else
return getPart(r->right, num);
}
//getPart public function: returns the string returned by the private getPart method; accepts a part's number as
// it's only argument and calls the private version, passing it the number and the root pointer
string AVLTree::getPart(int num)
{
return getPart(root, num);
}
//print
void AVLTree::print() const
{
print(root);
}
void AVLTree::print(Node* r) const
{
if (r == nullptr)
return;
print(r->left);
cout << r->partnum << "\t"
<< r->description << "\t"
<< r->qty << "\t"
<< r->price << endl;
print(r->right);
}