Skip to content

Commit 326e7dc

Browse files
Rename variables and types to Trie terminology
1 parent 032641c commit 326e7dc

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/utils/string.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { keys, map, partition, uniq } from 'lodash';
22

33
type Char = string;
4-
interface ICharTree extends Record<Char, CharLeaf> {}
5-
type CharLeaf = ICharTree;
4+
interface ICharTrie extends Record<Char, CharNode> {}
5+
type CharNode = ICharTrie;
66

7-
const terminator: ICharTree = {};
7+
const leafNode: CharNode = {};
88

99
/**
10-
* Arrange a head character and its suffixes into a tree.
11-
* Flatten leaves of the character tree containing a single letter.
10+
* Arrange a head character and its suffixes into a trie.
11+
* Flatten leaves of the character trie containing a single letter.
1212
* For example, { f: { o: { o: { '': {}, bar: {} } } } } flattens
1313
* to { foo: { '': {}, bar: {} } }
1414
*
1515
* @param headChar A character prefix
16-
* @param tailGroup A character tree of suffixes to headChar
17-
* @returns A character tree with tailGroup branching from headChar
16+
* @param tailGroup A character trie of suffixes to headChar
17+
* @returns A character trie with tailGroup branching from headChar
1818
*/
19-
function mergeGroups(headChar: Char, tailGroup: ICharTree): ICharTree {
19+
function mergeGroups(headChar: Char, tailGroup: ICharTrie): ICharTrie {
2020
const tails = keys(tailGroup);
2121
if (tails.length > 1) {
2222
return { [headChar]: tailGroup };
@@ -27,26 +27,26 @@ function mergeGroups(headChar: Char, tailGroup: ICharTree): ICharTree {
2727
}
2828

2929
/**
30-
* Parse a list of words to build a tree of common prefixes
30+
* Parse a list of words to build a trie of common prefixes.
3131
*
3232
* @param words A list of words to parse
33-
* @returns A tree of words grouped by the initial characters they share
33+
* @returns A trie of words grouped by the initial characters they share
3434
*/
35-
function groupUniqueByCommonHead(words: string[]): ICharTree {
35+
function groupUniqueByCommonHead(words: string[]): ICharTrie {
3636
if (words.length === 0) {
37-
return terminator;
37+
return leafNode;
3838
}
3939

4040
const wordToMatch = words[0];
4141

4242
if (wordToMatch === '') {
4343
// End of the target word reached. Include an empty string to signify that
44-
// a word ends at this spot, and group any remaining words in the tree.
44+
// a word ends at this spot, and group any remaining words in the trie.
4545
const [, nonEmptyWords] = partition(words, word => word === '');
46-
return { '': terminator, ...groupByCommonHead(nonEmptyWords) };
46+
return { '': leafNode, ...groupByCommonHead(nonEmptyWords) };
4747
}
4848

49-
// Begin a new tree containing all words starting with the same letter as wordToMatch
49+
// Begin a new trie containing all words starting with the same letter as wordToMatch
5050
const charToMatch = wordToMatch[0];
5151
const [wordsMatched, wordsMissed] = partition(words, ['[0]', charToMatch]);
5252

@@ -59,7 +59,7 @@ function groupUniqueByCommonHead(words: string[]): ICharTree {
5959
}
6060

6161
/** @borrows groupUniqueByCommonHead as groupByCommonHead */
62-
export function groupByCommonHead(words: string[]): ICharTree {
62+
export function groupByCommonHead(words: string[]): ICharTrie {
6363
const uniqueWords = uniq(words);
6464
return groupUniqueByCommonHead(uniqueWords);
6565
}

0 commit comments

Comments
 (0)