-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Search_Tree.js
191 lines (168 loc) · 4.05 KB
/
Binary_Search_Tree.js
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
/**
* js 实现二叉查找树
* 二叉查找树,也称二叉搜索树、有序二叉树、排序二叉树,是指一棵空树或者具有下列性质的二叉树:
* 1. 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
* 2. 若任意节点的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
* 3. 任意节点的左、右子树也分别为二叉查找树;
* 4. 没有键值相等的节点。
*/
/**
* 单个节点
*/
class Node {
constructor (val) {
this.val = val
this.left = this.right = null
}
}
class Tree {
constructor (val = null) {
this.root = null
if (val) {
this.root = new Node(val)
}
}
// 插入值
static _insert (val, node) {
if (val < node.val) {
if (node.left) {
this._insert(val, node.left)
} else {
node.left = new Node(val)
}
} else {
if (node.right) {
this._insert(val, node.right)
} else {
node.right = new Node(val)
}
}
}
insert (val) {
if (!this.root) {
this.root = new Node(val)
return
}
this.constructor._insert(val, this.root)
}
// 挂载的辅助函数
static _mountMinTree (tree, host) {
if (host.left) {
this._mountMinTree(tree, host.left)
} else {
host.left = tree
}
return host
}
static _minNode (node) {
if (!node) {
return null
}
while (node && node.left) {
node = node.left
}
return node
}
static _remove (val, node) {
if (!node) {
return null
}
if (val === node.val) { // 要删除的节点是当前节点
if (!node.right) { // 右子树不存在,直接用左子树替换当前节点流程结束
return node.left
} else if (!node.left) {
return node.right
} else { // 右子树存在
// return this._mountMinTree(node.left, node.right); // 取出左子树挂在到右子树的最小节点上,返回该右子树替换当前节点
const minNode = this._minNode(node.right)
node.val = minNode.val
node.right = this._remove(minNode.val, node.right)
return node
}
} else if (val < node.val) { // 要删除的节点在左子树
node.left = this._remove(val, node.left)
return node
} else { // 要删除的节点在右子树
node.right = this._remove(val, node.right)
return node
}
}
// 移除节点
remove (val) {
this.root = this.constructor._remove(val, this.root)
}
static _includes (val, node) {
if (!node) {
return false
}
if (val === node.val) {
return true
}
return this._includes(val, val < node.val ? node.left : node.right)
}
includes (val) {
return this.constructor._includes(val, this.root)
}
static depth (node) {
if (!node) {
return 0
}
return Math.max(this.depth(node.left), this.depth(node.right)) + 1
}
getMaxDepth () {
return this.constructor.depth(this.root)
}
bfs () {
if (!this.root) {
return []
}
const queue = [this.root]
const list = []
while (queue.length) {
const node = queue.shift()
list.push(node.val)
if (node.left) {
queue.push(node.left)
}
if (node.right) {
queue.push(node.right)
}
}
return list
}
dfsPre (node = this.root, list = []) {
if (node) {
list.push(node.val)
this.dfsPre(node.left, list)
this.dfsPre(node.right, list)
}
return list
}
dfsIn (node = this.root, list = []) {
if (node) {
this.dfsIn(node.left, list)
list.push(node.val)
this.dfsIn(node.right, list)
}
return list
}
dfsPost (node = this.root, list = []) {
if (node) {
this.dfsPost(node.left, list)
this.dfsPost(node.right, list)
list.push(node.val)
}
return list
}
}
const tree = new Tree()
tree.insert(8)
tree.insert(14)
tree.insert(13)
tree.insert(6)
tree.insert(3)
tree.insert(7)
tree.insert(11)
tree.remove(11)
tree.getMaxDepth()
console.log(tree)