Skip to content

Commit

Permalink
235th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 21, 2024
1 parent 5f81f74 commit e18bdb5
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ $ pnpm test twoSum.test.ts
- [樹 (Tree)](./algorithms/tree/README.md)
- [堆積 (Heap)](./algorithms/heap/README.md)
- [圖 (Graph)](./algorithms/graph/README.md)
- 字典樹 (Trie)
- [字典樹 (Trie)](./algorithms/trie/README.md)
- 排序 (Sorting)
- [搜尋 (Searching)](./algorithms/searching/README.md)
- 分治 (Divide and Conquer)
- 回溯 (Backtracking)
- [動態規劃 (Dynamic Programming)](./algorithms/dynamic-programming/README.md)
- 貪婪 (Greedy)
- 回溯 (Backtracking)
- 分治 (Divide and Conquer)
- 位元操作 (Bit Manipulation)

## Basic - LeetCode 75
Expand Down
78 changes: 78 additions & 0 deletions algorithms/trie/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 字典樹 (Trie)

```ts
class TrieNode {
children: Map<string, TrieNode>;
isEndOfWord: boolean;

constructor() {
this.children = new Map();
this.isEndOfWord = false;
}
}
```

```ts
class Trie {
root: TrieNode;

constructor() {
this.root = new TrieNode();
}

// 插入字詞
insert(word: string): void {
let current = this.root;

for (const char of word) {
if (!current.children.has(char)) {
current.children.set(char, new TrieNode());
}

current = current.children.get(char) as TrieNode;
}

current.isEndOfWord = true;
}

// 查找字
search(word: string): boolean {
let current = this.root;

for (const char of word) {
if (!current.children.has(char)) return false;
current = current.children.get(char) as TrieNode;
}

return current.isEndOfWord;
}

// 查找是否存在某個前綴
startsWith(prefix: string): boolean {
let current = this.root;

for (const char of prefix) {
if (!current.children.has(char)) return false;
current = current.children.get(char) as TrieNode;
}

return true;
}
}
```

```ts
const trie = new Trie();

trie.insert('apple');
trie.insert('banana');

console.log(trie.search('apple')); // true
console.log(trie.search('app')); // false
console.log(trie.search('banana')); // true

console.log(trie.startsWith('app')); // true

trie.insert('app');
console.log(trie.search('app')); // true
```
48 changes: 48 additions & 0 deletions algorithms/trie/Trie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { TrieNode } from './TrieNode';

export class Trie {
root: TrieNode;

constructor() {
this.root = new TrieNode();
}

// 插入字詞
insert(word: string): void {
let current = this.root;

for (const char of word) {
if (!current.children.has(char)) {
current.children.set(char, new TrieNode());
}

current = current.children.get(char) as TrieNode;
}

current.isEndOfWord = true;
}

// 查找字
search(word: string): boolean {
let current = this.root;

for (const char of word) {
if (!current.children.has(char)) return false;
current = current.children.get(char) as TrieNode;
}

return current.isEndOfWord;
}

// 查找是否存在某個前綴
startsWith(prefix: string): boolean {
let current = this.root;

for (const char of prefix) {
if (!current.children.has(char)) return false;
current = current.children.get(char) as TrieNode;
}

return true;
}
}
9 changes: 9 additions & 0 deletions algorithms/trie/TrieNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class TrieNode {
children: Map<string, TrieNode>;
isEndOfWord: boolean;

constructor() {
this.children = new Map();
this.isEndOfWord = false;
}
}

0 comments on commit e18bdb5

Please sign in to comment.