class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor(){
this.root = null;
}
insert(value) {
const newNode = new Node(value);
if(this.root === null) {
this.root = newNode;
} else {
this.insertNode(this.root, newNode);
}
}
insertNode(node, newNode) {
if(newNode.value < node.value) {
if(node.left === null) {
node.left = newNode;
} else {
this.insertNode(node.left, newNode);
}
} else {
if(node.right === null) {
node.right = newNode;
} else {
this.insertNode(node.right, newNode);
}
}
}
search(value){
}
inOrderTraverse(cb){
this.traverseNode(this.root, cb, 'center');
}
traverseNode(node, cb, order = 'top'){
if(node !== null) {
if(order === 'top') cb(node.value);
this.traverseNode(node.left, cb, order);
if(order === 'center') cb(node.value);
this.traverseNode(node.right, cb, order);
if(order === 'bottom') cb(node.value);
}
}
preOrderTraverse(cb){
this.traverseNode(this.root, cb, 'top');
}
postOrderTraverse(cb){
this.traverseNode(this.root, cb, 'bottom');
}
min(){
}
max(){
}
remove(){
}
}
const tree = new BinarySearchTree();
const arr = [11,7,15,5,3,9,8,10,13,12,14,20,18,25]
arr.forEach(a=>tree.insert(a));
console.log(JSON.stringify(tree));