js树
2017-01-31 00:28
316 查看
/** * @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; }
相关文章推荐
- JavaScript 倒计时
- AJAX和JSON—ajax
- 说说JSON和JSONP,也许你会豁然开朗
- javaScript——其他引用类型对象
- JavaScript 使用 requestAnimationFrame 代替 setInterval
- JSP和Servlet[2]
- JSP和Servlet[1]
- 【JS】幻灯片
- js数组方法分析
- JavaScript类型转换规则
- JS获取屏幕高度
- VS code写python代码,tasks.json配置
- Chart.js
- JavaScript -NAN
- js对象去重应用
- C#-WebForm JS定时器
- javascript中数组的常用高阶函数
- JavaScript基础2
- JavaScript基础1
- javascript实现-最简单选项卡切换