-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsaLib.h
executable file
·323 lines (294 loc) · 8.11 KB
/
dsaLib.h
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
/*
* =========================================================================================
* Name : dsaLib.h
* Author : Duc Dung Nguyen
* Email : [email protected]
* Copyright : Faculty of Computer Science and Engineering - Bach Khoa University
* Description : library for Assignment 2 - Data structures and Algorithms - Fall 2017
* =========================================================================================
*/
#ifndef A02_DSALIB_H
#define A02_DSALIB_H
#include <string>
#include <math.h>
#include <vector>
using namespace std;
class DSAException {
int _error;
string _text;
public:
DSAException() : _error(0), _text("Success") {}
DSAException(int err) : _error(err), _text("Unknown Error") {}
DSAException(int err, const char* text) : _error(err), _text(text) {}
int getError() { return _error; }
string& getErrorText() { return _text; }
};
template <class T>
struct L1Item {
T data;
L1Item<T> *pNext;
L1Item() : pNext(NULL) {}
L1Item(T &a) : data(a), pNext(NULL) {}
};
template <class T>
class L1List {
L1Item<T> *_pHead;// The head pointer of linked list
size_t _size;// number of elements in this list
public:
L1List() : _pHead(NULL), _size(0) {}
//~L1List();
void clean();
bool isEmpty() {
return _pHead == NULL;
}
size_t getSize() {
return _size;
}
T& at(int i);
T& operator[](int i);
bool find(T& a, int& idx);
T* find(T& a);
T* findLast(T& a);
int insert(int i, T& a);
int remove(int i);
int removeAll(T& a);
int push_back(T& a);
int insertHead(T& a);
int removeHead();
int removeLast();
void reverse();
void traverse(void (*op)(T&)) {
L1Item<T> *p = _pHead;
while (p) {
op(p->data);
p = p->pNext;
}
}
void traverse(void (*op)(T&, void*), void* pParam) {
L1Item<T> *p = _pHead;
while (p) {
op(p->data, pParam);
p = p->pNext;
}
}
};
template<class T>
T& L1List<T>::operator[](int i){
L1Item<T> *p = _pHead;
if (i <= _size && i >= 0) {
for (int index = 0; index < i; index++) {
p = p->pNext;
}
return p->data;
}
}
/// Insert item to the end of the list
/// Return 0 if success
template <class T>
int L1List<T>::push_back(T &a) {
if (_pHead == NULL) {
_pHead = new L1Item<T>(a);
}
else {
L1Item<T> *p = _pHead;
while (p->pNext) p = p->pNext;
p->pNext = new L1Item<T>(a);
}
_size++;
return 0;
}
/// Insert item to the front of the list
/// Return 0 if success
template <class T>
int L1List<T>::insertHead(T &a) {
L1Item<T> *p = new L1Item<T>(a);
p->pNext = _pHead;
_pHead = p;
_size++;
return 0;
}
/// Remove the first item of the list
/// Return 0 if success
template <class T>
int L1List<T>::removeHead() {
if(_pHead) {
L1Item<T>* p = _pHead;
_pHead = p->pNext;
delete p;
_size--;
return 0;
}
return -1;
}
/// Remove the last item of the list
/// Return 0 if success
template <class T>
int L1List<T>::removeLast() {
if(_pHead) {
if(_pHead->pNext) {
L1Item<T>* prev = _pHead;
L1Item<T>* pcur = prev->pNext;
while(pcur->pNext) {
prev = pcur;
pcur = pcur->pNext;
}
delete pcur;
prev->pNext = NULL;
}
else {
delete _pHead;
_pHead = NULL;
}
_size--;
return 0;
}
return -1;
}
/************************************************************************
* This section is for AVL tree
************************************************************************/
int max(int a, int b){
return a>b?a:b;
}
enum banlance{ L = -1, E = 0, R = 1};
template <class T>
struct AVLNode {
T _data;
AVLNode<T> *_left, *_right;
// #ifdef AVL_USE_HEIGHT
int _height;
// AVLNode(T &a) : _data(a), _left(NULL), _right(NULL), _height(1) {}
// #else
int _bFactor;
AVLNode(T &a) : _data(a), _left(NULL), _right(NULL), _bFactor(0), _height(1) {}
};
template <class T>
class AVLTree {
AVLNode<T> *_root;
public:
AVLTree() : _root(NULL) {}
~AVLTree() { destroy(_root); }
bool find(T& key, T* &ret) { return find(_root, key, ret); }
bool insert(T& key) { return insert(_root, key); }
bool remove(T& key) { return remove(_root, key); }
void traverseNLR(void (*op)(T&)) { traverseNLR(_root, op); }
void traverseLNR(void (*op)(T&)) { traverseLNR(_root, op); }
void traverseLRN(void (*op)(T&)) { traverseLRN(_root, op); }
protected:
AVLNode<T> *findMin(AVLNode<T> *pR);
void destroy(AVLNode<T>* &pR);
bool find(AVLNode<T> *pR, T& key, T* &ret);
bool insert(AVLNode<T>* &pR, T& a);
bool remove(AVLNode<T>* &pR, T& a);
void traverseNLR(AVLNode<T> *pR, void (*op)(T&));
void traverseLNR(AVLNode<T> *pR, void (*op)(T&));
void traverseLRN(AVLNode<T> *pR, void (*op)(T&));
int getBalance(AVLNode<T> *pR);
void rotLeft(AVLNode<T>* &pR);
void rotRight(AVLNode<T>* &pR);
void rotLR(AVLNode<T>* &pR);
void rotRL(AVLNode<T>* &pR);
bool balanceLeft(AVLNode<T>* &pR);
bool balanceRight(AVLNode<T>* &pR);
};
template<class T>
int AVLTree<T>::getBalance(AVLNode<T> *pR){
return pR->_left->_height - pR->_right->_height;
}
template<class T>
void AVLTree<T>::rotLeft(AVLNode<T> *&pR){
if(!pR) return;
AVLNode<T> *temp = pR;
pR = pR->_right;
temp->_right = pR->_left;
pR->_left = temp;
temp->_height = 1+max(temp->_left->_height, temp->_right->_height);
pR->_height = 1+max(pR->_left->_height, pR->_right->_height);
}
template<class T>
void AVLTree<T>::rotRight(AVLNode<T> *&pR){
if(!pR) return;
AVLNode<T> *temp = pR;
pR = pR->_left;
temp->_left = pR->_right;
pR->_right = temp;
temp->_height = 1+max(temp->_left->_height, temp->_right->_height);
pR->_height = 1+max(pR->_left->_height, pR->_right->_height);
}
template<class T>
bool AVLTree<T>::insert(AVLNode<T>* &pR, T& a){
if (!pR) pR = new AVLNode<T>(a);
if(a < pR->_data)
insert(pR->_left, a);
else if (a > pR->_data) {
insert(pR->_right, a);
}
else return true;
pR->_height = 1+max(pR->_left->_height, pR->_right->_height);
int balance = getBalance(pR);
if(balance > 1 && a < pR->_left->_data){ //L-L
rotRight(pR);
}
if(balance < -1 && a > pR->_right->_data){ //R-R
rotLeft(pR);
}
if(balance > 1 && a > pR->_left->data){ //L-R
rotLeft(pR->_left);
rotRight(pR);
}
if(balance < -1 && a < pR->_right->_data){ //R-L
rotRight(pR->_right);
rotLeft(pR);
}
return true;
}
template<class T>
bool AVLTree<T>::remove(AVLNode<T>* &pR, T& a){
if(!pR) return false;
if(a < pR->_data){
remove(pR->_left, a);
}
else if (a > pR->_data) {
remove(pR->_right, a);
}
else {
if(!pR->_left || !pR->_right){
AVLNode<T> *temp = pR->_left ? pR->_left : pR->_right;
if(!temp){
temp = pR;
pR = NULL;
}
else
*pR = *temp;
delete temp;
}
else {
//2 children
AVLNode<T> *temp = findMin(pR->_right);
pR->_data = temp->_data;
remove(pR, temp->_data);
}
}
//if(!pR) return false;
pR->_height = 1+max(pR->_left->_height - pR->_right->_height);
int balance = getBalance(pR);
if(balance > 1 && getBalance(pR->_left) >= 0){
rotRight(pR);
return true;
}
if(balance < -1 && getBalance(pR->_right) <= 0){
rotLeft(pR);
return true;
}
if(balance > 1 && getBalance(pR->_left) < 0){
rotLeft(pR->_left);
rotRight(pR);
return true;
}
if(balance < -1 && getBalance(pR->_right)>0){
rotRight(pR->_right);
rotLeft(pR);
return true;
}
}
#endif //A02_DSALIB_H