-
Notifications
You must be signed in to change notification settings - Fork 21
/
Tmain.cpp
384 lines (319 loc) · 12.4 KB
/
Tmain.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
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
//
// Created by light on 19-10-18.
//
#include <algorithm>
#include "../rbtree/rb_tree.h"
#include "../hash/hash.h"
#include "../avl/AVL.h"
#include "../file/FileOps.h"
#include "../set_map/BSTSet.h"
#include "../set_map/BSTMap.h"
#include "../set_map/LinkedListSet.h"
#include "../set_map/LinkedListMap.h"
#include "../set_map/SeqMap.h"
#include "../set_map/SeqSet.h"
#include "../queue/ArrayQueue.h"
#include "../queue/loopQueue.h"
#include "../queue/LinkedListQueue.h"
#include "../stack/ArrayStack.h"
#include "../stack/LinkedListStack.h"
#include "../linklist/LinkedList.h"
using namespace std;
template<typename T>
double testMap(T *map, string filename) {
clock_t startTime = clock();
srand(66);
vector<string> words;
if (FileOps::readFile(filename, words)) {
std::cout << "Total words: " << words.size() << std::endl;
for (string word : words) {
if (map->contain(word)) {
map->set(word, *(map->search(word)) + 1);
} else {
map->insert(word, 1);
}
}
if (map->contain("labour"))
cout << "'labour' : " << *(map->search("labour")) << endl;
else
cout << "No word 'labour' in " + filename << endl;
map->remove("labour"); // remove返回值为树根
if (map->contain("labour"))
cout << "'labour' : " << *(map->search("labour")) << endl;
else
cout << "No word 'labour' in " + filename << endl;
}
clock_t endTime = clock();
return double(endTime - startTime) / CLOCKS_PER_SEC;
}
template<typename T>
double testSet(T *set, string filename) {
clock_t startTime = clock();
srand(66);
vector<string> words;
if (FileOps::readFile(filename, words)) {
std::cout << "Total words: " << words.size() << std::endl;
for (string word : words) {
set->insert(word);
}
std::cout << "Total different words: " << set->size() << std::endl;
}
clock_t endTime = clock();
return double(endTime - startTime) / CLOCKS_PER_SEC;
}
template<typename T>
double testQueue(BaseQueue<T> *q, int opCount) {
clock_t startTime = clock();
for (int i = 0; i < opCount; i++) {
q->enqueue(i);
}
for (int i = 0; i < opCount; i++) {
q->dequeue();
}
clock_t endTime = clock();
return double(endTime - startTime) / CLOCKS_PER_SEC;
}
template<typename T>
double testStack(CommonStack<T> *stack, int opCount) {
clock_t startTime = clock();
for (int i = 0; i < opCount; i++) {
stack->push(i);
}
for (int i = 0; i < opCount; i++) {
stack->pop();
}
clock_t endTime = clock();
return double(endTime - startTime) / CLOCKS_PER_SEC;
}
/**
* 对于完全随机的数据,普通的二分搜索树很好用!
* 缺点:极端情况退化成链表(或高度不平衡)
* 对于查询较多的使用情况,AVL树很好用
* 红黑树牺牲了平衡性(2longn的高度)
* 统计性能更优(综合增删改查所有的操作)
* @return
*/
int main() {
// 我们使用文本量更小的共产主义宣言进行试验:)
string filename = "/home/light/CLionProjects/alg/算法基础/file/communist.txt";
vector<string> words;
if (FileOps::readFile(filename, words)) {
cout << "There are totally " << words.size() << " words in " << filename << endl;
cout << endl;
// 测试1, 我们按照文本原有顺序插入进二分搜索树
time_t startTime = clock();
BST<string, int> *bst = new BST<string, int>();
for (vector<string>::iterator iter = words.begin(); iter != words.end(); iter++) {
int *res = (*bst).search(*iter);
if (res == NULL)
(*bst).insert(*iter, 1);
else
(*res)++;
}
vector<pair<string, int>> res1 = bst->_postOrder();
cout << endl;
vector<pair<string, int>> res2 = bst->postOrder();
if (res1 != res2) cout << "not ok" << endl;
else
cout << "ok" << endl;
// 我们查看labour一词的词频
if (bst->contain("labour"))
cout << "'labour' : " << *(*bst).search("labour") << endl;
else
cout << "No word 'labour' in " + filename << endl;
time_t endTime = clock();
cout << "BST , time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " s." << endl;
cout << endl;
delete bst;
// 测试2, 我们按照文本原有顺序插入顺序查找表
startTime = clock();
SequenceST<string, int> *sst = new SequenceST<string, int>();
for (vector<string>::iterator iter = words.begin(); iter != words.end(); iter++) {
int *res = (*sst).search(*iter);
if (res == NULL)
(*sst).insert(*iter, 1);
else
(*res)++;
}
if (sst->contain("labour"))
cout << "'labour' : " << *(*sst).search("labour") << endl;
else
cout << "No word 'labour' in " + filename << endl;
int *value = sst->remove("labour");
cout << *value << endl;
// 我们查看labour一词的词频
if (sst->contain("labour"))
cout << "'labour' : " << *(*sst).search("labour") << endl;
else
cout << "No word 'labour' in " + filename << endl;
endTime = clock();
cout << "SST , time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " s." << endl;
cout << endl;
delete sst;
// 测试3, 我们将原文本排序后插入二分搜索树, 查看其效率
startTime = clock();
BST<string, int> *bst2 = new BST<string, int>();
sort(words.begin(), words.end());
for (vector<string>::iterator iter = words.begin(); iter != words.end(); iter++) {
int *res = (*bst2).search(*iter);
if (res == NULL)
(*bst2).insert(*iter, 1);
else
(*res)++;
}
// 我们查看labour一词的词频
cout << "'labour' : " << *(*bst2).search("labour") << endl;
endTime = clock();
cout << "BST2 , time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " s." << endl;
cout << endl;
delete bst2;
// 测试1, 我们按照文本原有顺序插入进二分搜索树
startTime = clock();
RBTree<string, int> *rbTree = new RBTree<string, int>();
for (vector<string>::iterator iter = words.begin(); iter != words.end(); iter++) {
int *res = (*rbTree).search(*iter);
if (res == NULL)
(*rbTree).insert(*iter, 1);
else
(*res)++;
}
// 我们查看labour一词的词频
if (rbTree->contain("labour"))
cout << "'labour' : " << *(rbTree->search("labour")) << endl;
else
cout << "No word 'labour' in " + filename << endl;
//
// rbTree->insert(100, "v100");
// rbTree->insert(50, "v50");
// rbTree->insert(150, "v150");
// rbTree->insert(20, "v20");
// rbTree->insert(85, "v85");
// rbTree->insert(10, "v10");
// rbTree->insert(15, "a15");
// rbTree->insert(75, "v75");
// rbTree->insert(95, "v95");
// rbTree->insert(65, "v65");
// rbTree->insert(76, "v76");
// rbTree->insert(60, "v60");
// rbTree->insert(66, "v66");
// rbTree->insert(61, "v61");
// rbTree->levelOrder();
endTime = clock();
cout << "RBTree, time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " s." << endl;
cout << endl;
delete rbTree;
startTime = clock();
HashTable<string, int> *ht = new HashTable<string, int>(words.size());
for (string word : words) {
if (ht->contains(word)) {
ht->set(word, ht->get(word) + 1);
} else {
ht->add(word, 1);
}
}
if (ht->contains("labour"))
cout << "'labour' : " << ht->get("labour") << endl;
else
cout << "No word 'labour' in " + filename << endl;
// int ret = ht->remove("labour");
// cout << ret << endl;
// cout << ht->getSize() << endl;
// if (ht->contains("labour"))
// cout << "'labour' : " << ht->get("labour") << endl;
// else
// cout << "No word 'labour' in " + filename << endl;
endTime = clock();
cout << "HashTable, time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " s." << endl;
delete ht;
cout << endl;
startTime = clock();
AVL<string, int> *avlTree = new AVL<string, int>();
for (string word : words) {
if (avlTree->contain(word)) {
avlTree->set(word, *(avlTree->search(word)) + 1);
} else {
avlTree->insert(word, 1);
}
}
if (avlTree->contain("labour"))
cout << "'labour' : " << *(avlTree->search("labour")) << endl;
else
cout << "No word 'labour' in " + filename << endl;
avlTree->remove("labour");
if (avlTree->contain("labour"))
cout << "'labour' : " << *(avlTree->search("labour")) << endl;
else
cout << "No word 'labour' in " + filename << endl;
std::cout << avlTree->isBST() << endl;
std::cout << avlTree->isBalanced() << endl;
endTime = clock();
cout << "avlTree, time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " s." << endl;
delete avlTree;
cout << endl;
LinkedListMap<string, int> *linkedListMap = new LinkedListMap<string, int>();
float linkMapTime = testMap(linkedListMap, filename);
std::cout << "linkMap time : " << linkMapTime << " s " << std::endl;
delete linkedListMap;
cout << endl;
BSTMap<string, int> *bstMap = new BSTMap<string, int>();
float bstMapTime = testMap(bstMap, filename);
std::cout << "bstMap time : " << bstMapTime << " s " << std::endl;
delete bstMap;
cout << endl;
SeqMap<string, int> *seqMap = new SeqMap<string, int>();
float seqMapTime = testMap(seqMap, filename);
std::cout << "seqMap time : " << seqMapTime << " s " << std::endl;
delete seqMap;
cout << endl;
AVLMap<string, int> *avlMap = new AVLMap<string, int>();
float avlMapTime = testMap(avlMap, filename);
std::cout << "AVLMap time : " << avlMapTime << " s " << std::endl;
delete avlMap;
cout << endl;
BSTSet<string> *bstSet = new BSTSet<string>();
double time1 = testSet(bstSet, filename);
std::cout << "BstSet time : " << time1 << " s " << std::endl;
delete bstSet;
cout << endl;
LinkedListSet<string> *linkedListSet = new LinkedListSet<string>();
double time2 = testSet(linkedListSet, filename);
std::cout << "LinkedListSet time : " << time2 << " s " << std::endl;
delete linkedListSet;
cout << endl;
SeqSet<string> *seqSet = new SeqSet<string>();
double time3 = testSet(seqSet, filename);
std::cout << "SeqSet time : " << time3 << " s " << std::endl;
delete seqSet;
cout << endl;
AVLSet<string> *avlSet = new AVLSet<string>();
double time4 = testSet(avlSet, filename);
std::cout << "AVLSet time : " << time4 << " s " << std::endl;
delete avlSet;
cout << endl;
BaseQueue<int> *queue = new ArrayQueue<int>();
BaseQueue<int> *lq = new loopQueue<int>();
BaseQueue<int> *linkq = new LinkedListQueue<int>();
int opCount = 100000;
cout << "queue time " << endl;
cout << testQueue(queue, opCount) << endl;
cout << "loopQueue time " << endl;
cout << testQueue(lq, opCount) << endl;
cout << "LinkedListQueue time " << endl;
cout << testQueue(linkq, opCount) << endl;
delete queue;
delete lq;
delete linkq;
cout << endl;
CommonStack<int> *stack = new ArrayStack<int>();
LinkedListStack<int> *linkedListStack = new LinkedListStack<int>();
cout << "array stack time " << endl;
cout << testStack(stack, opCount) << endl;
cout << "linkedList stack time " << endl;
cout << testStack(linkedListStack, opCount) << endl;
delete stack;
delete linkedListStack;
}
vector<int> a = {1, 23};
vector<int> b = {2, 23};
return 0;
}