zoukankan      html  css  js  c++  java
  • 二叉查找树

    // 实现二叉查找树
    function BinaryTree() {
        this.root = null;
        this.node = function (value) {return {value: value, left: null, right: null}};
        this.add  = function (value) {
            if (this.root === null) {
                this.root = this.node(value);
            }
            else {
                var current = this.root;
                var parent;
    
                while (true) {
                    parent = current;
                    if (value < current.value) {
                        current = current.left;
                        if (current === null) {
                            parent.left = this.node(value);
                            break;
                        }
                    }
                    else {
                        current = current.right;
                        if (current === null) {
                            parent.right = this.node(value);
                            break;
                        }
                    }
                }
            }
        };
    }
    
    
    // 遍历查找,从左到右
    function inOrder(node) {
        if (node !== null) {
            inOrder(node.left);
            inOrder(node.right);
        }
    }
    
    
    // 查找最小值
    function getMin(node) {
        while (node.left !== null) {
            node = node.left;
        }
        return node.value;
    }
    
    
    // 查找最大值
    function getMax(node) {
        while (node.right !== null) {
            node = node.right;
        }
        return node.value;
    }
    
    
    // 查找指定值
    function find(node, value) {
        while (node !== null) {
            if (value === node.value) {
                return true;
            }
            else {
                node = value < node.value ? node.left : node.right;
            }
        }
        return false;
    }
    
    
    // 删除节点
    function remove(node, value, type) {
        if (node === null) return null;
        if (value === node.value) {
            if (node.left === null && node.right === null) return null;
            if (node.left === null) return node.right;
            if (node.right === null) return node.left;
            if (typeof type !== 'undefined') {
                if (type === 'getMax') {
                    node.value = getMax(node.right);
                    node.right = remove(node.right, node.value);
                }
                else {
                    node.value = getMin(node.left);
                    node.left  = remove(node.left, node.value);
                }
            }
            else {
                node.value = getMin(node.right);
                node.right = remove(node.right, node.value);
            }
        }
        else {
            value < node.value ? node.left = remove(node.left, value, 'getMax') : node.right = remove(node.right, value, 'getMin');
        }
        return node;
    }

     

    var obj = new BinaryTree();
      obj.add(0);
      obj.add(2);
      obj.add(1);
      obj.add(3);
      obj.add(4);
      obj.add(8);
      obj.add(1);
      obj.add(-3);
      obj.add(-2);
      obj.add(-7);
      obj.add(-22);
      obj.add(-1);
      obj.add(-3);
      obj.add(-1);

  • 相关阅读:
    oracle中的DECODE
    服务器修改密码cmd
    oracle 创建用户,授权用户,创建表,查询表
    Oralce 处理字符串函数
    oracle 非数字型转数字型
    d3
    linux SVN 安装配置
    JAVA with Cassandra
    Struts2实现文件上传和下载
    xmanager 5图文使用教程
  • 原文地址:https://www.cnblogs.com/shanchenba/p/5664419.html
Copyright © 2011-2022 走看看