Skip to content

Commit

Permalink
name changes for chapters - each exercise now owns a chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ishtms committed Sep 17, 2023
1 parent 4a89681 commit 9cd91c2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 85 deletions.
50 changes: 25 additions & 25 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,31 +255,31 @@ The repo for our backend framework- [Velocy](https://github.com/ishtms/velocy)
- [Improving the `Router` API](/chapters/ch06.3-improving-the-router-api.md)
- [The need for a `trie`](/chapters/ch06.4-the-need-for-a-trie.md)
- [What is a `Trie` anyway?](/chapters/ch06.4-the-need-for-a-trie.md#what-is-a-trie-anyway)
- [Exercise 1 - Implementing a `Trie`](/chapters/ch07.0-ex-implementing-a-trie.md#exercise---implementing-a-trie)
- [Root Node](/chapters/ch07.0-ex-implementing-a-trie.md#root-node)
- [End of the word](/chapters/ch07.0-ex-implementing-a-trie.md#end-of-the-word)
- [Challenge 1: Basic Trie with `insert` Method](/chapters/ch07.0-ex-implementing-a-trie.md#challenge-1-basic-trie-with-insert-method)
- [Requirements](/chapters/ch07.0-ex-implementing-a-trie.md#requirements)
- [More details](/chapters/ch07.0-ex-implementing-a-trie.md#more-details)
- [Solution](/chapters/ch07.0-ex-implementing-a-trie.md#solution)
- [Challenge 2: Implement `search` method](/chapters/ch07.0-ex-implementing-a-trie.md#challenge-2-implement-search-method)
- [Requirements](/chapters/ch07.0-ex-implementing-a-trie.md#requirements-1)
- [More details](/chapters/ch07.0-ex-implementing-a-trie.md#more-details-1)
- [Hints](/chapters/ch07.0-ex-implementing-a-trie.md#hints)
- [Exercise 1 - Implementing a `Trie`](/chapters/ch07-ex-implementing-a-trie.md#exercise---implementing-a-trie)
- [Root Node](/chapters/ch07-ex-implementing-a-trie.md#root-node)
- [End of the word](/chapters/ch07-ex-implementing-a-trie.md#end-of-the-word)
- [Challenge 1: Basic Trie with `insert` Method](/chapters/ch07-ex-implementing-a-trie.md#challenge-1-basic-trie-with-insert-method)
- [Requirements](/chapters/ch07-ex-implementing-a-trie.md#requirements)
- [More details](/chapters/ch07-ex-implementing-a-trie.md#more-details)
- [Solution](/chapters/ch07-ex-implementing-a-trie.md#solution)
- [Challenge 2: Implement `search` method](/chapters/ch07-ex-implementing-a-trie.md#challenge-2-implement-search-method)
- [Requirements](/chapters/ch07-ex-implementing-a-trie.md#requirements-1)
- [More details](/chapters/ch07-ex-implementing-a-trie.md#more-details-1)
- [Hints](/chapters/ch07-ex-implementing-a-trie.md#hints)
- [Solution](#solution-1)
- [Exercise 2 - Implementing our Trie based `Router`](/chapters/ch07.1-ex-implementing-router.md)
- [Challenge 1: Implementing the `addRoute` method](/chapters/ch07.1-ex-implementing-router.md#challenge-1-implementing-the-addroute-method)
- [Requirements](/chapters/ch07.1-ex-implementing-router.md#requirements)
- [More details](/chapters/ch07.1-ex-implementing-router.md#more-details)
- [Hints](/chapters/ch07.1-ex-implementing-router.md#hints)
- [Solution](/chapters/ch07.1-ex-implementing-router.md#solution)
- [Explanation](/chapters/ch07.1-ex-implementing-router.md#explanation)
- [Challenge 2: Implementing the `findRoute` method](/chapters/ch07.1-ex-implementing-router.md#challenge-2-implementing-the-findroute-method)
- [Requirements](/chapters/ch07.1-ex-implementing-router.md#requirements-1)
- [More details](/chapters/ch07.1-ex-implementing-router.md#more-details-1)
- [Starting Boilerplate](/chapters/ch07.1-ex-implementing-router.md#starting-boilerplate)
- [Hints](/chapters/ch07.1-ex-implementing-router.md#hints-1)
- [Solution](/chapters/ch07.1-ex-implementing-router.md#solution-1)
- [Explanation](/chapters/ch07.1-ex-implementing-router.md#explanation-1)
- [Exercise 2 - Implementing our Trie based `Router`](/chapters/ch08-ex-implementing-router.md)
- [Challenge 1: Implementing the `addRoute` method](/chapters/ch08-ex-implementing-router.md#challenge-1-implementing-the-addroute-method)
- [Requirements](/chapters/ch08-ex-implementing-router.md#requirements)
- [More details](/chapters/ch08-ex-implementing-router.md#more-details)
- [Hints](/chapters/ch08-ex-implementing-router.md#hints)
- [Solution](/chapters/ch08-ex-implementing-router.md#solution)
- [Explanation](/chapters/ch08-ex-implementing-router.md#explanation)
- [Challenge 2: Implementing the `findRoute` method](/chapters/ch08-ex-implementing-router.md#challenge-2-implementing-the-findroute-method)
- [Requirements](/chapters/ch08-ex-implementing-router.md#requirements-1)
- [More details](/chapters/ch08-ex-implementing-router.md#more-details-1)
- [Starting Boilerplate](/chapters/ch08-ex-implementing-router.md#starting-boilerplate)
- [Hints](/chapters/ch08-ex-implementing-router.md#hints-1)
- [Solution](/chapters/ch08-ex-implementing-router.md#solution-1)
- [Explanation](/chapters/ch08-ex-implementing-router.md#explanation-1)

![](https://uddrapi.com/api/img?page=readme)
2 changes: 1 addition & 1 deletion chapters/ch06.4-the-need-for-a-trie.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ Every node, including `root` will be an object that contain some necessary infor

Enough with the theory. In the next chapter, we'll dive into our very first exercise for this book: **implementing a Trie**.

[![Read Next](/assets/imgs/next.png)](/chapters/ch07.0-ex-implementing-a-trie.md)
[![Read Next](/assets/imgs/next.png)](/chapters/ch07-ex-implementing-a-trie.md)

![](https://uddrapi.com/api/img?page=ch6.4)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Exercises - Implementing a `Trie`

> This exercise will motivate you to work on implementing your solution independently. Once you have completed the exercise, you can move on to the next challenge or read the solution to find a different approach.
>
>
> In these exercises, we are not focusing on performance, so it's important to focus on making your solution work correctly the first time you attempt to solve a problem.
To re-iterate, Trie (pronounced "try") is a tree-like data structure that stores a dynamic set of strings, typically used to facilitate operations like searching, insertion, and deletion. Tries are particularly useful for tasks that require quick lookups of strings with a common prefix, such as in text autocomplete or in a Router implementation to find the matching paths.
Expand All @@ -24,13 +24,13 @@ The Trie starts with a root node that doesn't hold any character. It serves as t
T O C
```

- **Level 1**: You have the characters "O", "T", and "C" branching from the root node.
- **Level 2 and Beyond**: These nodes further branch out to form the words.
- "O" branches to "K", completing the word "OK".
- "T" branches to "O", completing the word "TO".
- "C" branches to "A" and "U":
- "A" further branches to "R" for "CAR" and "T" for "CAT".
- "U" further branches to "P", completing the word "CUP".
- **Level 1**: You have the characters "O", "T", and "C" branching from the root node.
- **Level 2 and Beyond**: These nodes further branch out to form the words.
- "O" branches to "K", completing the word "OK".
- "T" branches to "O", completing the word "TO".
- "C" branches to "A" and "U":
- "A" further branches to "R" for "CAR" and "T" for "CAT".
- "U" further branches to "P", completing the word "CUP".

## End of the word

Expand Down Expand Up @@ -60,10 +60,10 @@ In this first challenge, your task is to implement a Trie data structure with on
2. **Traversal**: For each character in the word you want to insert, you'll traverse the Trie from the root node, going as far down as the current character sequence allows.

3. **Node Creation**: If a character in the word doesn't match any child node of the current node:
- Create a new node for that character.
- Link this new node to the current one.
- Move down to this new node and continue with the next character in the word.

- Create a new node for that character.
- Link this new node to the current one.
- Move down to this new node and continue with the next character in the word.

4. **End-of-Word**: When you've inserted all the characters for a particular word, mark the last node in some way to indicate that it's the end of a valid word. This could be a boolean property in the node object, for example.

Expand Down Expand Up @@ -166,8 +166,8 @@ class TrieNode {

Initializes an instance of the `TrieNode` class. A TrieNode has two properties:

- `isEndOfWord`: A boolean flag that denotes whether the node is the last character of a word in the Trie. Initially set to `false`.
- `children`: A Map to store the children nodes. The keys are letters, and the values are TrieNode objects.
- `isEndOfWord`: A boolean flag that denotes whether the node is the last character of a word in the Trie. Initially set to `false`.
- `children`: A Map to store the children nodes. The keys are letters, and the values are TrieNode objects.

```js
add(letter, _isLastCharacter) {
Expand Down Expand Up @@ -253,16 +253,16 @@ If you are having trouble or are stuck, here are some hints to help you with the
1. **Starting Point**: Similar to the `insert` method, you'll start at the root node and traverse the Trie based on the characters in the word you're searching for.

2. **Character Check**: For each character in the word, check if there's a child node for that character from the current node you're at.
- **If Yes**: Move to that child node.
- **If No**: Return `false`, as the word can't possibly exist in the Trie.

- **If Yes**: Move to that child node.
- **If No**: Return `false`, as the word can't possibly exist in the Trie.

3. **End-of-Word Check**: If you've reached the last character of the word, check the `isEndOfWord` property of the current node. If it's `true`, the word exists in the Trie; otherwise, it doesn't.

4. **Recursion or Loop**: You can choose to implement this method either recursively or iteratively.
- **Recursion**: If you opt for recursion, you might want to include an additional parameter in the `search` method for the current node, similar to how you did it for the `insert` method.
- **Loop**: If you prefer loops, you can use a `for` loop to go through each character in the word, updating your current node as you go along.

- **Recursion**: If you opt for recursion, you might want to include an additional parameter in the `search` method for the current node, similar to how you did it for the `insert` method.
- **Loop**: If you prefer loops, you can use a `for` loop to go through each character in the word, updating your current node as you go along.

5. **Return Value**: Don't forget to return `true` or `false` to indicate whether the word exists in the Trie.

Expand Down Expand Up @@ -301,4 +301,6 @@ search(word) {

Awesome work. Now you know the basics of the `Trie` data structure and how to implement it. In the next exercise, we'll implement our `Router` from scratch! The next exercise will be more challenging and exhaustive.

[![Read Next](/assets/imgs/next.png)](/chapters/ch08-ex-implementing-router.md)

![](https://uddrapi.com/api/img?page=ch7.0_exer)
Loading

0 comments on commit 9cd91c2

Please sign in to comment.