-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
196 lines (177 loc) · 6.53 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace binaryTree
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>() { 50, 30, 70, 10, 40, 90, 80 };
//创建二叉树
BSTree bsTree = CreateBST(list);
Console.Write("中序遍历的原始数据");
//中序遍历
InOrderR_BST(bsTree);
Console.WriteLine("\n------------------------------------------------------------n");
//查找一个节点
Console.WriteLine("\n10在二叉树中是否包含:" + SearchBST(bsTree, 20));
Console.WriteLine("\n------------------------------------------------------------n");
bool isExcute = false;
//插入一个节点
InsertBST(bsTree, 20, ref isExcute);
Console.WriteLine("\n20插入到二叉树,中序遍历后:");
//中序遍历
InOrderR_BST(bsTree);
Console.WriteLine("\n------------------------------------------------------------n");
Console.Write("删除叶子节点20,\n中序遍历后:");
//删除一个节点
DeleteBST(ref bsTree, 20);
InOrderR_BST(bsTree);
Console.WriteLine("\n------------------------------------------------------------n");
Console.WriteLine("删除单孩子节点90,\n中序遍历后:");
//删除单孩子节点
DeleteBST(ref bsTree, 90);
InOrderR_BST(bsTree);
Console.WriteLine("\n------------------------------------------------------------n");
Console.WriteLine("删除根节点50,\n中序遍历后");
//删除根节点
DeleteBST(ref bsTree, 50);
InOrderR_BST(bsTree);
}
//定义一个二叉树结构
public class BSTree
{
public int data;
public BSTree left;
public BSTree right;
}
//二叉树的插入操做
//ref定义一个引用传递,与C++中引用传递类似,和实际值使用同一个地址,对它的修改就是对实际值得修改
static void InsertBST(BSTree bsTree, int key, ref bool isExcute)
{
if (bsTree == null)
return;
//如果父节点大于key,则遍历左子树
if(bsTree.data > key)
InsertBST(bsTree.left, key, ref isExcute);
else
InsertBST(bsTree.right, key, ref isExcute);
if (!isExcute)
{
//构建当前节点
BSTree current = new BSTree();
{
current.data = key;
current.left = null;
current.right = null;
};
if (bsTree.data > key)
bsTree.left = current;
else
bsTree.right = current;
isExcute = true;
}
}
//创建二叉树
static BSTree CreateBST(List<int> list)
{
//构建根节点
BSTree bsTree = new BSTree();
{
bsTree.data = list[0];
bsTree.left = null;
bsTree.right = null;
};
for (int i = 1; i < list.Count; i++)
{
bool isExcute = false;
InsertBST(bsTree, list[i], ref isExcute);
}
return bsTree;
}
//在排序二叉树中搜索指定节点
static bool SearchBST(BSTree bsTree, int key)
{
if (bsTree == null)
return false;
if (bsTree.data == key)
return true;
if(bsTree.data > key)
return SearchBST(bsTree.left, key);
else
return SearchBST(bsTree.right, key);
}
//中序排列二叉树
static void InOrderR_BST(BSTree bsTree)
{
if (bsTree != null)
{
//遍历左子树
InOrderR_BST(bsTree.left);
//输出节点数据
Console.WriteLine(bsTree.data + "");
//遍历右子书
InOrderR_BST(bsTree.right);
}
}
//删除二叉排序树中指定节点
static void DeleteBST(ref BSTree bsTree, int key)
{
if (bsTree == null)
return;
if (bsTree.data == key)
{
//第一种情况:叶子节点
if (bsTree.left == null && bsTree.right == null)
{
bsTree = null;
return;
}
//第二种情况:左子树不为空
if (bsTree.left != null && bsTree.right == null)
{
bsTree = bsTree.left;
return;
}
//第三种情况:右子树不为空
if (bsTree.left == null && bsTree.right == null)
{
bsTree = bsTree.right;
return;
}
//第四种情况:左右子树都不为空
if (bsTree.left != null && bsTree.right != null)
{
var node = bsTree.right; //
//找到右子树中的最左节点
while (node.left != null)
{
//遍历左子树
node = node.left;
}
//交换左右孩子
node.left = bsTree.left;
//判断是真正的叶子节点还是空左孩子的父节点
if (node.right == null)
{
//删掉右子树最左节点
DeleteBST(ref bsTree, node.data);
node.right = bsTree.right;
}
bsTree = node;
}
}
if (bsTree.data > key)
{
DeleteBST(ref bsTree.left, key);
}
else
{
DeleteBST(ref bsTree.right, key);
}
}
}
}