Skip to content

Commit

Permalink
feat(cluster): add traverse method
Browse files Browse the repository at this point in the history
Traverses all the nodes of the tree and calls provided callback on each of them
  • Loading branch information
stropitek committed Feb 22, 2017
1 parent 9c9d1bf commit e437076
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Cluster.prototype.cut = function (threshold) {

/**
* Merge the leaves in the minimum way to have 'minGroups' number of clusters
* @param {number} minGroups
* @param {number} minGroups - Them minimum number of children the first level of the tree should have
* @return {Cluster}
*/
Cluster.prototype.group = function (minGroups) {
Expand All @@ -61,4 +61,21 @@ Cluster.prototype.group = function (minGroups) {
return root;
};

/**
* Traverses the tree depth-first and provide callback to be called on each individual node
* @param {function} cb - The callback to be called on each node encounter
* @type {Cluster}
*/
Cluster.prototype.traverse = function (cb) {
function visit(root, callback) {
callback(root);
if (root.children) {
for (var i = root.children.length - 1; i >= 0; i--) {
visit(root.children[i], callback);
}
}
}
visit(this, cb);
};

module.exports = Cluster;

0 comments on commit e437076

Please sign in to comment.