二叉搜索树

二叉搜索树实现

定义

左孩子节点小于父节点,右孩子节点大于父节点。

实现

function Node(ele) {
    this.data = ele;
    this.left = null;
    this.right = null;
    this.parentNode = null;
    this.show = show;
}
function BST() {
    this.root = null;
    this.insert = insert;
    this.search = search;
    this.min = min;
    this.max = max;
    this.indexPoint = 0;
    this.indexLinear = 0;
    this.point = point;
    this.linear = linear;
    this.deleteEle = deleteEle;
}
function point(node,obj) {//有几个元素节点
    if(node !== null){
        obj.indexPoint++;
        if(node.left !== null){
            point(node.left,obj);
        }
        if(node.right !== null){
            point(node.right,obj);
        }
    }
}
function linear(node,obj) {//有几个连线
    if(node !== null){
        if(node.left !== null){
            obj.indexLinear++;
            linear(node.left,obj);
        }
        if(node.right !== null){
            obj.indexLinear++;
            linear(node.right,obj);
        }
    }
}
function search(data) {
    let cur = this.root;
    while (cur !== null){
        if(cur.data === data){
            return cur;
        }else if(cur.data > data){
            cur = cur.left;
            if(cur === null){
                break;
            }
        }else{
            cur = cur.right;
            if(cur === null){
                break;
            }
        }
    }
    return null;
}
function show() {
    return this.data;
}
function preOder(node) {
    if(node !== null){
        console.log(node.data);
        preOder(node.left);
        preOder(node.right);
    }
}
function inOder(node) {
    if(node !== null){
        inOder(node.left);
        console.log(node.data);
        inOder(node.right);
    }
}
function postOder(node) {
    if(node !== null){
        postOder(node.left);
        postOder(node.right);
        console.log(node.data);
    }
}
function min() {
    var cur = this.root;
    while (cur.left != null){
        cur = cur.left;
    }
    return cur.data;
}
function max() {
    var cur = this.root;
    while (cur.right != null){
        cur = cur.right;
    }
    return cur.data;
}
function insert(data) {
    var node = new Node(data,null,null),
        cur = this.root,
        parent = null;
    if(this.root == null){
        this.root = node;
    }else{
        while (true){
            parent = cur;
            if(data < cur.data){
                cur = cur.left;
                if(cur == null){
                    parent.left = node;
                    node.parentNode = parent;
                    break;
                }
            }else {
                cur = cur.right;
                if(cur == null){
                    parent.right = node;
                    node.parentNode = parent;
                    break;
                }
            }
        }
    }
}
function deleteEle(node) {
    if(node.right === null){
        node.parentNode.left = node.left;
        node = null;
    }else if(node.left === null){
        node.parentNode.right = node.right;
    }else{
        let rightMin = node.right;
        while(rightMin.left !== null){
            rightMin = rightMin.left;
        }
        rightMin.parentNode.left = null;
        rightMin.parentNode = node.parentNode;
        node.parentNode.left = rightMin;
        rightMin.right = node.right;
        rightMin.left = node.left;
        rightMin.right.parentNode = rightMin;
        rightMin.left.parentNode = rightMin;
    }
}

删除节点时若被删除节点有左右孩子,则用于替代删除节点d的新元素s应满足:
1. s>d的所有左孩子节点,所以s应该在d的右孩子中找
2. s< d的所有右孩子节点,即s的右孩子中最小的一个

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值