11import BinaryTreeNode from '../BinaryTreeNode' ;
2+ import Comparator from '../../../utils/comparator/Comparator' ;
23
34export 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+ */
421 insert ( value ) {
5- if ( this . value === null ) {
22+ if ( this . nodeValueComparator . equal ( this . value , null ) ) {
623 this . value = value ;
724 return this ;
825 }
926
10- if ( value < this . value ) {
27+ if ( this . nodeValueComparator . lessThan ( value , this . value ) ) {
1128 // Insert to the left.
1229 if ( this . left ) {
1330 this . left . insert ( value ) ;
1431 } else {
1532 this . setLeft ( new BinarySearchTreeNode ( value ) ) ;
1633 }
17- } else if ( value > this . value ) {
34+ } else if ( this . nodeValueComparator . greaterThan ( value , this . value ) ) {
1835 // Insert to the right.
1936 if ( this . right ) {
2037 this . right . insert ( value ) ;
@@ -26,27 +43,38 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
2643 return this ;
2744 }
2845
46+ /**
47+ * @param {* } value
48+ * @return {BinarySearchTreeNode }
49+ */
2950 find ( value ) {
3051 // Check the root.
31- if ( this . value === value ) {
52+ if ( this . nodeValueComparator . equal ( this . value , value ) ) {
3253 return this ;
3354 }
3455
35- if ( value < this . value && this . left ) {
56+ if ( this . nodeValueComparator . lessThan ( value , this . value ) && this . left ) {
3657 // Check left nodes.
3758 return this . left . find ( value ) ;
38- } else if ( value > this . value && this . right ) {
59+ } else if ( this . nodeValueComparator . greaterThan ( value , this . value ) && this . right ) {
3960 // Check right nodes.
4061 return this . right . find ( value ) ;
4162 }
4263
4364 return null ;
4465 }
4566
67+ /**
68+ * @param {* } value
69+ * @return {boolean }
70+ */
4671 contains ( value ) {
4772 return ! ! this . find ( value ) ;
4873 }
4974
75+ /**
76+ * @param {* } value
77+ */
5078 remove ( value ) {
5179 const nodeToRemove = this . find ( value ) ;
5280
@@ -65,7 +93,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
6593 // Find the next biggest value (minimum value in the right branch)
6694 // and replace current value node with that next biggest value.
6795 const nextBiggerNode = nodeToRemove . right . findMin ( ) ;
68- if ( nextBiggerNode !== nodeToRemove . right ) {
96+ if ( ! this . nodeComparator . equal ( nextBiggerNode , nodeToRemove . right ) ) {
6997 this . remove ( nextBiggerNode . value ) ;
7098 nodeToRemove . value = nextBiggerNode . value ;
7199 } else {
@@ -85,6 +113,9 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
85113 }
86114 }
87115
116+ /**
117+ * @return {BinarySearchTreeNode }
118+ */
88119 findMin ( ) {
89120 if ( ! this . left ) {
90121 return this ;
0 commit comments