New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Graph nodes store adjacents in HashSet object which does not exist #102
Comments
Hi Aït-Kaci! Glad to hear you like the content and thanks for taking the time to report a possible error. Could you please paste the code here where you find the error? or even better could you please create a pull request and write a test case either in graph.spec.js or node.spec.js that captures the bug? |
Hi, const HashSet = require('../sets/hash-set');
// tag::constructor[]
/**
* Graph node/vertex that hold adjacencies nodes
* For performance, uses a HashSet instead of array for adjacents.
*/
class Node {
constructor(value) {
this.value = value;
this.adjacents = new HashSet(); // adjacency list
}
// end::constructor[]
// tag::addAdjacent[]
/**
* Add node to adjacency list
* Runtime: O(1)
* @param {Node} node
*/
addAdjacent(node) {
this.adjacents.add(node);
}
// end::addAdjacent[]
// tag::removeAdjacent[]
/**
* Remove node from adjacency list
* Runtime: O(1)
* @param {Node} node
* @returns removed node or `false` if node was not found
*/
removeAdjacent(node) {
return this.adjacents.delete(node);
}
// end::removeAdjacent[]
// tag::isAdjacent[]
/**
* Check if a Node is adjacent to other
* Runtime: O(1)
* @param {Node} node
*/
isAdjacent(node) {
return this.adjacents.has(node);
}
// end::isAdjacent[]
/**
* Get all adjacent nodes
*/
getAdjacents() {
return Array.from(this.adjacents);
}
// tag::constructor[]
}
// end::constructor[]
module.exports = Node; |
Oh, that's the code from node.js. Sorry, I thought you found an issue with some experimental code using it. So about your question,
In dsa.js, HashSet or HashMapSet is the same. The only different set is the TreeSet. Let me know if you have more questions about it. |
Hi
Great content.
I am experimenting with it.
In : dsa.js-data-structures-algorithms-javascript/src/data-structures/graphs/node.js
the constructor uses
HashSet
where it should beHashMapSet
if I am not mistaken.The text was updated successfully, but these errors were encountered: