Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

keshav Sandhu
keshav Sandhu

Posted on

     

Introduction to Trie (Prefix Tree)

ATrie is a tree-like data structure that is used for efficiently storing and searching strings, especially in applications like autocomplete, spell checking, and IP routing.


Key Properties of a Trie:

  1. Nodes: Each node represents a character.
  2. Root: The root node is empty and serves as the starting point.
  3. Children: Each node has up to 26 children (for lowercase English letters) or more, depending on the character set.
  4. End-of-Word Marker: Some nodes are marked to indicate the end of a word.

Basic Trie Operations:

1.Insertion

Inserting a word involves traversing the trie and creating new nodes for characters that don’t exist.

2.Search

Search checks whether a word exists in the trie by traversing its nodes.

3.Prefix Search

This checks whether any word in the trie starts with a given prefix.


Implementation of Basic Trie in #"apple");console.log(trie.search("apple"));// trueconsole.log(trie.search("app"));// falseconsole.log(trie.startsWith("app"));// truetrie.insert("app");console.log(trie.search("app"));// true
Enter fullscreen modeExit fullscreen mode


Advanced Trie Operations:

1.Delete a Word:

Deleting a word involves a recursive approach, where we remove nodes that are no longer needed.

delete(word,node=this.root,depth=0){if(depth===word.length){if(!node.isEndOfWord)returnfalse;// Word doesn't existnode.isEndOfWord=false;returnObject.keys(node.children).length===0;// Check if node has children}constchar=word[depth];if(!node.children[char])returnfalse;constshouldDeleteChild=this.delete(word,node.children[char],depth+1);if(shouldDeleteChild){deletenode.children[char];returnObject.keys(node.children).length===0&&!node.isEndOfWord;}returnfalse;}
Enter fullscreen modeExit fullscreen mode

2.Count Words with a Prefix:

Count how many words start with a given prefix.

countWordsWithPrefix(prefix){letnode=this.root;for(letcharofprefix){if(!node.children[char])return0;node=node.children[char];}returnthis._countWords(node);}_countWords(node){letcount=node.isEndOfWord?1:0;for(letcharinnode.children){count+=this._countWords(node.children[char]);}returncount;}
Enter fullscreen modeExit fullscreen mode

3.Autocomplete Suggestions:

Given a prefix, return all words that start with it.

getWordsWithPrefix(prefix){letnode=this.root;for(letcharofprefix){if(!node.children[char])return[];node=node.children[char];}returnthis._collectWords(node,prefix);}_collectWords(node,prefix){letresults=[];if(node.isEndOfWord)results.push(prefix);for(letcharinnode.children){results=results.concat(this._collectWords(node.children[char],prefix+char));}returnresults;}
Enter fullscreen modeExit fullscreen mode

Time Complexity:

  1. Insert: O(L) (L = length of the word)
  2. Search: O(L)
  3. Prefix Search: O(L)
  4. Delete: O(L)

Applications of Trie:

  1. Autocomplete Systems (e.g., search bars, text editors).
  2. Spell Checkers.
  3. IP Routing (longest prefix matching).
  4. Word Games (e.g., Boggle).
  5. DNA Sequence Matching.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

keshav Sandhu
👋 Hi there! I'm a passionate developer with a love for solving challenging problems through code. I enjoy exploring new algorithms, optimizing solutions, and continuously learning about new techs.
  • Joined

More fromkeshav Sandhu

Resume-worthy project ideas for Frontend Devs
#frontend#webdev#programming#javascript
null vs undefined in #"/keshav___dev/learn-about-memory-consumption-optimization-on-server-side-4ifo"> Learn about MEMORY CONSUMPTION optimization on server side! 💾
#webdev#programming#linux#backend
DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp