diff --git a/book/content/part02/hash-map.asc b/book/content/part02/hash-map.asc index dcdf6a20..8f239cba 100644 --- a/book/content/part02/hash-map.asc +++ b/book/content/part02/hash-map.asc @@ -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') ---- diff --git a/book/content/part03/binary-search-tree.asc b/book/content/part03/binary-search-tree.asc index db6fcfce..312421fa 100644 --- a/book/content/part03/binary-search-tree.asc +++ b/book/content/part03/binary-search-tree.asc @@ -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] diff --git a/book/content/part03/graph.asc b/book/content/part03/graph.asc index cceacd24..3e2fff43 100644 --- a/book/content/part03/graph.asc +++ b/book/content/part03/graph.asc @@ -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[] diff --git a/book/interview-questions/longest-substring-without-repeating-characters.spec.js b/book/interview-questions/longest-substring-without-repeating-characters.spec.js index 7011e0d4..91df0b9a 100644 --- a/book/interview-questions/longest-substring-without-repeating-characters.spec.js +++ b/book/interview-questions/longest-substring-without-repeating-characters.spec.js @@ -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); + }); }); }); diff --git a/src/data-structures/graphs/graph.js b/src/data-structures/graphs/graph.js index 80fea526..1e01f886 100644 --- a/src/data-structures/graphs/graph.js +++ b/src/data-structures/graphs/graph.js @@ -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) { @@ -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 @@ -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 @@ -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 */