-
Notifications
You must be signed in to change notification settings - Fork 0
ds.visitors
The ds.visitors
module is a collection of visitors on data structures.
breadthFirst(root, callback, [doProcess])
Traverses the nodes of a graph in breadth-first search order, starting at a particular root
node.
The algorithm extends nodes with a non-enumerable property __hash
in order to tell visited from unvisited nodes in cyclic graphs.
See Wikipedia, Breadth-first search for an explanation of this tree or graph traversion algorithm.
The adjacent nodes of a particular node are expected to be made available via the node's children
property (no other assumptions are made on the node structure):
{
name: "root",
value: 3,
children: [
{
name: "A",
value: 2
},
{
name: "B",
value: 1
}
]
}
-
Object root The designated root node.
-
Function callback A callback f(node, path) to be executed upon visiting
node
, wherepath
is an array of node indexes fromroot
tonode
. -
Function:Boolean|undefined [doProcess] A function f(node, path) which indicates if a particular node shall be visited or not by returning a truthy or falsy value, respectively, where
path
is an array of node indexes fromroot
tonode
.
-
Error Throws if
callback
is not a function. -
Error Throws if
doProcess
is defined and is not a function.
depthFirst(root, callback, [doProcess])
Traverses the nodes of a graph in depth-first search order, starting at a particular root
node.
The algorithm extends nodes with a non-enumerable property __hash
in order to tell visited from unvisited nodes in cyclic graphs.
See Wikipedia, Depth-first search for an explanation of this tree or graph traversion algorithm.
The adjacent nodes of a particular node are expected to be made available via the node's children
property (no other assumptions are made on the node structure):
{
name: "root",
value: 3,
children: [
{
name: "A",
value: 2
},
{
name: "B",
value: 1
}
]
}
-
Object root The designated root node.
-
Function callback A callback f(node, path) to be executed upon visiting
node
, wherepath
is an array of node indexes fromroot
tonode
. -
Function:Boolean|undefined [doProcess] A function f(node, path) which indicates if a particular node shall be visited or not by returning a truthy or falsy value, respectively, where
path
is an array of node indexes fromroot
tonode
.
-
Error Throws if
callback
is not a function. -
Error Throws if
doProcess
is defined and is not a function.