Skip to content

Instantly share code, notes, and snippets.

@deleteman
Created July 25, 2020 11:34
Show Gist options
  • Save deleteman/773a394f70e435da3694e2baff451e3e to your computer and use it in GitHub Desktop.
Save deleteman/773a394f70e435da3694e2baff451e3e to your computer and use it in GitHub Desktop.
class Node {
constructor(value) {
this.value = value
this.links = []
}
linkTo(node, weight) {
this.links.push(new Link(this, weight, node))
}
}
class Link {
constructor(a, weight, b) {
this.left = a;
this.weight = weight
this.right = b
}
}
class Graph {
constructor(root_node) {
this.root_node = root_node
this.dfs_visited = new Set();
}
dfs(starting_node) {
if(!starting_node) starting_node = this.root_node
let node = starting_node
console.log(node.value);
this.dfs_visited.add(node);
node.links.forEach( neighbour => {
if (!this.dfs_visited.has(neighbour.right)) {
this.dfs(neighbour.right);
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment