Created
July 25, 2020 11:34
-
-
Save deleteman/773a394f70e435da3694e2baff451e3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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