Skip to content

Commit

Permalink
Merge pull request #98 from amejiarosario/fix/sam-findings
Browse files Browse the repository at this point in the history
Fix/sam findings
  • Loading branch information
amejiarosario authored Dec 22, 2020
2 parents 65b6edd + 436848d commit 4350fca
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion book/content/part02/hash-map.asc
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ function longestSubstring(s) {
.Examples
[source, javascript]
----
longestSubstring('abcdaefg'); // 4 ('abcd' or 'aefg')
longestSubstring('abcdaefg'); // 7 ('bcdaefg')
longestSubstring('abbaa'); // 2 ('ab')
longestSubstring('abbadvdf') // 4 ('badv')
----
Expand Down
13 changes: 8 additions & 5 deletions book/content/part03/binary-search-tree.asc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ With the methods `add` and `remove`, we have to guarantee that our tree always h
===== Inserting new elements in a BST

.For inserting an element in a BST, we have two scenarios:
1. If the tree is empty (root element is null), we add the newly created node as root, and that's it!
2. If the root is not null. Start from it and compare the node’s value against the new element. If the node has higher than a new item, we move to the right child, otherwise to the left. We check each node recursively until we find an empty spot to put the new element and keep the rule `right < parent < left`.
3. If we insert the same value multiple times, we don’t want duplicates. So, we can keep track of multiples using a duplicity counter.

For instance, let’s say that we want to insert the values 19, 21, 10, 2, 8 in a BST:
. If the tree is empty (root element is null), we add the newly created node as root, and that's it!
. If the tree has a root, compare the new value with the root. Then we have three possibilities:
.. `root == newValue`: we increase the duplicity counter in that case, and done!
.. `root > newValue`, we search on the left side of the root.
.. `root < newValue`, we search on the right side of the root.
. Repeat the comparison between the current node and `newValue`, until we find the value or (null) space.

For instance, let’s say that we want to insert the values 19, 21, 10, 2, 18 in a BST:

.Inserting values on a BST.
image::image36.png[image,width=528,height=329]
Expand Down
4 changes: 2 additions & 2 deletions book/content/part03/graph.asc
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ include::{codedir}/data-structures/graphs/node.js[tag=removeAdjacent, indent=0]
.2+.^s| Data Structure 2+^s| Vertices 2+^s| Edges .2+^.^s| Space Complexity
^|_Add_ ^|_Remove_ ^|_Add_ ^|_Remove_
| Graph (adj. matrix) ^| O(\|V\|^2^) ^| O(\|V\|^2^) ^|O(1) ^|O(1) ^|O(\|V\|^2^)
| Graph (adj. list w/array) ^| O(1) ^| O(\|V\| + \|E\|)) ^|O(1) ^|O(\|V\| + \|E\|) ^|O(\|V\| + \|E\|)
| Graph (adj. list w/HashSet) ^| O(1) ^| O(\|V\|)) ^|O(1) ^|O(\|V\|) ^|O(\|V\| + \|E\|)
| Graph (adj. list w/array) ^| O(1) ^| O(\|V\| + \|E\|)) ^|O(1) ^|O(\|E\|) ^|O(\|V\| + \|E\|)
| Graph (adj. list w/HashSet) ^| O(1) ^| O(\|V\|)) ^|O(1) ^|O(1) ^|O(\|V\| + \|E\|)
|===
// end::table[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@ const { lenLongestSubstring } = require('./longest-substring-without-repeating-c
const expected = 5;
expect(fn(actual)).toEqual(expected);
});

it('should work with example', () => {
const actual = 'abcdaefg';
const expected = 7;
expect(fn(actual)).toEqual(expected);
});
});
});
18 changes: 10 additions & 8 deletions src/data-structures/graphs/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Graph {
* Removes node from graph
* It also removes the reference of the deleted node from
* anywhere it was adjacent to.
* Runtime: O(|V| + |E|)
* Runtime: O(|V|) because adjacency list is implemented with a HashSet.
* It were implemented with an array then it would be O(|V| + |E|).
* @param {any} value node's value
*/
removeVertex(value) {
Expand All @@ -55,9 +56,9 @@ class Graph {

// tag::addEdge[]
/**
* Create a connection between source node and destination node.
* If the graph is undirected it will also create the conneciton from destination to destination.
* If the nodes doesn't exist then it will create them on the fly
* Create a connection between the source node and the destination node.
* If the graph is undirected, it will also create the link from destination to source.
* If the nodes don't exist, then it will make them on the fly.
* Runtime: O(1)
* @param {any} source
* @param {any} destination
Expand All @@ -79,10 +80,11 @@ class Graph {

// tag::removeEdge[]
/**
* Remove connection between source node and destination.
* If the graph is undirected it will also remove the conneciton from destination to destination.
* Remove the connection between source node and destination.
* If the graph is undirected, it will also create the link from destination to source.
*
* Runtime: O(|E|)
* Runtime: O(1): implemented with HashSet.
* If implemented with array, would be O(|E|).
*
* @param {any} source
* @param {any} destination
Expand All @@ -105,7 +107,7 @@ class Graph {

// tag::areAdjacents[]
/**
* True if two nodes are adjacent to each other
* True if two nodes are adjacent.
* @param {any} source node's value
* @param {any} destination node's value
*/
Expand Down

0 comments on commit 4350fca

Please sign in to comment.