JavaScript Build Tree Data Structure Using Array

1 篇文章 0 订阅
 function Tree(name,parent,child){
    this.name = name;
    this.parent = parent || '';
    this.children = child || [];
    this.addNode = function (parent){
        this.children = parent;
    }
    this.addChild = function (childName, parentName){
        this.children.push(new Tree(childName, parentName));
    }
    this.addChildrenForNode = function (nodeChild, nodeName) {

            if (this.name == nodeName) {
                this.addChild(nodeChild, nodeName);
            }

            else if (this.children != null && this.children.length > 0) {
               (function () {
                var subTree = arguments[0];
                var indicatorName = arguments[1];
                for (var j = 0; j < subTree.length; j++) {
                    if (subTree[j].name == indicatorName) {
                        subTree[j].addChild(nodeChild, indicatorName);
                        break;
                    }
                    else if (subTree[j].children != null && subTree[j].children.length > 0)
                    {
                        arguments.callee(subTree[j].children, nodeName);
                    }
                }
                } ) (this.children, nodeName);
            }
             
    }
    }

    function getNodeAllChildren(node) {
        
        if (node.children != null && node.children.length > 0) {
            for (var i = 0; i < node.children.length; i++) {
               childrenNames.push(node.children[i].name);
               if (node.children[i].children != null && node.children[i].children.length > 0) {
                        getNodeAllChildren(node.children[i]);
               }
            }
        }

    }

    function deleteNodeAllChildren(tree, childName) {

            if (tree.name == childName) {
                getNodeAllChildren(tree);
                tree.children =[];
            }

            else if (tree.children != null && tree.children.length > 0) {
               return(function () {
                var subTree = arguments[0];
                var indicatorName = arguments[1];
                for (var j = 0; j < subTree.length; j++) {
                    if (subTree[j].name == indicatorName) {
                        getNodeAllChildren(subTree[j]);
                        subTree[j].children = [];
                        break;
                    }
                    else if (subTree[j].children != null && subTree[j].children.length > 0)
                    {
                        arguments.callee(subTree[j].children, childName);
                    }
                }
                } ) (tree.children, childName);
            }
            return tree;
             
    }

    function deleteDrillDownIndicator(tree, indicator) {

            if (tree.name == indicator) {
                tree.children =[];
            }

            else if (tree.children != null && tree.children.length > 0) {
               return(function () {
                var subTree = arguments[0];
                var indicatorName = arguments[1];
                for (var j = 0; j < subTree.length; j++) {
                    if (subTree[j].name == indicatorName) {
                        deleteNodeAllChildren(tree, subTree[j].parent);
                        break;
                    }
                    else if (subTree[j].children != null && subTree[j].children.length > 0)
                    {
                        arguments.callee(subTree[j].children, indicator);
                    }
                }
                } ) (tree.children, indicator);
            }
            return tree;
             
    }

    var tree = new Tree("Indicator"); // create a tree (or a portion of a tree) with root "A" and empty children

    tree.addChildrenForNode("1A","Indicator");   // A -> B1
    tree.addChildrenForNode("1B","Indicator");   // A -> B2
    tree.addChildrenForNode("2AA","1A");   // add this sub tree under A->B1
    tree.addChildrenForNode("2AB","1A");
    tree.addChildrenForNode("2BA","1B");   // add this sub tree under A->B1
    tree.addChildrenForNode("2BB","1B");
    tree.addChildrenForNode("3BAA","2BA");
    tree.addChildrenForNode("3BAB","2BA");

    var childrenNames = [];
    console.log(JSON.stringify(tree));
    deleteDrillDownIndicator(tree, "2BA");
    console.log(JSON.stringify(childrenNames));

    childrenNames = [];
    console.log(JSON.stringify(tree));
    deleteDrillDownIndicator(tree, "1B");
    console.log(JSON.stringify(childrenNames));
    console.log(JSON.stringify(tree));
    //findChild(tree,"2BA");

其中deleteDrillDownIndicator 为项目所需,当在Data Warehouse Query 中执行Drill-down操作时,产生不同level的新Drill-down indicator,当删除特定indicator时,该indicator父节点所有子树都需要被删除。 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值