Trie: K-ary search tree
Implementing the trie data structure in typescript
I am software developer, primarily working on the nodejs, graphql, react and mongoDB.
Search for a command to run...
Implementing the trie data structure in typescript
I am software developer, primarily working on the nodejs, graphql, react and mongoDB.
No comments yet. Be the first to comment.
Last time we built a connection pool from scratch. This time: how does a database not lose your data when the power dies? You write a row. The database says OK. A millisecond later, someone trips ove

Nothing is magic — part of a series on building infrastructure primitives from scratch. I used to think request queuing and connection pooling were deep infrastructure magic — something libraries did
A practical guide to pgrx with a real-world data masking example

Why Rust became my favorite language — and how Corrode’s article taught me to enjoy the messy first draft.

A little over a year ago, I got curious about the 1 Billion Row Challenge (1BRC). It seemed like the perfect playground to test Rust’s performance chops — 1 billion weather station measurements, aggregate per-city statistics (min, max, average), and ...
Trie is a typeof k-ary search tree used for storing and searching a specific key from a set.
Trie helps us to find the key in O(length of the key) time.
Check out the code example: Github: Trie implementation link
The trie will be made of trie nodes, which will have three fields. Key, children and isWord
key will store the character.children will be the map that will keep track of the child trie node on the basis of a key character.isWord will be a boolean field. which will mark if the node is the word or not.// TrieNode.ts
type childrenType = {
[key: string]: any
}
export class TrieNode {
key: string | null;
children: chilrenType;
isWord: boolean;
constructor(key: string | null) {
this.key = key;
this.children = {};
this.isWord = false;
}
}
key value of null and every Trie Node will have two properties of children: an empty object and isWord as a false boolean field.null while instantiating the TrieNode for the root node. // Trie.ts
import { TrieNode } from "./TrieNode";
export class Trie {
root: TrieNode;
constructor() {
this.root = new TrieNode(null);
}
...
}
The insertWord function will be responsible for inserting the word in the trie.
// Trie.ts
...
insertWord(word: string) {
let node = this.root;
const n = word.length;
for (let i = 0; i < n; i++) {
const char = word[i];
// set children
if (!node.children[char]) {
node.children[char] = new TrieNode(char);
}
// move node forward
node = node.children[char];
// set last char as word
if (i === n - 1) {
node.isWord = true;
}
}
}
...
The insertWord function adds the word, character by character, in the trie. If the node with the key is not found in the trie at the particular position, then we create the new trie node.
In the end, when we reach the last character, we mark that node, isWord field as true.
The contains function is responsible for checking if the word exists the trie. It will search for the word which we want to check.
// Trie.ts
...
contains(word: string): boolean {
let node = this.root;
let n = word.length;
for (let i = 0; i < n; i++) {
const char = word.charAt(i);
if (node.children[char]) {
node = node.children[char];
} else {
return false;
}
}
return node.isWord;
}
...
type childrenType = {
[key: string]: any
}
export class TrieNode {
key: string | null;
children: chilrenType;
isWord: boolean;
constructor(key: string | null) {
this.key = key;
this.children = {};
this.isWord = false;
}
}
export class Trie {
root: TrieNode;
constructor() {
this.root = new TrieNode(null);
}
insertWord(word: string) {
let node = this.root;
const n = word.length;
for (let i = 0; i < n; i++) {
const char = word[i];
// set children
if (!node.children[char]) {
node.children[char] = new TrieNode(char);
}
// move node forward
node = node.children[char];
// set last char as word
if (i === n - 1) {
node.isWord = true;
}
}
}
contains(word: string): boolean {
let node = this.root;
let n = word.length;
for (let i = 0; i < n; i++) {
const char = word.charAt(i);
if (node.children[char]) {
node = node.children[char];
} else {
return false;
}
}
return node.isWord;
}
}
Check out the code example: Github: Trie implementation link