1
1
import BinaryTreeNode from '../BinaryTreeNode' ;
2
+ import Comparator from '../../../utils/comparator/Comparator' ;
2
3
3
4
export default class BinarySearchTreeNode extends BinaryTreeNode {
5
+ /**
6
+ * @param {* } value
7
+ * @param {BinaryTreeNode } parent
8
+ * @param {function } compareFunction
9
+ */
10
+ constructor ( value = null , parent = null , compareFunction = undefined ) {
11
+ super ( value , parent ) ;
12
+
13
+ // This comparator is used to compare node values with each other.
14
+ this . nodeValueComparator = new Comparator ( compareFunction ) ;
15
+ }
16
+
17
+ /**
18
+ * @param {* } value
19
+ * @return {BinarySearchTreeNode }
20
+ */
4
21
insert ( value ) {
5
- if ( this . value === null ) {
22
+ if ( this . nodeValueComparator . equal ( this . value , null ) ) {
6
23
this . value = value ;
7
24
return this ;
8
25
}
9
26
10
- if ( value < this . value ) {
27
+ if ( this . nodeValueComparator . lessThan ( value , this . value ) ) {
11
28
// Insert to the left.
12
29
if ( this . left ) {
13
30
this . left . insert ( value ) ;
14
31
} else {
15
32
this . setLeft ( new BinarySearchTreeNode ( value ) ) ;
16
33
}
17
- } else if ( value > this . value ) {
34
+ } else if ( this . nodeValueComparator . greaterThan ( value , this . value ) ) {
18
35
// Insert to the right.
19
36
if ( this . right ) {
20
37
this . right . insert ( value ) ;
@@ -26,27 +43,38 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
26
43
return this ;
27
44
}
28
45
46
+ /**
47
+ * @param {* } value
48
+ * @return {BinarySearchTreeNode }
49
+ */
29
50
find ( value ) {
30
51
// Check the root.
31
- if ( this . value === value ) {
52
+ if ( this . nodeValueComparator . equal ( this . value , value ) ) {
32
53
return this ;
33
54
}
34
55
35
- if ( value < this . value && this . left ) {
56
+ if ( this . nodeValueComparator . lessThan ( value , this . value ) && this . left ) {
36
57
// Check left nodes.
37
58
return this . left . find ( value ) ;
38
- } else if ( value > this . value && this . right ) {
59
+ } else if ( this . nodeValueComparator . greaterThan ( value , this . value ) && this . right ) {
39
60
// Check right nodes.
40
61
return this . right . find ( value ) ;
41
62
}
42
63
43
64
return null ;
44
65
}
45
66
67
+ /**
68
+ * @param {* } value
69
+ * @return {boolean }
70
+ */
46
71
contains ( value ) {
47
72
return ! ! this . find ( value ) ;
48
73
}
49
74
75
+ /**
76
+ * @param {* } value
77
+ */
50
78
remove ( value ) {
51
79
const nodeToRemove = this . find ( value ) ;
52
80
@@ -65,7 +93,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
65
93
// Find the next biggest value (minimum value in the right branch)
66
94
// and replace current value node with that next biggest value.
67
95
const nextBiggerNode = nodeToRemove . right . findMin ( ) ;
68
- if ( nextBiggerNode !== nodeToRemove . right ) {
96
+ if ( ! this . nodeComparator . equal ( nextBiggerNode , nodeToRemove . right ) ) {
69
97
this . remove ( nextBiggerNode . value ) ;
70
98
nodeToRemove . value = nextBiggerNode . value ;
71
99
} else {
@@ -85,6 +113,9 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
85
113
}
86
114
}
87
115
116
+ /**
117
+ * @return {BinarySearchTreeNode }
118
+ */
88
119
findMin ( ) {
89
120
if ( ! this . left ) {
90
121
return this ;
0 commit comments