-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathbinary-search-trees.js
More file actions
219 lines (186 loc) · 6.12 KB
/
binary-search-trees.js
File metadata and controls
219 lines (186 loc) · 6.12 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
// 已知传入的节点 A 具有左节点和右节点,返回节点值 A 最接近的 A 的子节点
const findClosestNode = (node) => {
const val = node.node;
const leftNode = node.leftChild.rightChild
? node.leftChild.rightChild
: node.leftChild;
const rightNode = node.rightChild.leftChild
? node.rightChild.leftChild
: node.rightChild;
return (rightNode.node - val) > (leftNode.node - val)
? leftNode
: rightNode;
};
class Node {
constructor(val, parent = null) {
this.val = val;
this.parent = parent;
this.leftNode = null;
this.rightNode = null;
}
get node() {
return this.val;
}
set node(val) {
this.val = val;
}
get parentNode() {
return this.parent;
}
set parentNode(parent) {
this.parent = parent;
}
// get left child node value
get left() {
return this.leftNode ? this.leftNode.node : null;
}
// get left child node
get leftChild() {
return this.leftNode;
}
// set left child node
set leftChild(leftNode) {
if (leftNode !== null && this.node < leftNode.node) {
throw new Error('Left node value should be smaller than root node value!');
}
this.leftNode = leftNode;
leftNode.parentNode = this;
}
// get right child node value
get right() {
return this.rightNode ? this.rightNode.node : null;
}
// get right child node
get rightChild() {
return this.rightNode;
}
// set right child node
set rightChild(rightNode) {
if (rightNode !== null && this.node > rightNode.node) {
throw new Error('Right node value should be bigger than root node value!');
}
this.rightNode = rightNode;
rightNode.parentNode = this;
}
insert(val) {
if (this.node < val) {
// 如果要插入的值大于当前节点值,则寻找其右子节点
if (this.right === null) {
this.rightChild = new Node(val, this);
} else {
this.rightChild.insert(val);
}
} else if (this.node > val) {
// 如果要插入的值小于当前节点值,则寻找其左子节点
if (this.left === null) {
this.leftChild = new Node(val, this);
} else {
this.leftChild.insert(val);
}
}
}
find(val) {
// 若 target === 根节点的值,则直接查找成功
if (this.node === val) return this;
if (val < this.node) {
// 若 target < 根节点的值,则搜索左子节点
if (this.left === null) return null;
return this.leftChild.find(val);
} else if (val > this.node) {
// 若 target > 根节点的值,则搜索右子节点
if (this.right === null) return null;
return this.rightChild.find(val);
}
}
// 返回该节点的子节点数目
get childNodeCount() {
return (this.left === null ? 0 : 1) + (this.right === null ? 0 : 1);
}
// 判断当前节点属于父节点的左子节点还是右子节点
get isLeftChild() {
return this.parentNode.leftChild === this;
}
get isRightChild() {
return this.parentNode.rightChild === this;
}
destory() {
this.node = null;
}
remove(val) {
let findedNode = this.find(val);
this.removeNode(findedNode);
}
removeNode(findedNode) {
if (!findedNode) return;
const childNodeCount = findedNode.childNodeCount;
if (childNodeCount === 2) {
// 如果有两个子节点,则找到最接近需要删除的节点的那个子节点
const substitute = findClosestNode(findedNode);
findedNode.node = substitute.node;
substitute.remove(substitute.node);
} else {
const parentNode = findedNode.parentNode;
const childKey = findedNode.isLeftChild
? 'leftChild'
: 'rightChild';
// 如果要移除的目标节点没有左右子节点,则直接删除
let childNode = null;
if (childNodeCount) {
// 如果只有一个子节点,则把仅有的那个子节点提升
childNode = findedNode.leftChild || findedNode.rightChild;
childNode.parentNode = parentNode;
}
parentNode[childKey] = childNode;
findedNode.destory();
findedNode = null;
}
}
}
/* ==================================================== */
console.log(' ========== 创建二叉搜索树并插入元素 ========== ');
// 初始化创建一个二叉搜索树,根元素为 10
const bst = new Node(10);
bst.insert(1);
bst.insert(20);
bst.insert(0.1);
bst.insert(2);
bst.insert(11);
bst.insert(9);
bst.insert(17);
bst.insert(5);
bst.insert(1.5);
bst.insert(9.5);
bst.insert(1.7);
console.log(`bst.left: ${bst.left}`); // 1
console.log(`bst.right: ${bst.right}`); // 20
console.log(`bst.leftChild.left: ${bst.leftChild.left}`); // 0.1
console.log(`bst.leftChild.right: ${bst.leftChild.right}`); // 2
console.log(`bst.rightChild.left: ${bst.rightChild.left}`); // 11
console.log(`bst.rightChild.right: ${bst.rightChild.right}`); // null
/* ==================================================== */
console.log('\n ========== 从二叉搜索树中查找元素 ========== ');
console.log('Find node which value is 9:');
let findedNode = bst.find(9);
console.log(`findedNode value: ${findedNode.node}`); // 9
console.log(`findedNode.left: ${findedNode.left}`); // 5
console.log(`findedNode.right: ${findedNode.right}`); // 9.5
console.log('\nFind node which value is 2:');
findedNode = bst.find(2);
console.log(`findedNode value: ${findedNode.node}`); // 2
console.log(`findedNode.left: ${findedNode.left}`); // 1.5
console.log(`findedNode.right: ${findedNode.right}`); // 9
/* ==================================================== */
console.log('\n ========== 从二叉搜索树中删除元素 ========== ');
console.log('删除只有一个子节点的元素:20');
bst.remove(20);
console.log(`bst.left: ${bst.left}`); // 1
console.log(`bst.right: ${bst.right}`); // 11
console.log('删除有两个子节点的元素:2');
bst.remove(2);
console.log(`bst.left: ${bst.left}`); // 1
console.log(`bst.right: ${bst.right}`); // 11
console.log(`bst.leftChild.left: ${bst.leftChild.left}`); // 0.1
console.log(`bst.leftChild.right: ${bst.leftChild.right}`); // 1.7
console.log('删除有一个子节点的元素:1');
bst.remove(1);
console.log(`bst.left: ${bst.left}`); // 0.1