数据结构与算法JavaScript描述-个人练习(Binary Tree)

#Data Structure - Binary Tree @(Data Structure & Algorithm)

function Node(key,parent,left,right){
    this.key = key;
    this.parent = parent;
    this.left = left;
    this.right = right;
}
Node.prototype = {
    constructor: Node,
    display: function(){
        var parent = (this.parent !== null) ? this.parent.key : "null";
        var left = (this.left !== null) ? this.left.key : "null";
        var right = (this.right !== null) ? this.right.key : "null";
        console.log("Key: " + this.key + " Parent: " + parent + " Left: " + left + " Right: " + right);
    }
};
//二叉查找树
//较小的值插入到左节点
function BST(){
    this.root = null;
}
BST.prototype = {
    constructor: BST,
    insert: function(key){
        var node = new Node(key, null, null, null);
        if (this.root === null){
            this.root = node;
            return true;
        }
        var p = this.root;
        while (true){
            if (key < p.key){
                if (p.left === null){
                    p.left = node;
                    node.parent = p;
                    return true;
                }
                p = p.left;
                continue;
            } else{
                if (p.right === null){
                    p.right = node;
                    node.parent = p;
                    return true;
                }
                p = p.right;
                continue;
            }
        }
        return false;
    },
    //inorder tree walk
    itw: function(p){
        if (p !== null){
            this.itw(p.left);
            p.display();
            this.itw(p.right);
        }
        return true;
    },
    //preorder
    pretw: function(p){
        if (p !== null){
            p.display();
            this.itw(p.left);
            this.itw(p.right);
        }
        return true;
    },
    //postorder
    posttw: function(p){
        if (p !== null){
            this.itw(p.left);
            this.itw(p.right);
            p.display();
        }
        return true;
    },
    find: function(key){
        var n = this.root;
        while (n !== null) {
            if (key < n.key){
                n = n.left;
                continue;
            } else if (key > n.key){
                n = n.right;
                continue;
            } else{
                return n;
            }
        }
        return false;
    },
    max: function(){
        var n = this.root;
        while (n.right !== null){
            n = n.right;
        }
        n.display();
    },
    min: function(){
        var n = this.root;
        while (n.left !== null){
            n = n.left;
        }
        n.display();
    },
    replace: function(prevNode,postNode){
        prevNode.key = postNode.key;
        prevNode.left = postNode.left;
        prevNode.right = postNode.right;
        return true;
    },
    replaceKey: function(prevNode,postNode){
        prevNode.key = postNode.key;
        return true;
    },
    removeLeafNode: function(node){
        var p = node.parent;
        if (p.left === node){
            p.left = null;
            return true;
        }
        p.right = null;
        return true;
    },
    remove: function(key){
        var node = this.find(key);
        if (node){
            if (node.left === null){
                if (node.right === null){
                    this.removeLeafNode(node);
                } else {
                    this.replace(node,node.right);
                }
            } else {
                if (node.left.right === null){
                    this.replace(node, node.left);
                } else {
                    var maxNode = node.left.right;
                    while (maxNode.right !== null){
                        maxNode = maxNode.right;
                    }
                    this.replaceKey(node, maxNode);
                    this.removeLeafNode(maxNode);
                }
            }
            return true;
        } else { 
            return false;
        }
    }
};



var arr = [49,63,51,81,55,60];
var t = new BST();
for (var i = 0; i < arr.length; i++){
    t.insert(arr[i]);
}
//tree walk
console.log("inorder");
t.itw(t.root);
console.log("---");

//remove node
t.remove(63);
t.itw(t.root);

转载于:https://my.oschina.net/xixine/blog/471546

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值