二叉搜索树

来源:互联网 发布:淘宝店铺如何取消分流 编辑:程序博客网 时间:2024/06/05 05:37

定义

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

实现

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的右孩子中最小的一个