二叉查找树(查找最大值、最小值、给定值、删除给定值的实现)

    function Node(data, left, right) {
        this.data = data;
        this.left = left;
        this.right = right;
        this.show = show;
    }
    function show() {
        return this.data;
    }
    function BST() {
        this.root = null;
        this.insert = insert;
        this.getMin = getMin;
        this.getMax = getMax;
        this.find = find;
        this.remove = remove;
    }
    function insert(data) {
        var n = new Node(data, null, null);
        if (this.root == null) {
            this.root = n;
        } else {
            var current = this.root;
            var parent;
            while (true) {
                parent = current;
                if (data < current.data) {
                    current = current.left;
                    if (current == null) {
                        parent.left = n;
                        break;
                    }
                } else {
                    current = current.right;
                    if (current == null) {
                        parent.right = n;
                        break;
                    }
                }
            }
        }
    }
    function getMin() {
        var current = this.root;
        while (current.left != null) {
            current = current.left;
        }
        return current.data;
    }
    function getMax() {
        var current = this.root;
        while (current.right != null) {
            current = current.right;
        }
        return current.data;
    }
    var nums = new BST();
    nums.insert(23);
    nums.insert(45);
    nums.insert(16);
    nums.insert(37);
    nums.insert(3);
    nums.insert(99);
    nums.insert(22);
    var min = nums.getMin();
    document.write("The minimum value of the BST is: " + min);
    document.write("<br />");
    var max = nums.getMax();
    document.write("The maximum value of the BST is: " + max);
    document.write("<br />");

    function find(data) {
        var current = this.root;
        while (current != null) {
            if (current.data == data) {
                return current;
            } else if (data < current.data) {
                current = current.left;
            } else {
                current = current.right;
            }
        }
        return null;
    }
    var s = 99;
    var found = nums.find(s);
    if (found != null) {
        document.write("Found " + s + " in the BST.");
    } else {
        document.write(s + " was not found in the BST.");
    }
    document.write("<br />");

    function getSmallest(node) {//查找最小节点  
        while (node.left != null) {
            node = node.left;
        }
        return node;
    }
    function remove(data) {
        root = removeNode(this.root, data);
    }
    function removeNode(node, data) {
        if (node == null) {
            return null;
        }
        if (data == node.data) {
            if (node.left == null && node.right == null) {
                return null;
            }
            if (node.left == null) {
                return node.right;
            }
            if (node.right == null) {
                return node.left;
            }
            var tempNode = getSmallest(node.right);//查找待删节点右子树上的最小值
            node.data = tempNode.data;//将待删节点右子树上的最小值赋值给待删除节点
            node.right = removeNode(node.right, tempNode.data);//删除待删节点右子树上的最小值
            return node;
        } else if (data < node.data) {
            node.left = removeNode(node.left, data);
            return node;
        } else {
            node.right = removeNode(node.right, data);
            return node;
        }
    }
    nums.remove(99);
    function inOrder(node) {
        if (node != null) {
            inOrder(node.left);
            document.write(node.show() + " ");
            inOrder(node.right);
        }
    }
    document.write("Inorder traversal: " + "<br />");
    inOrder(nums.root);
    /*上述程序运行结果如下:
    The minimum value of the BST is: 3
    The maximum value of the BST is: 99
    Found 99 in the BST.
    Inorder traversal: 
    3 16 22 23 37 45 */

 

    原文作者:绯乐
    原文地址: https://www.cnblogs.com/feile/p/5391767.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞