-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPS.h
381 lines (332 loc) · 6.69 KB
/
DPS.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
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
#pragma once
#include <iostream>
#include <vector>
#include <list>
using namespace std;
class DPSDataBase // áûâøåå äåðåâî, êîòîðîå ñòàëî áàçîé äàííûõ ïðàâîíàðóøåíèé ïî âñåì âîäèòåëÿì
{
public:
// îïèñàíèå íàðóøåíèÿ ïðàâèë äîðîæíîãî äâèæåíèÿ
class Violation
{
public: // âîîáùå-òî ïî õîðîøåìó çäåñü äîëæíà áûòü ñåêöèÿ private!
string article; // ñòàòüÿ ïðàâîíàðóøåíèÿ
string name; // êîðîòêîå íàçâàíèå ïðàâîíàðóøåíèÿ
string description; // ïîäðîáíîå îïèñàíèå ïðàâîíàðóøåíèÿ
double penalty; // øòðàô â ãðí.
public:
// çäåñü áóäóò ãåòòåðû-ñåòòåðû, êîíñòðóêòîðû, äåñòðóêòîð
};
// êëàññ-àäàïòåð (îáîëî÷êà íàä ñòàíäàðòíûì STL-åâñêèì ñïèñêîì
class ViolationList
{
public: // çäåñü äîëæåí áûòü ïðèâàò!
//Violation first;
//Violation second;
//Violation third;
//Violation* ar;
//int violation_count;
//Vector<Violation> list; // òàê ìîæíî ñäåëàòü, åñëè ãîòîâ êëàññ øàáëîííîãî âåêòîðà
//ViolationVector list; // ýòî ñðàáîòàåò åñëè ïåðåäåëàòü âåêòîð èíòîâ â âåêòîð íàðóøåíèé
//vector<Violation> list; // èñïîëüçóåì ñòàíäàðòíûé STL-âñêèé âåêòîð! ïî÷åìó áû è íåò, åñëè íàðóøåíèé áóäåò ìíîãî, è ê íèì íóæåí áûñòðûé ïðîèñçîëüíûé äîñòóï
//SLL<Violation> list; // åñëè íàïèñàòü êàñòîìíûé øàáëîííûé êëàññ ñïèñîê
//ViolationSLL list;
list<Violation> list;
public:
void AddViolation(const Violation& v)
{
list.push_back(v);
}
void Print() const
{
for (auto v : list)
{
cout << v.name << "\n";
}
}
};
// êëàññ, êîòîðûé îïèñûâàåò ïàðó "êëþ÷-çíà÷åíèå"
// îäèí îáúåêò òàêîãî òèïà - ýòî îäèí óçåë äåðåâà (à äåðåâî áóäåò ñàìîé áàçîé äàííûõ)
class KeyValuePair
{
public: // äîëæåí áûòü private!!
string car_number; // key
ViolationList list; // value
public:
// ãåòòåðû ñåòòåðû êîíñòðóêòîðû äåñòðóêòîð - âñ¸ ýòî ìîæíî ñîçäàòü ðåøàðïåðîì èëè âèæóàë àññèñòîì íà 2 ìèíóòû!
};
// îïöèîíàëüíûé êëàññ (ìîæíî íå äåëàòü, åñëè ëåíü)
class Person
{
public: // äîëæåí áûòü private!!
string name;
string surname;
};
class CarInfo
{
public: // äîëæåí áûòü private!!
KeyValuePair info;
Person driver;
// îïöèîíàëüíî:
// ïîëå, êîòîðîå îòâå÷àåò çà íîìåð ñòðàõîâîãî ïîëèñà
// èíôîðìàöèÿ î ïðàâàõ âîäèòåëÿ (íîìåð ñåðèÿ ñðîê ãîäíîñòè)
// èíôîðìàöèÿ î äîêóìåíòàõ íà àâòîìîáèëü
};
struct Node // one element of the DPSDataBase
{
CarInfo* value; // useful data
Node* parent;
Node* right;
Node* left;
void ShowNode()
{
cout << value->info.car_number << "\n";
cout << value->driver.name << "\n";
cout << value->driver.surname << "\n";
value->info.list.Print();
}
};
///////////////////////////////////
private:
Node* root;
// int count;
public:
DPSDataBase()
{
root = nullptr;
}
bool IsEmpty()
{
return root == nullptr;
}
~DPSDataBase()
{
Clear();
}
private:
void ShowDPSDataBase(Node* elem)
{
if (elem != nullptr)
{
ShowDPSDataBase(elem->left);
elem->ShowNode();
ShowDPSDataBase(elem->right);
}
}
public:
void ShowDPSDataBase()
{
cout << "\n";
ShowDPSDataBase(root);
cout << "\n";
}
private:
Node* GetRoot()
{
return root;
}
int GetCount(Node* elem, int count)
{
if (elem != nullptr)
{
count = GetCount(elem->left, count);
count++; // 1
count = GetCount(elem->right, count);
}
return count;
}
public:
int GetCount()
{
int count = 0;
count = GetCount(root, count);
return count;
}
private:
void Clear(Node* elem)
{
if (elem != nullptr)
{
Clear(elem->left);
Clear(elem->right);
delete elem;
elem = nullptr;
}
}
public:
void Clear()
{
if (!IsEmpty())
{
Clear(root);
root = nullptr;
}
}
// îäèí èç ìåòîäîâ ïîèñêà! èùåì òà÷êó ïî íîìåðó àâòîìîáèëÿ!
// ìîæíî áóäåò ñäåëàòü åù¸ ýííîå êîëè÷åñòâî òàêèõ ìåòîäîâ - ñ ïîèñêîì ïî ôàìèëèè âîäèòåëÿ, èëè ïî êîíêðåòíîìó ïðàâîíàðóøåíèþ
// FindNodesByDriver
// FindNodesByViolation
Node* FindNodeByCarNumber(const string& car_number)
{
if (IsEmpty())
return nullptr;
Node* f = root;
while (true)
{
if (car_number < f->value->info.car_number)
// äëÿ ïîèñêà ïî ôàìèëèè âîäèòåëÿ
//if (driver_surname < f->value->driver.surname)
// äëÿ ïîèñêà ïî êîíêðåòíîìó ïðàâîíàðóøåíèþ
//if (violation < f->value->info.list[i])
{
if (f->left != nullptr)
f = f->left;
else
break;
}
else if (car_number > f->value->info.car_number)
{
if (f->right != nullptr)
f = f->right;
else
break;
}
else
return f;
}
return nullptr;
}
/*void AddNode(int value)
{
if (FindNode(value))
return;
Node* n = new Node;
n->right = n->left = nullptr;
n->value = value;
Node* parent = nullptr;
if (IsEmpty())
{
root = n;
root->parent = nullptr;
}
else
{
Node* p = root;
while (p != nullptr)
{
parent = p;
if (n->value > p->value)
p = p->right;
else
p = p->left;
}
n->parent = parent;
if (n->value < parent->value)
parent->left = n;
else
parent->right = n;
}
}
DPSDataBase& operator = (const DPSDataBase& obj)
{
if (!IsEmpty())
Clear();
AddNode(obj.root->value);
Copy(obj.root);
return *this;
}
DPSDataBase(const DPSDataBase& obj)
{
root = nullptr;
AddNode(obj.root->value);
Copy(obj.root);
}
void Copy(Node* elem)
{
if (elem->left != nullptr)
AddNode(elem->left->value);
if (elem->right != nullptr)
AddNode(elem->right->value);
if (elem->left != nullptr)
Copy(elem->left);
if (elem->right != nullptr)
Copy(elem->right);
}
void DeleteNode(int value)
{
Node* d = FindNode(value);
if (d == nullptr)
return;
Node* parent = d->parent;
if (d == root && GetCount() == 1)
{
Clear();
return;
}
if (d->left == nullptr && d->right == nullptr)
{
if (parent->left == d)
parent->left = nullptr;
else
parent->right = nullptr;
delete d;
return;
}
if (d->left == nullptr && d->right != nullptr)
{
if (parent->left == d)
{
parent->left = d->right;
}
else
{
parent->right = d->right;
}
d->right->parent = parent;
delete d;
return;
}
if (d->left != nullptr && d->right == nullptr)
{
if (parent->left == d)
{
parent->left = d->left;
}
else
{
parent->right = d->left;
}
d->left->parent = parent;
delete d;
return;
}
if (d->left != nullptr && d->right != nullptr)
{
Node* r = d->right;
if (r->right == nullptr && r->left == nullptr)
{
d->value = r->value;
d->right = nullptr;
delete r;
}
else if (r->left != nullptr)
{
Node* p = r->left;
while (p->left != nullptr)
p = p->left;
d->value = p->value;
if (p->right == nullptr)
p->parent->left = nullptr;
else
p->parent->left = p->right;
delete p;
}
else
{
d->value = r->value;
d->right = r->right;
delete r;
}
}
} */
};