-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tree.cs
315 lines (299 loc) · 10.2 KB
/
Tree.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 快排
{
class Tree
{
private int _data;
private Tree _leftChild;
private Tree _rightChild;
public Tree(int value)
{
this._data = value;
this._leftChild = null;
this._rightChild = null;
}
public int data
{
get { return this._data; }
set { this._data = value; }
}
public Tree leftChild
{
get { return this._leftChild; }
set { this._leftChild = value; }
}
public Tree rightChild
{
get { return this._rightChild; }
set { this._rightChild = value; }
}
}
class text
{
private static int dataCount = 100; //数据量
static Tree comTree = new Tree(0); //完全二叉树的根节点
static Tree sortTree = new Tree(0); //排序二叉树的根节点
static List<Tree> comList = new List<Tree>();
static int[] ranData = new int[dataCount];
static int comLeafCount = 0;
static int sortLeafCount = 0;
static void Main(string[] args)
{
Random ran = new Random();
for(int i=0;i<dataCount;i++)
{
ranData[i] = ran.Next(1, 200);
}
do
{
switch (menu())
{
case 1:
{
comTree = new Tree(0);
buildComTree();
Console.WriteLine("完全二叉树构建完成!按任意键继续...\n");
Console.ReadKey();
break;
}
case 2:
{
sortTree = new Tree(0);
buildSortTree();
Console.WriteLine("排序二叉树构建完成!按任意键继续...\n");
Console.ReadKey();
break;
}
case 3:
{
if(sortTree.data==0)
{
Console.WriteLine("排序二叉树尚未被构建!按任意键继续...\n");
}
else
{
InOrder(sortTree);
Console.WriteLine();
Console.WriteLine("\n中序序列输出完毕!按任意键继续...\n");
}
Console.ReadKey();
break;
}
case 4:
{
if (sortTree.data != 0)
{
sortLeafCount = 0;
countSortLeaf(sortTree);
Console.WriteLine("leafCount:" + sortLeafCount + "\n");
}
if ( comTree.data != 0 )
{
comLeafCount = 0;
countComLeaf(comTree);
Console.WriteLine("leafCount:" + comLeafCount + "\n");
}
Console.WriteLine("输出完毕!按任意键继续...\n");
Console.ReadKey();
break;
}
case 5:
{
if (sortTree.data != 0)
{
int leafDepth = 0;
leafDepth = depth(sortTree);
Console.WriteLine("sortTreeDepth:" + leafDepth + "\n");
}
if (comTree.data != 0)
{
int leafDepth = 0;
leafDepth = depth(comTree);
Console.WriteLine("comTreeDepth:" + leafDepth + "\n");
}
Console.WriteLine("输出完毕!按任意键继续...\n");
Console.ReadKey();
break;
}
default: return;
}
} while (true);
}
/// <summary>
/// 显示菜单,提示用户输入,处理输入并作为返回值
/// </summary>
static int menu()
{
int selected;
Console.WriteLine("\n\n 菜单\n\n");
Console.WriteLine("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n\n");
Console.WriteLine("1 .生成完全二叉树\n");
Console.WriteLine("2 .生成二叉排序树\n");
Console.WriteLine("3 .输出中序遍历序列\n");
Console.WriteLine("4 .计算叶子结点数\n");
Console.WriteLine("5 .计算二叉树深度\n");
Console.WriteLine("6 .退出程序\n");
Console.WriteLine("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n\n\n");
do
{
try
{
selected = int.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("请输入正确的选项!\n");
continue;
}
if(selected>=1 && selected<=7)
{
return selected;
}
else
{
Console.WriteLine("请输入正确的选项!\n");
}
} while (true);
}
/// <summary>
/// 创建完全二叉树
/// </summary>
static void buildComTree()
{
comList.Clear();
comTree.data = ranData[0];
comList.Add(comTree);
for(int i=1;i<dataCount;i++)
{
Tree comTree2 = new Tree(ranData[i]);
comList.Add(comTree2);
if(i%2==0)
{
comList[(i-1)/2].rightChild = comTree2;
}
else
{
comList[i/2].leftChild = comTree2;
}
}
}
/// <summary>
/// 创建排序二叉树
/// </summary>
static void buildSortTree()
{
sortTree.data = ranData[0];
for(int i=1;i<dataCount;i++)
{
Tree newSort = new Tree(ranData[i]);
Tree temp = sortTree;
while(true)
{
if(temp.data<=newSort.data)
{
if (temp.rightChild == null)
{
temp.rightChild = newSort;
break;
}
else
{
temp = temp.rightChild;
}
}
else
{
if (temp.leftChild == null)
{
temp.leftChild = newSort;
break;
}
else
{
temp = temp.leftChild;
}
}
}
}
}
/// <summary>
/// 计算完全二叉树的叶子数
/// </summary>
static void countComLeaf(Tree root)
{
if (root != null)
{
if (root.leftChild == null && root.rightChild == null)
{
comLeafCount++;
}
else
{
if (root.leftChild != null)
{
countComLeaf(root.leftChild);
}
if (root.rightChild != null)
{
countComLeaf(root.rightChild);
}
}
}
}
/// <summary>
/// 计算排序二叉树的叶子数
/// </summary>
static void countSortLeaf(Tree root)
{
if (root != null)
{
if (root.leftChild == null && root.rightChild == null)
{
sortLeafCount++;
}
else
{
if (root.leftChild != null)
{
countSortLeaf(root.leftChild);
}
if (root.rightChild != null)
{
countSortLeaf(root.rightChild);
}
}
}
}
/// <summary>
/// 计算二叉树的深度,并作为返回值
/// </summary>
static int depth(Tree root)
{
int treeDepth = 0;
if(root!=null)
{
int depthLeft = depth(root.leftChild);
int depthRight = depth(root.rightChild);
treeDepth = 1 + (depthLeft >= depthRight ? depthLeft : depthRight);
}
return treeDepth;
}
/// <summary>
/// 用以输出中序序列
/// </summary>
/// <param name="root"></param>
static void InOrder(Tree root)
{
if (root != null)
{
InOrder(root.leftChild);
Console.Write(root.data+" ");
InOrder(root.rightChild);
}
}
}
}