js树
来源:互联网 发布:nginx alias用法 编辑:程序博客网 时间:2024/05/20 23:05
/** * @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
- JS 树
- js树
- JS动态生成树(JS+JSP)
- js树型控件
- js树型菜单
- js源码--树
- JS树效果
- 通过js生成树
- JS树型结构
- 风声JS菜单树
- JS树型菜单
- JS静态树_Dtree
- 静态JS树
- js树型菜单
- JS树简易实现
- js树的应用
- js 树
- 大量数据js树
- 安卓下Glide缓存问题
- 【ZooKeeper】分布式系统服务器上下线自动感知程序开发
- 浅析 UIApplication 生命周期的一些 delegate 方法
- 【PAT】1008. Elevator
- 蓝桥杯 - 算法训练 - 删除数组零元素 - Java
- js树
- 24. Swap Nodes in Pairs
- vue学习 第四节
- 【问题收录】INSTALL_FAILED_NO_MATCHING_ABIS
- 25. Reverse Nodes in k-Group
- 26. Remove Duplicates from Sorted Array
- Mac基于docker的hadoop单机环境搭建
- 27. Remove Element
- 28. Implement strStr()(KMP)