JavaScript Build Tree Data Structure Using Array
2014-05-10 04:03
483 查看
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父节点所有子树都需要被删除。
相关文章推荐
- Data Structure Binary Tree: Connect nodes at same level using constant extra space
- FWNX- build a binary search tree using array - java version <<recursive>>
- Build Tree View Structure for SharePoint List Data
- Data Structure Array: Maximum of all subarrays of size k
- Data Structure Array: Move all zeroes to end of array
- Accessing List Data using the JavaScript Client OM and displaying in a Dialog
- 树状结构 Tree data structure in C#
- [转载]EXT核心API详解Ext.data(十)-DataReader/ArrayReader/JsonReader/XmlReader javascript
- MySQL display data structure using command line
- Data Structure Binary Tree: Check for Children Sum Property in a Binary Tree
- Data Structure Array: Find the minimum distance between two numbers
- OVa Online Judge 学习笔记- AOAPC I: Volume 2. Data Structure Tree
- bosondata/chrome-prerender: Render JavaScript-rendered page as HTML/PDF/mhtml/png/jpeg using headless Chrome
- Senior Data Structure · 浅谈线段树(Segment Tree)
- Data Structure Binary Tree: Check if a given Binary Tree is SumTree
- Build a JavaScript Compressor tool using NodeJS, ExpressJS, Jade, UglifyJS tutorial Read more: http
- Data structure of tree
- [Immutable.js] Using fromJS() to Convert Plain JavaScript Objects into Immutable Data
- [数据结构]tree-form data structure summary
- 【DataStructure&AlgorithmInJava】Ch02-HighArrayDemo