-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.ts
230 lines (225 loc) · 6.85 KB
/
BinarySearchTree.ts
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
{
// 节点类
class Node {
data: any;
left: Node | null = null;
right: Node | null = null;
constructor(value: any) {
this.data = value;
}
}
// 二叉查找、排序树
class BinaryTree {
root: Node | null = null;
// 插入,关键点在于找到要插入的节点位置
insert(value: any, node: Node | null = this.root) {
if (!node) {
this.root = new Node(value);
} else {
if (node.data === value) {
console.log(value + '该值已存在!');
return;
}
if (node.data > value) {
if (!node.left) {
node.left = new Node(value);
} else {
this.insert(value, node.left)
}
} else {
if (!node.right) {
node.right = new Node(value);
} else {
this.insert(value, node.right)
}
}
}
}
// 递归先序遍历
preOrderRecursion(node: Node | null = this.root) {
if (node != null) {
console.log(node.data);
this.preOrderRecursion(node.left);
this.preOrderRecursion(node.right);
}
}
// 递归中序遍历
inOrderRecursion(node: Node | null = this.root) {
if (node != null) {
this.inOrderRecursion(node.left);
console.log(node.data);
this.inOrderRecursion(node.right);
}
}
// 递归后序遍历
postOrderRecursion(node: Node | null = this.root) {
if (node != null) {
this.postOrderRecursion(node.left);
this.postOrderRecursion(node.right);
console.log(node.data);
}
}
// 利用栈中序遍历,其实中序遍历后即是排序过的
inOrderStack() {
let node = this.root;
let stack: Array<Node> = [];
while (node || stack.length > 0) {
while (node) {
stack.push(node);
node = node.left;
}
if (stack.length > 0) {
node = stack.pop()!;
console.log(node.data);
node = node.right;
}
}
}
// 利用栈先序遍历
preOrderStack() {
let node = this.root;
let stack: Array<Node> = [];
while (node || stack.length > 0) {
while (node) {
stack.push(node);
console.log(node.data);
node = node.left;
}
if (stack.length > 0) {
node = stack.pop()!;
node = node.right;
}
}
}
// 利用栈后序遍历
postOrderStack() {
let node = this.root;
let stackA: Array<Node> = [];
let stackB: Array<Node> = [];
if (node) {
stackA.push(node);
while (stackA.length > 0) {
node = stackA.pop()!;
stackB.push(node);
if (node.left) {
stackA.push(node.left);
}
if (node.right) {
stackA.push(node.right);
}
}
while (stackB.length > 0) {
console.log(stackB.pop()?.data);
}
} else {
console.log('树为空!');
}
}
// 层序遍历
levelOrder() {
let node = this.root;
let queue: Array<Node> = [];
if (node) {
queue.push(node);
while (queue.length > 0) {
node = queue.shift()!;
console.log(node.data);
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
}
} else {
console.log('树为空!');
}
}
/**
* 查询节点
* @param value 要查找的值
*/
findNode(value: any) {
let node = this.root;
if (!node) {
console.log('空二叉树!');
return;
}
while (node) {
if (value < node.data) {
node = node.left;
} else if (value > node.data) {
node = node.right;
} else {
return node;
}
}
return null;
}
// 获取二叉树节点最大值
findMax() {
let node = this.root;
if (!node) {
console.log('空二叉树!');
return;
}
while (node) {
if (!node.right) {
return node.data;
} else {
node = node.right;
}
}
}
// 获取二叉树节点最小值
findMin() {
let node = this.root;
if (!node) {
console.log('空二叉树!');
return;
}
while (node) {
if (!node.left) {
return node.data;
} else {
node = node.left;
}
}
}
// 删除节点
delete() {
}
}
var test = new BinaryTree();
test.insert(10);
test.insert(6);
test.insert(16);
test.insert(5);
test.insert(7);
test.insert(12);
test.insert(18);
test.insert(4);
test.insert(8);
test.insert(11);
test.insert(13);
test.insert(17);
test.insert(19);
console.log(test.findMax());
console.log(test.findMin());
// console.log(test.findNode(11));
// console.log(test.findNode(20));
// console.log('递归先序遍历----------');
// test.preOrderRecursion();
// console.log('递归中序遍历----------');
// test.inOrderRecursion();
// console.log('递归后序遍历----------');
// test.postOrderRecursion();
// console.log('利用栈先序遍历---------');
// test.preOrderStack();
// console.log('利用栈中序遍历---------');
// test.inOrderStack();
// console.log('利用栈后序遍历---------');
// test.postOrderStack();
// console.log('层序遍历---------');
// test.levelOrder();
}