mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
45 lines
833 B
JavaScript
45 lines
833 B
JavaScript
import BinarySearchTreeNode from './BinarySearchTreeNode';
|
|
|
|
export default class BinarySearchTree {
|
|
/**
|
|
* @param {function} [nodeValueCompareFunction]
|
|
*/
|
|
constructor(nodeValueCompareFunction) {
|
|
this.root = new BinarySearchTreeNode(null, nodeValueCompareFunction);
|
|
|
|
// Steal node comparator from the root.
|
|
this.nodeComparator = this.root.nodeComparator;
|
|
}
|
|
|
|
/**
|
|
* @param {*} value
|
|
* @return {BinarySearchTreeNode}
|
|
*/
|
|
insert(value) {
|
|
return this.root.insert(value);
|
|
}
|
|
|
|
/**
|
|
* @param {*} value
|
|
* @return {boolean}
|
|
*/
|
|
contains(value) {
|
|
return this.root.contains(value);
|
|
}
|
|
|
|
/**
|
|
* @param {*} value
|
|
* @return {boolean}
|
|
*/
|
|
remove(value) {
|
|
return this.root.remove(value);
|
|
}
|
|
|
|
/**
|
|
* @return {string}
|
|
*/
|
|
toString() {
|
|
return this.root.toString();
|
|
}
|
|
}
|