-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefOptimization.cpp
More file actions
334 lines (247 loc) · 5.92 KB
/
RefOptimization.cpp
File metadata and controls
334 lines (247 loc) · 5.92 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
// RefOptimization.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include "stack.h"
#include "stack.cpp"
#include "RBTree.h"
#include "BinaryTree.h"
#include "BinaryTree.cpp"
#include <stack>
#include <queue>
#include <deque>
#include <list>
using namespace std;
int g_constructorCount = 0;
int g_copyConstructCount = 0;
int g_desstructCount = 0;
struct A
{
A()
{
cout << "contrcutor: " << ++g_constructorCount << endl;
}
A(const A& a)
{
cout << "copy construct: " << ++g_copyConstructCount << endl;
}
~A()
{
cout << "" << ++g_desstructCount << endl;
}
};
A GetA()
{
return A();
}
int main()
{
//A a = GetA();
//A&& a = GetA();
//测试红黑树
#if 0
//
int a[] = { 10, 40, 30, 60, 90, 70, 20, 50, 80 };
int check_insert = 0; // "插入"动作的检测开关(0,关闭;1,打开)
int check_remove = 0; // "删除"动作的检测开关(0,关闭;1,打开)
int i;
int ilen = (sizeof(a)) / (sizeof(a[0]));
RBTree<int>* tree = new RBTree<int>();
cout << "== 原始数据: ";
for (i = 0; i < ilen; i++)
cout << a[i] << " ";
cout << endl;
for (i = 0; i < ilen; i++)
{
tree->insert(a[i]);
// 设置check_insert=1,测试"添加函数"
if (check_insert)
{
cout << "== 添加节点: " << a[i] << endl;
cout << "== 树的详细信息: " << endl;
tree->print();
cout << endl;
}
}
cout << "== 前序遍历: ";
tree->preOrder();
cout << "\n== 中序遍历: ";
tree->inOrder();
cout << "\n== 后序遍历: ";
tree->postOrder();
cout << endl;
cout << "== 最小值: " << tree->minimum() << endl;
cout << "== 最大值: " << tree->maximum() << endl;
cout << "== 树的详细信息: " << endl;
tree->print();
// 设置check_remove=1,测试"删除函数"
if (check_remove)
{
for (i = 0; i < ilen; i++)
{
tree->remove(a[i]);
cout << "== 删除节点: " << a[i] << endl;
cout << "== 树的详细信息: " << endl;
tree->print();
cout << endl;
}
}
// 销毁红黑树
tree->destroy();
#endif
//测试二叉树
#if 0
//二叉树的操作过程
int root = 10;
int my_array[] = {2,1,3,5,7,9,11,13,15,17};
BinaryTree<int> b_tree(root);
b_tree.Insert(my_array[0]);
b_tree.Insert(my_array[1]);
b_tree.Insert(my_array[2]);
b_tree.Insert(my_array[3]);
b_tree.Insert(my_array[4]);
b_tree.Insert(my_array[5]);
b_tree.Insert(my_array[6]);
b_tree.Insert(my_array[7]);
b_tree.Insert(my_array[8]);
b_tree.Insert(my_array[9]);
b_tree.PreOrder();
cout << "\n" << endl;
b_tree.InOrder();
cout << "\n" << endl;
b_tree.PostOrder();
cout << "\n After delete 17..." << endl;
b_tree.Remove(17);
b_tree.PreOrder();
cout << "\n" << endl;
b_tree.InOrder();
cout << "\n" << endl;
b_tree.PostOrder();
cout << "\n After delete 2... ..." << endl;
b_tree.Remove(2);
cout << "\n ... ..." << endl;
b_tree.PreOrder();
cout << "tree depth: ..." << endl;
int depth = b_tree.GetTreeDepth();
cout << depth << endl;
#endif
//STL标准模板库的测试
#if 0
//测试STL标准库中的栈(stack)(FILO)
cout << "-----------stack------------" << endl;
stack<double> p_stack_list;
for (auto i = 0;i < 10;i++)
{
p_stack_list.push((double)i);
}
for (auto m = 0;m < p_stack_list.size();m++)
{
cout << p_stack_list.top() << endl;
p_stack_list.pop();
m--;
if (p_stack_list.empty())
{
break;
}
}
//测试STL标准库中的队列(queue)
cout << "-------------queue------------" << endl;
queue<double> p_queue_list;
for (auto i = 0;i < 6;i++)
{
p_queue_list.push((double)i);
}
cout << "---------------front of queue------------" << endl;
for (auto m = 0; m < p_queue_list.size(); m++)
{
cout << p_queue_list.front() << endl;
p_queue_list.pop();
m--;
}
cout << "----------------back of queue----------------" << endl;
for (auto i = 0; i < 6; i++)
{
p_queue_list.emplace((double)i);
}
for (auto m = 0;m < p_queue_list.size();m++)
{
cout << p_queue_list.back() << endl;
}
for (auto m = 0; m < p_queue_list.size(); m++)
{
cout << p_queue_list.front() << endl;
p_queue_list.pop();
m--;
}
//测试STL标准库中的双端队列(dequeue)
cout << "----------dequeue-------------" << endl;
deque<double> p_deque_list;
for (auto m = 0;m < 6;m++)
{
p_deque_list.emplace_front((double)m);
}
for (auto i = 0;i < 6;i++)
{
cout << p_deque_list.at(i) << endl;
}
cout << "==========" << endl;
p_deque_list.assign(6,7);
for (auto iter = p_deque_list.begin();iter != p_deque_list.end();iter++)
{
cout << *iter << endl;
}
//测试STL标准模板库之LIST容器,这个容器不支持随机访问,只支持删除与插入,因为它是链表
cout << "----list----" << endl;
list<int> p_list;
p_list.emplace_front(1);
p_list.emplace_front(2);
p_list.emplace_front(3);
p_list.emplace_back(5);
p_list.emplace_back(6);
for (auto iter = p_list.begin();iter != p_list.end();iter++)
{
//3,2,1,5,6
cout << *iter << endl;
}
//从LIST中间删除一个元素
p_list.remove(5);
cout << "---" << endl;
for (auto iter = p_list.begin(); iter != p_list.end(); iter++)
{
//3,2,1,6
cout << *iter << endl;
}
//在LIST中间插入一个元素(指定位置需要用到迭代器,其实比较麻烦)
cout << "---" << endl;
auto iter = p_list.begin();
iter++;
p_list.insert(iter, 10);
for (auto iter = p_list.begin();iter != p_list.end();iter++)
{
cout << *iter << endl;
}
#endif
#if 1
//测试自己定义的数据结构--栈
CStack<double> my_stack;
my_stack.Push(1);
my_stack.Push(2);
my_stack.Push(3);
my_stack.Push(4);
//the top is 4
cout << my_stack.GetTopData() << endl;
//pop the top
my_stack.Pop();
//the top is 3
cout << my_stack.GetTopData() << endl;
my_stack.Pop();
//the top is 2
cout << my_stack.GetTopData() << endl;
my_stack.Pop();
//the top is 1
cout << my_stack.GetTopData() << endl;
my_stack.Pop();
#endif
Sleep(10000);
return 0;
}