@@ -3,11 +3,12 @@ import Comparator from '../../../utils/comparator/Comparator';
3
3
4
4
export default class BinarySearchTreeNode extends BinaryTreeNode {
5
5
/**
6
- * @param {* } [value]
7
- * @param {function } [compareFunction]
6
+ * @param {* } [value] - node value.
7
+ * @param {Object } [meta] - any meta information that is attached to the node.
8
+ * @param {function } [compareFunction] - comparator function for node values.
8
9
*/
9
- constructor ( value = null , compareFunction = undefined ) {
10
- super ( value ) ;
10
+ constructor ( value = null , meta = null , compareFunction = undefined ) {
11
+ super ( value , meta ) ;
11
12
12
13
// This comparator is used to compare node values with each other.
13
14
this . compareFunction = compareFunction ;
@@ -16,27 +17,30 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
16
17
17
18
/**
18
19
* @param {* } value
20
+ * @param {Object } [meta]
19
21
* @return {BinarySearchTreeNode }
20
22
*/
21
- insert ( value ) {
23
+ insert ( value , meta = null ) {
22
24
if ( this . nodeValueComparator . equal ( this . value , null ) ) {
23
25
this . value = value ;
26
+ this . meta = meta ;
27
+
24
28
return this ;
25
29
}
26
30
27
31
if ( this . nodeValueComparator . lessThan ( value , this . value ) ) {
28
32
// Insert to the left.
29
33
if ( this . left ) {
30
- this . left . insert ( value ) ;
34
+ this . left . insert ( value , meta ) ;
31
35
} else {
32
- this . setLeft ( new BinarySearchTreeNode ( value , this . compareFunction ) ) ;
36
+ this . setLeft ( new BinarySearchTreeNode ( value , meta , this . compareFunction ) ) ;
33
37
}
34
38
} else if ( this . nodeValueComparator . greaterThan ( value , this . value ) ) {
35
39
// Insert to the right.
36
40
if ( this . right ) {
37
- this . right . insert ( value ) ;
41
+ this . right . insert ( value , meta ) ;
38
42
} else {
39
- this . setRight ( new BinarySearchTreeNode ( value , this . compareFunction ) ) ;
43
+ this . setRight ( new BinarySearchTreeNode ( value , meta , this . compareFunction ) ) ;
40
44
}
41
45
}
42
46
0 commit comments