-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.js
183 lines (170 loc) · 3.74 KB
/
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
class Node {
constructor (key, value) {
this.key = key
this.value = value
this.left = null
this.right = null
}
}
class Tree {
constructor () {
this.root = null
this.size = 0
}
insert (key, value) {
const newNode = new Node(key, value)
if (this.size === 0) { // 没有根节点
this.root = newNode
this.size += 1
} else {
let node = this.root
let parent
while (node) {
if (node.key === key) {
node.value = value
return
} else {
parent = node
if (key > node.key) {
node = node.right
} else if (key < node.key) {
node = node.left
}
}
}
if (key > parent.key) {
parent.right = newNode
} else {
parent.left = newNode
}
this.size += 1
}
}
get (key) {
if (!this.root) return -1
let current = this.root
while (current.key !== key) {
if (key > current.key) {
current = current.right
} else {
current = current.left
}
if (current === null) {
return -1
}
}
return current
}
binarySearch (cb) {
if (!this.root) return -1
this.binarySearchNode(this.root, cb)
}
binarySearchNode (node, cb) {
cb(node)
node.left && this.binarySearchNode(node.left, cb)
node.right && this.binarySearchNode(node.right, cb)
}
remove (key) {
if (!this.root) return -1
// 先找到要删除的节点
let current = this.root
let parent = null
let isLeft = true
while (current.key !== key) {
parent = current
if (key > current.key) {
isLeft = false
current = current.right
} else if (key < current.key) {
isLeft = true
current = current.left
}
if (current === null) {
return -1
}
}
if (current.left === null && current.right === null) {
// 1. 叶子节点
if (parent === null) {
this.root = null
} else {
if (isLeft) {
parent.left = null
} else {
parent.right = null
}
}
} else if (current.left !== null && current.right !== null) {
// 有两个叶子节点
const successor = this.getSuccessor(current) // 找到后继节点
if (isLeft) {
parent.left = successor
} else {
parent.right = successor
}
successor.left = current.left
} else {
// 只有一个叶子节点
if (parent === null) {
if (isLeft) {
this.root = current.left
} else {
this.root = current.right
}
} else {
if (current.left) {
if (isLeft) {
parent.left = current.left
} else {
parent.right = current.left
}
} else {
if (isLeft) {
parent.left = current.right
} else {
parent.right = current.right
}
}
}
}
}
getSuccessor (delNode) {
let successor = delNode.right
let successorParent = delNode.right
while (successor.left !== null) {
successorParent = successor
successor = successor.left
}
if (delNode.right !== successor) {
successorParent.left = successor.right
successor.right = delNode.right
}
return successor
}
}
const t = new Tree()
function print () {
let str = ''
t.binarySearch((node) => {
str += String(node.key) + '-'
})
console.log(str)
}
t.insert(11, 11)
t.insert(9, 9)
t.insert(30, 30)
t.insert(8, 8)
t.insert(10, 10)
t.insert(5, 5)
t.insert(25, 25)
t.insert(22, 22)
t.insert(26, 26)
t.insert(34, 34)
t.insert(31, 31)
t.insert(33, 33)
t.insert(35, 35)
print()
t.remove(10)
print()
// t.delNode(6)
// console.log(t)