JavaScript Build Tree Data Structure Using Array
来源:互联网 发布:怎么知道路由器mac地址 编辑:程序博客网 时间:2024/05/22 11:41
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父节点所有子树都需要被删除。
0 0
- JavaScript Build Tree Data Structure Using Array
- Build Tree View Structure for SharePoint List Data
- Dynamic Array--Data Structure
- Array of Data Structure
- Data Structure: Tree
- data structure -- tree
- Data Structure:Binary Tree
- Basic Tree--Data Structure
- AVL tree--Data Structure
- Data Structure -Tree
- Data structure of tree
- Data Structure: Binary Index Tree
- Binary Search Tree--Data Structure
- Build Min Heap Using Array
- MySQL display data structure using command line
- FWNX- build a binary search tree using array - java version <<recursive>>
- Perl implement Tree data structure (1)
- Perl implement Tree data structure (2)
- JDK动态代理
- MFC对话框中屏蔽Enter键与ESC键
- maven 的搭建
- 【Unity3d】游戏开发笔记二 C#与JS之间的语法差异
- [LeetCode30]Substring with Concatenation of All Words
- JavaScript Build Tree Data Structure Using Array
- 写可测试的代码
- photoshop cs6基础学习
- ssh无密码原理简介
- ucosii的任务调度原理(文章来自百度)
- [转]使用Eclipse与Pydev开发Python
- 分析在cocos2dx中lua调用c++类的细节
- Uncaught SyntaxError: Unexpected end of input
- 初学 Ext.extends