js--数据结构--树
2016-04-08 22:18
483 查看
1.树的构造函数
1.1 Node属性:
data存储的值
parent指向节点的父节点
children指向许多孩子节点
Node构造函数:
function Node(data) { this.data = data; this.parent = null; this.children = []; }
1.2Tree
属性和方法:
root:指向一棵树的根节点
deepTraversal(callback,node):遍历与DFS树的节点
widthTraversal(callback,node):遍历与BFS树的节点
contasins(data,traversal)搜索树中的节点
add(data,toData,traverse)将节点添加到一棵树
remove(child,parent)在一棵树中删除一个节点
Tree构造函数:
先创建一个新实例的Node,随后指派node作为树的根
function Tree (data) { var node = new Node(data); this._root = node; }
1.3创建tree
var tree = new Tree('CEO'); tree._root;//{data:'CEO',parent:null,children:[]}
2.一个树的方法
2.1 traverseDF(callback)遍历与DFS树的节点//深度遍历 Tree.prototype.deepTraversal = function(callback,node){ var nodes = []; if (node != null) { var stack = []; stack.push(node); while (stack.length != 0) { var item = stack.pop(); callback(item); nodes.push(item); var children = item.children; for (var i = children.length - 1; i >= 0; i--) stack.push(children[i]); } } return nodes; };
举例
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.push(new Node('four')); tree._root.children[2].parent = tree; tree._root.children[0].children.push(new Node('five')); tree._root.children[0].children[0].parent = tree._root.children[0]; tree._root.children[0].children.push(new Node('six')); tree._root.children[0].children[1].parent = tree._root.children[0]; tree._root.children[2].children.push(newNode('seven')); tree._root.children[2].children[0].parent = tree._root.children[2]; /* creates this tree one ├── two │ ├── five │ └── six ├── three └── four └── seven */ tree.deepTraversal(function(node) { console.log(node.data) },tree._root); /* logs the following strings to the console 'five' 'six' 'two' 'three' 'seven' 'four' 'one' */
2.2 traverseBF(callback)遍历与BFS树的节点
//广度搜索 Tree.prototype.wideTraversal = function(callback,node) { var nodes = []; if (node != null) { var queue = []; queue.unshift(node); while (queue.length != 0) { var item = queue.shift(); callback(item); nodes.push(item); var children = item.children; for (var i = 0; i < children.length; i++) queue.push(children[i]); } } return nodes; }
2.3 contains(data,traversal)搜索树中的节点
//接受两个参数 ︰ 搜索和遍历类型的数据。 Tree.prototype.contains = function(callback, traversal) { traversal.call(this, callback); };
想象一下我们想要登录到控制台包含的数据具有奇数和遍历我们与 BFS 树中的每个节点的任何节点。这是我们要编写的代码 ︰
// tree is an example of a root node tree.contains(function(node){ if(node.data === 'two') { console.log(node); } }, tree.traverseBF);
2.4 add(child,parent)将节点添加到一棵树
第一个参数,data,用于创建新实例的Node。
第二个参数,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 { thrownewError('Cannot add node to a non-existent parent.'); } };
实例:
vartree = newTree('CEO'); tree.add( 'CEO''VP of Happiness', , tree.traverseBF); /* our tree 'CEO' └── 'VP of Happiness' */
实例:
vartree = newTree('CEO'); tree.add( 'CEO''VP of Happiness', , tree.traverseBF); tree.add( 'CEO''VP of Finance', , tree.traverseBF); tree.add('VP of Sadness', 'CEO', tree.traverseBF); tree.add( 'VP of Finance''Director of Puppies', , tree.traverseBF); tree.add( 'Director of Puppies''Manager of Puppies', , tree.traverseBF); /* tree 'CEO' ├── 'VP of Happiness' ├── 'VP of Finance' │ ├── 'Director of Puppies' │ └── 'Manager of Puppies' └── 'VP of Sadness' */
2.5 remove(node,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; } }; this.contains(callback, traversal); if(parent) { index = findIndex(parent.children, data); if(index === undefined) { thrownewError('Node to remove does not exist.'); } else { childToRemove = parent.children.splice(index, 1); } } else { thrownewError('Parent does not exist.'); } return childToRemove; };
3.一棵树深度和广度的遍历
function Node(data) { this.data = data; this.parent = null; this.children = []; }
function Tree(data) {
var node = new Node(data);
this._root = node;
}
//深度搜索
Tree.prototype.deepTraversal = function(callback,node){
var nodes = [];
if (node != null) {
var stack = [];
stack.push(node);
while (stack.length != 0) {
var item = stack.pop();
callback(item);
nodes.push(item);
var children = item.children;
for (var i = children.length - 1; i >= 0; i--)
stack.push(children[i]);
}
}
return nodes;
};
//广度搜索 Tree.prototype.wideTraversal = function(callback,node) { var nodes = []; if (node != null) { var queue = []; queue.unshift(node); while (queue.length != 0) { var item = queue.shift(); callback(item); nodes.push(item); var children = item.children; for (var i = 0; i < children.length; i++) queue.push(children[i]); } } return nodes; }
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.push(new Node('four'));
tree._root.children[2].parent = tree;
tree._root.children[0].children.push(new Node('five'));
tree._root.children[0].children[0].parent = tree._root.children[0];
tree._root.children[0].children.push(new Node('six'));
tree._root.children[0].children[1].parent = tree._root.children[0];
tree._root.children[2].children.push(new Node('seven'));
tree._root.children[2].children[0].parent = tree._root.chil
var arr = tree.deepTraversal(function(node) {
console.log(node.data)
},tree._root);
console.log(arr);
var arr2 = tree.wideTraversal(function(node) {
console.log(node.data)
},tree._root);
console.log(arr2);
相关文章推荐
- 数据结构-线性表的顺序结构
- 数据结构杂谈(一)浅谈基本排序算法
- PostgreSQL异步客户端(并模拟redis 数据结构)
- [Java8 Collection源码+算法+数据结构]-List(二)
- 数据结构——树
- 数据结构(线段树):BZOJ 1018: [SHOI2008]堵塞的交通traffic
- 数据结构实用概念
- 【学习笔记----数据结构07-队列】
- 【学习笔记----数据结构06-栈的应用】
- 数据结构-堆
- 【学习笔记----数据结构05-栈与队列】
- 【学习笔记----数据结构04-单循环链表】
- 位运算-Number of 1 Bits(求一个十进制数字,它的二进制表示中有多少个 1(bit))
- 【学习笔记----数据结构03--线性链表】
- 数组分段查找
- 栈-Min Stack(实现一个栈,用常数级时间找出栈中的最小值)
- 【学习笔记----数据结构01-概述】
- hdu 1506(dp || 单调栈)
- 常用数据结构及类
- HashMap源码注解 之 内部数据结构 Node (三)