Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 92f6462

Browse files
blocks refactor
1 parent d0c7834 commit 92f6462

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

‎data-structures/binary-search-tree.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ BST.prototype.insert = function (value) {
1818

1919
while (x !== null) {
2020
current = x
21-
if (node.key < x.key) x = x.left
22-
else x = x.right
21+
if (node.key < x.key)
22+
x = x.left
23+
else
24+
x = x.right
2325
}
2426

25-
if (current === null) this.root = node
26-
else if (node.key < current.key) current.left = node
27-
else current.right = node
27+
if (current === null)
28+
this.root = node
29+
else if (node.key < current.key)
30+
current.left = node
31+
else
32+
current.right = node
33+
2834
node.parent = current
2935
}
3036

@@ -64,39 +70,49 @@ BST.prototype.transplant = function (u, v) {
6470
BST.prototype.search = function (value) {
6571
let current = this.root
6672
while (current) {
67-
if (value === current.key) return current
68-
else if (value < current.key) current = current.left
69-
else current = current.right
73+
if (value === current.key)
74+
return current
75+
else if (value < current.key)
76+
current = current.left
77+
else
78+
current = current.right
7079
}
7180
return null
7281
}
7382

7483
BST.prototype.min = function (node) {
75-
while (node.left) node = node.left
84+
while (node.left)
85+
node = node.left
86+
7687
return node
7788
}
7889

7990
BST.prototype.max = function (node) {
80-
while (node.right) node = node.right
91+
while (node.right)
92+
node = node.right
93+
8194
return node
8295
}
8396

8497
BST.prototype.sucessor = function(value) {
8598
let current = this.search(value)
86-
if (current.right){
99+
if (current.right)
87100
return this.min(current.right)
88-
}
101+
89102
while (current.parent && current === current.parent.right)
90103
current = current.parent
104+
91105
return current.parent
92106
}
93107

94108
BST.prototype.predecessor = function(value) {
95109
let current = this.search(value)
96110
if (current.left)
97111
return this.max(current.left)
112+
98113
while (current.parent && current === current.parent.left)
99114
current = current.parent
115+
100116
return current.parent
101117
}
102118

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /