Skip to content

Instantly share code, notes, and snippets.

@thirdreplicator
Created June 19, 2011 02:46
Show Gist options
  • Save thirdreplicator/1033696 to your computer and use it in GitHub Desktop.
Save thirdreplicator/1033696 to your computer and use it in GitHub Desktop.
Walking a graph in Javascript.
<script>
var H = function(){
var g=function(){};
g.prototype.children = [];
g.prototype.values = {};
g.prototype.walk = function(f){
for(var i in H.all){
H.all[i].visited = false;
}
this.walk_helper(f);
};
g.prototype.walk_helper = function(f){
if (this.visited == false){
f( this );
this.visited = true;
for(var i in this.children){
this.children[i].walk_helper( f );
}
}
};
return { g: g};
}();
H.all = {};
H.all[2] = new H.g
H.all[2].values = {x:6};
H.all[3] = new H.g
H.all[3].values = {x:7};
H.all[1] = new H.g
H.all[1].values = {x:5};
H.all[1].children = [H.all[2], H.all[3]];
H.all[1].walk( function(k){ alert( k.values.x ); } );
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment