Skip to content

Commit 70cc029

Browse files
authored
Update README.md
1 parent 030d4b6 commit 70cc029

File tree

1 file changed

+97
-4
lines changed
  • 21 - Trie Data Structure Problems/01 - Example

1 file changed

+97
-4
lines changed

21 - Trie Data Structure Problems/01 - Example/README.md

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ public:
9696
};
9797
```
9898

99-
100-
99+
---
101100
### **Insertion Operation**
102101

103102
#### **Explanation**
@@ -131,7 +130,101 @@ Insert `"cat"` into an empty Trie:
131130
2. `'a'`: Create a new node at `children[0]`.
132131
3. `'t'`: Create a new node at `children[19]` and mark it `isTerminal = true`.
133132
133+
#### Explanation of `int charIndex = word[index] - 'a';`
134+
135+
This line of code is used to compute the **index** of the current character (`word[index]`) in the `children` array of the TrieNode. It is based on the assumption that the `children` array has 26 slots (one for each lowercase English letter, from 'a' to 'z').
136+
137+
138+
139+
#### **How It Works**
140+
1. **Characters as Integers (ASCII):**
141+
- Each character in programming is internally represented by a unique integer value (its ASCII code).
142+
- For example:
143+
- `'a'` = 97
144+
- `'b'` = 98
145+
- `'c'` = 99
146+
- ... and so on.
147+
148+
2. **Subtracting `'a'`:**
149+
- The expression `word[index] - 'a'` computes the position of the character relative to `'a'`.
150+
- Example:
151+
- If `word[index] = 'a'`, then `'a' - 'a' = 0` → Index 0.
152+
- If `word[index] = 'b'`, then `'b' - 'a' = 1` → Index 1.
153+
- If `word[index] = 'z'`, then `'z' - 'a' = 25` → Index 25.
154+
155+
3. **Purpose in the Trie:**
156+
- The result (`charIndex`) is used to decide which slot of the `children` array corresponds to the current character.
157+
158+
159+
160+
#### **Why Use This Calculation?**
161+
1. **Efficient Mapping:**
162+
- It efficiently maps each character ('a' to 'z') to an index (0 to 25), which corresponds to the position in the `children` array.
163+
164+
2. **Uniform Array Indexing:**
165+
- Instead of having separate conditions or mappings for each character, this single subtraction operation provides a uniform way to compute indices.
166+
167+
3. **Space Optimization:**
168+
- The `children` array only needs 26 slots, and the subtraction ensures each character is directly mapped to the correct slot without any gaps.
169+
170+
171+
172+
#### **Example with a Word**
173+
Suppose the word is `"cat"`, and we are processing each character:
174+
175+
1. For `word[0] = 'c'`:
176+
- `'c' - 'a' = 99 - 97 = 2`
177+
- `charIndex = 2`
178+
- Use `children[2]` to store or look for `'c'`.
179+
180+
2. For `word[1] = 'a'`:
181+
- `'a' - 'a' = 97 - 97 = 0`
182+
- `charIndex = 0`
183+
- Use `children[0]` for `'a'`.
134184
185+
3. For `word[2] = 't'`:
186+
- `'t' - 'a' = 116 - 97 = 19`
187+
- `charIndex = 19`
188+
- Use `children[19]` for `'t'`.
189+
190+
191+
192+
#### **Common Questions**
193+
1. **What if the input contains uppercase letters?**
194+
- Uppercase letters (`'A'` to `'Z'`) have different ASCII values (e.g., `'A'` = 65).
195+
- To handle uppercase, you can convert the character to lowercase first using `tolower()`.
196+
197+
```cpp
198+
char lowerChar = tolower(word[index]);
199+
int charIndex = lowerChar - 'a';
200+
```
201+
202+
2. **What about non-alphabetic characters?**
203+
- Subtracting `'a'` for non-alphabetic characters (e.g., digits, symbols) may result in invalid indices.
204+
- Ensure the input is sanitized to contain only valid lowercase English letters before performing this calculation.
205+
206+
207+
208+
#### **Visualization**
209+
For the word `"cat"`, here’s how it maps to the Trie:
210+
211+
```
212+
Character ASCII Value Index in Trie
213+
'c' 99 2
214+
'a' 97 0
215+
't' 116 19
216+
```
217+
218+
In the TrieNode's `children` array:
219+
- `children[2]` points to the node for `'c'`.
220+
- `children[0]` points to the node for `'a'`.
221+
- `children[19]` points to the node for `'t'`.
222+
223+
224+
225+
This approach ensures fast and direct access to each character's corresponding node, making Trie operations efficient.
226+
227+
---
135228

136229
### **Search Operation**
137230

@@ -164,7 +257,7 @@ Search for `"cat"`:
164257
2. Check `isTerminal` at `'t'`. If `true`, the word exists.
165258
166259
167-
260+
---
168261
### **Deletion Operation**
169262
170263
#### **Explanation**
@@ -218,7 +311,7 @@ Delete `"cat"`:
218311
3. Repeat for `'a'` and `'c'`.
219312

220313

221-
314+
---
222315
### **Complexity Analysis**
223316

224317
| Operation | Time Complexity | Space Complexity |

0 commit comments

Comments
 (0)