-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f81f74
commit e18bdb5
Showing
4 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |