Javascript实现树结构
2016-04-23 23:02
537 查看
树节点属性 Node
data:节点值
parent :指向节点的父节点
children:指向节点的孩子节点
Tree 属性与方法
_root :树的根节点
traverseDF(callback) :深度遍历
traverseBF(callback):广度遍历
树的实现
树节点定义:
树的构造函数:
traverseBF(callback)
contains(data, traversal)
add(child, parent)
1 of 5:
例如:
4 of 5:
5 of 5:
原文地址:http://code.tutsplus.com/articles/data-structures-with-javascript-tree--cms-23393
data:节点值
parent :指向节点的父节点
children:指向节点的孩子节点
Tree 属性与方法
_root :树的根节点
traverseDF(callback) :深度遍历
traverseBF(callback):广度遍历
树的实现
树节点定义:
function Node(data) { this.data = data; this.parent = null; this.children = []; }
树的构造函数:
function Tree(data) { var node = new Node(data); this._root = node; }
树的方法
traverseDF(callback)traverseBF(callback)
contains(data, traversal)
add(child, parent)
remove(node, parent)
1 of 5:
traverseDF(callback)
Tree.prototype.traverseDF = function(callback) { (function traverse(currentNode) { var i,len; var children = currentNode.children; for(i=0,len = children.length;i<len;i++) { traverse(children[i]); } callback(currentNode); })(this._root); }2 of 5:
traverseBF(callback)
Tree.prototype.traverseBF = function(callback) { var queue = [], i, len; queue.push(this._root); while(queue.length) { var cur = queue.shift(); callback(cur); for(i=0,len = cur.children.length; i < len;i++) { queue.push(cur.children[i]); } } }3 of 5:
contains(callback, traversal)
Tree.prototype.contains = function(callback, traversal) { traversal.call(this, callback); }
例如:
tree.contains(function(node){ if (node.data === 'two') { console.log(node); } }, tree.traverseBF);
4 of 5:
add(data, toData, 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 { throw new Error('cannot add node to an unexist parent'); } }
5 of 5:
remove(data, fromData, traversal)
Tree.prototype.remove = function(data, fromData, traversal) { var tree = this, parent = null, childToRemove = null, i,len,children, index; var callback = function(node) { if(node.data === fromData) { parent = node; } } this.contains(callback, traversal); if(parent) { index = findIndex(parent.children, data); console.log(index); if(index === undefined) { throw new Error("Node to remove does not exist"); }else { childToRemove = parent.children.splice(index, 1); } } console.log(childToRemove); return childToRemove; }
function findIndex(arr, data){ var i=0, len = arr.length, index; for(;i<len;i++) { if(arr[i].data === data) { index = i; } } return index; }例如:
var tree = new Tree('one'); tree._root.children.push(new Node('two')); tree._root.children[0].parent = tree; tree._root.children.push(new Node('three')); tree._root.children[1].parent = tree; tree._root.children[0].children.push(new Node('four')); tree._root.children[0].children[0].parent = tree._root.children[0]; tree._root.children[0].children.push(new Node('five')); tree._root.children[0].children[1].parent = tree._root.children[0]; tree._root.children[1].children.push(new Node('six')); tree._root.children[1].children[0].parent = tree._root.children[1]; tree._root.children[1].children.push(new Node('serven')); tree._root.children[1].children[1].parent = tree._root.children[1]; // tree.traverseBF(function(node) { // console.log(node.data); // }); tree.contains(function(node) { if(node.data === 'two') { console.log(node); } }, tree.traverseBF); tree.add("eight","four", tree.traverseBF); var remove = tree.remove("two", "one", tree.traverseBF); tree.traverseBF(function(node) { console.log(node.data); });
原文地址:http://code.tutsplus.com/articles/data-structures-with-javascript-tree--cms-23393
相关文章推荐
- 笔记之js使用ajax接受服务器传过来的数组异常时的情况处理(含视频)
- JavaScript Date 对象
- 用javascript做的简单复选框的全选与反选
- JavaScript实现本地存储
- Function javascript
- Seajs - JS模块化简介
- javascript 实现jsonp
- js的get set, 构造函数,匿名函数,prototype(原型)
- JavaScript中的for in (1)
- 【bzoj 1013】 [JSOI2008] 球形空间产生器sphere
- button按钮通过JSP给table添加下拉列表
- 遍历 JavaScript 数组
- cxf web service 解决返回的json格式数据乱码问题
- JS中window.showModalDialog()详解 HTML DOM open() 方法
- js闭包
- json多层解析,和风天气数据源,volley请求
- jstl el
- 用js判断复选框(checkbox)是否选中的问题
- json&jsonp
- javascript限制只能输入数字