js binary tree algorithm

       //生成二叉树
        function binarySearchTree() {
            let Node = function(key) {
                this.key = key;
                this.left = null;
                this.right = null;
            }
            let root = null;
            //插入一个节点
            this.insert = function(key) {
                    let newNode = new Node(key);
                    root == null ? (root =the newNode): (insertNode (the root, the newNode)) 
                } 
            // in a sequence arranged in a binary tree array ascending sort 
            this .inOrderRraverse = function (the callback) { 
                    inOrderRraverseNode (the root, the callback); 
                } 
            // Get the maximum value of 
            this = .max function () {
                     return findMaxNode (the root); 
                } 
            // Get the minimum 
            the this .min = function () {
                     return findMinNode (the root); 
                } 
            // determines whether there is a specified value 
            the this .search = function(key) {
                    return searchNode(root, key);
                }
            //删除指定的值
            this.remove = function(key) {
                root = removeNode(root, key);
            }
        }

        function removeNode(node, key) {
            if (node === null) { //node等于null
                return null;
            }
            if (key > node.key) { //node在右子树
                node.right = removeNode(node.right, key);
                return node;
            } The else  if (Key <node.key) { // Node in the left subtree 
                node.left = the removeNode (node.left, Key);
                 return Node; 
            } the else { // Node equal node.key 
                // no child node 
                if (node.left === null && node.right === null ) { 
                    node = null ;
                     return node 
                } 
                // only one child node 
                IF (node.left === null ) { 
                    node= Node.right;
                     return node; 
                } the else  IF (node.right === null ) { 
                    node = node.left;
                     return node 
                } 
                // two child nodes 
                // right subtree of the node as the smallest to find a node the key, delete a right subtree of child nodes corresponding to 
                the let AUX = findMinNode (node.right); 
                node.key = AUX; 
                node.right = the removeNode (node.right, aux.key);
                 return node; 
            } 

        }

        function searchNode(node, key) {
            if (node != null) {
                if (key > node.key) {
                    return searchNode(node.right);
                } else if (key < node.key) {
                    return searchNode(node.left);
                } else {
                    return true;
                }
            } else {
                return false;
            }
        }

        function findMaxNode(node) {
            if (node) {
                while (node && node.right) {
                    node = node.right;
                }
                return node.key;
            } else {
                return null;
            }
        }

        function findMinNode(node) {
            if (node) {
                while (node && node.left) {
                    node = node.left;
                }
                return node.key;
            } else {
                return null;
            }

        }

        function insertNode(node, newNode) {
            if (node.key > newNode.key) { //比父节点小,在左子树
                node.left == null ? (node.left = newNode) : (insertNode(node.left, newNode))
            } else {
                node.right == null ? (node.right = newNode) : (insertNode(node.right, newNode))
            }
        }

        function inOrderRraverseNode(node, callback) {
            if (node != null) {
                inOrderRraverseNode(node.left, callback)
                callback(node.key);
                inOrderRraverseNode(node.right, callback)
            }
        }

demo:

let tree = new binarySearchTree();
        let arr = [20, 40, 60, 70, 50, 10, 15, 4, 7];
        arr.map(item => {
            tree.insert(item);
        });
        console.log(tree);
        let callback = function(key) {
            console.log(key);
        }
        tree.inOrderRraverse(callback);
        console.log('max++++>' + tree.max());
        console.log('min++++>' + tree.min());
        console.log('search++++>' + tree.search(5));
        tree.remove(20);
        tree.inOrderRraverse(callback);

result:

 

 

 

 

 

Reference from: https://cloud.tencent.com/developer/article/1482473

 

Guess you like

Origin www.cnblogs.com/xiaofenguo/p/11724547.html