编辑代码

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));