js树

    /**
     * @Author    Toney
     * @Explain   [description]
     * @DateTime  2017-01-22
     * @copyright [datacvg]
     * @param     {[type]}      data [description]
     */
    function Node(data) {
        this.data = data;
        this.children = [];
    }
    /**
     * @Author    Toney
     * @Explain   [description]
     * @DateTime  2017-01-22
     * @copyright [datacvg]
     * @param     {[type]}      data [description]
     */
    function Tree(data){
        var node = new Node(data);
        this._root = node;
    }
    /**
     * @Author    Toney
     * @Explain   [深度优先遍历]
     * @DateTime  2017-01-22
     * @copyright [datacvg]
     * @param     {Function}    callback [description]
     * @return    {[type]}               [description]
     */
    Tree.prototype.traverseDF = function (callback) {
        (function recurse (currentNode) {
            // 迭代每一个子
            for(var i = 0,len = currentNode.children.length;i < len;i++){
                recurse(currentNode.children[i]);
            }
            callback(currentNode);
        })(this._root);
    }
    /**
     * @Author    Toney
     * @Explain   [广度优先遍历]
     * @DateTime  2017-01-22
     * @copyright [datacvg]
     * @param     {Function}    callback [description]
     * @return    {[type]}               [description]
     */
    Tree.prototype.traverseBF = function(callback){
        var queue = [];

        queue.push(this._root);

        var currentTree = queue.shift();

        while(currentTree){
            for(var i = 0,len = currentTree.children.length; i < len; i++){
                queue.push(currentTree.children[i]);
            }
            callback(currentTree);
            currentTree = queue.shift();
        }
    }
    /**
     * @Author    Toney
     * @Explain   [搜索包含节点]
     * @DateTime  2017-01-22
     * @copyright [datacvg]
     * @param     {Function}    callback  [description]
     * @param     {[type]}      traversal [description]
     * @return    {[type]}                [description]
     */
    Tree.prototype.contains = function(callback,traversal){
        traversal.call(this,callback);
    }
    /**
     * @Author    Toney
     * @Explain   [添加一个节点到树]
     * @DateTime  2017-01-22
     * @copyright [datacvg]
     * @param     {[string]}      data      [新添加节点]
     * @param     {[string]}      toData    [目标节点]
     * @param     {[function]}      traversal [遍历树所用的方法类型]
     */
    Tree.prototype.add = function(data,toData,traversal){
        var child = new Node(data),
            parent = null,
            callback = function(node){
                if(node.data === toData){
                    parent = node;
                }
            };

        this.contains(callback,traversal);

        if(parent){
            parent.children.push(child);
            child.parent == parent;
        }else{
            console.log('Error:Cannot add node to a non-existent parent.');
        }
    }
    Tree.prototype.remove = function(data,fromData,traversal){

        var tree = this,
            parent = null,
            childToRemove = null,
            index;
        var callback = function(node){
            if(node.data === fromData){
                parent = node;
            }
        };

        var findIndex = function(arr, data){

            var index;

            for(var i = 0; i < arr.length; i++) {
                if(arr[i].data === data) {
                    index = i;
                }
            }

            return index;
        };

        this.contains(callback,traversal);

        if(parent){
            index = findIndex(parent.children,data);
            if(index === undefined){
                console.log('Error:Node to remove does not exist');
            }else{
                childToRemove = parent.children.splice(index,1);
            }
        }else{
            console.log('Error:parent does not exist.');
        }
        return childToRemove;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值