-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMariosBST.cpp
More file actions
162 lines (146 loc) · 4.96 KB
/
Copy pathMariosBST.cpp
File metadata and controls
162 lines (146 loc) · 4.96 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
// Written by Mario Misencik
#include <iostream>
#include <string>
#include <pthread.h>
#include "./EvansMap.cpp"
using namespace std;
class MariosBST : public ObjectInterface {
private:
struct node {
int key; // Integer value of node
string value; // String value of node
node *left, *right; // Children of node
node(int key, string value) {
this->key = key;
this->value = value;
this->left = this->right = NULL;
}
};
pthread_mutex_t mutex; // Mutex for the tree
node *root; // The root of the tree
// Finds the node with the minimum value in the node's subtree.
node *findMinimum(node *n) {
if (!n) return NULL;
while (n->left) n = n->left;
return n;
}
// Recursively removes a node from the tree with the corresponding key.
node *removeNode(node *n, int key) {
if (!n) return NULL;
else if (key < n->key) n->left = removeNode(n->left,key);
else if (key > n->key) n->right = removeNode(n->right,key);
else {
if (!n->left) return n->right;
else if (!n->right) return n->left;
else {
node *temp = findMinimum(n->right);
n->key = temp->key;
n->value = temp->value;
n->right = removeNode(n->right,temp->key);
}
}
return n;
}
// Searches in the tree for the key and determines whether it exists in it.
bool containsKey(int key) {
node *current = root;
while (current) {
if (key < current->key) current = current->left;
else if (key > current->key) current = current->right;
else return true;
}
return false;
}
// Recursively deletes all nodes in the tree.
void clearNodes(node *n) {
if (!n) return;
clearNodes(n->left);
clearNodes(n->right);
delete n;
}
public:
MariosBST() {
root = NULL;
pthread_mutex_init(&mutex, NULL);
}
~MariosBST() {
clear();
pthread_mutex_destroy(&mutex);
}
// Inserts a node.
bool insert(int key, string value) {
node *newNode = new node(key,value);
if (!root) {
root = newNode;
return true;
}
node *current = root;
while (current) {
if (key < current->key) {
if (!current->left) {
current->left = newNode;
return true;
}
current = current->left;
} else if (key > current->key) {
if (!current->right) {
current->right = newNode;
return true;
}
current = current->right;
} else return false;
}
return false;
}
// Removes a node if it is found in the tree.
bool remove(int key) {
if (!containsKey(key)) return false;
removeNode(root,key);
return true;
}
// Retrives the string value of a node based on its key.
string get(int key) {
node *current = root;
while (current) {
if (key < current->key) current = current->left;
else if (key > current->key) current = current->right;
else return current->value;
}
return "No " + to_string(key);
}
// Clears all the nodes, including the root.
void clear() {
if (root) {
clearNodes(root);
root = NULL;
}
}
string runOp(struct opsStruct *op) {
pthread_mutex_lock(&mutex);
switch (op->op) {
case 'I':
if (insert(op->key, op->value)) {
pthread_mutex_unlock(&mutex);
return "OK";
} else {
pthread_mutex_unlock(&mutex);
return "Fail";
}
case 'L':
pthread_mutex_unlock(&mutex);
return get(op->key);
case 'D':
if (remove(op->key)) {
pthread_mutex_unlock(&mutex);
return "OK";
} else {
pthread_mutex_unlock(&mutex);
return "Fail";
}
default:
pthread_mutex_unlock(&mutex);
return "Fail";
}
}
void print() {}
};