@@ -3,11 +3,12 @@ import Comparator from '../../../utils/comparator/Comparator';
33
44export default class BinarySearchTreeNode extends BinaryTreeNode {
55 /**
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.
89 */
9- constructor ( value = null , compareFunction = undefined ) {
10- super ( value ) ;
10+ constructor ( value = null , meta = null , compareFunction = undefined ) {
11+ super ( value , meta ) ;
1112
1213 // This comparator is used to compare node values with each other.
1314 this . compareFunction = compareFunction ;
@@ -16,27 +17,30 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
1617
1718 /**
1819 * @param {* } value
20+ * @param {Object } [meta]
1921 * @return {BinarySearchTreeNode }
2022 */
21- insert ( value ) {
23+ insert ( value , meta = null ) {
2224 if ( this . nodeValueComparator . equal ( this . value , null ) ) {
2325 this . value = value ;
26+ this . meta = meta ;
27+
2428 return this ;
2529 }
2630
2731 if ( this . nodeValueComparator . lessThan ( value , this . value ) ) {
2832 // Insert to the left.
2933 if ( this . left ) {
30- this . left . insert ( value ) ;
34+ this . left . insert ( value , meta ) ;
3135 } else {
32- this . setLeft ( new BinarySearchTreeNode ( value , this . compareFunction ) ) ;
36+ this . setLeft ( new BinarySearchTreeNode ( value , meta , this . compareFunction ) ) ;
3337 }
3438 } else if ( this . nodeValueComparator . greaterThan ( value , this . value ) ) {
3539 // Insert to the right.
3640 if ( this . right ) {
37- this . right . insert ( value ) ;
41+ this . right . insert ( value , meta ) ;
3842 } else {
39- this . setRight ( new BinarySearchTreeNode ( value , this . compareFunction ) ) ;
43+ this . setRight ( new BinarySearchTreeNode ( value , meta , this . compareFunction ) ) ;
4044 }
4145 }
4246
0 commit comments