{"id":1070,"date":"2025-09-01T22:43:12","date_gmt":"2025-09-01T17:13:12","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=1070"},"modified":"2025-09-01T22:43:13","modified_gmt":"2025-09-01T17:13:13","slug":"implement-trie-prefix-tree","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/implement-trie-prefix-tree\/","title":{"rendered":"Implement Trie (Prefix Tree) | Leetcode 208 | Efficient String Data Structure"},"content":{"rendered":"\n<p>The <strong>Implement Trie (Prefix Tree)<\/strong> problem asks us to design a Trie data structure that supports the following operations:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Insert(word):<\/strong> Insert a word into the Trie.<\/li>\n\n\n\n<li><strong>Search(word):<\/strong> Return <code>True<\/code> if the word is in the Trie, <code>False<\/code> otherwise.<\/li>\n\n\n\n<li><strong>startsWith(prefix):<\/strong> Return <code>True<\/code> if there is any word in the Trie that starts with the given prefix.<\/li>\n<\/ol>\n\n\n\n<p>A <strong>Trie (Prefix Tree)<\/strong> is a tree-like data structure that efficiently stores and retrieves strings, especially useful for prefix-based searches.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/implement-trie-prefix-tree\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Examples<\/h3>\n\n\n\n<p><strong>Example 1<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Input:\nTrie trie = new Trie();\ntrie.insert(&quot;apple&quot;);\ntrie.search(&quot;apple&quot;);   \/\/ returns True\ntrie.search(&quot;app&quot;);     \/\/ returns False\ntrie.startsWith(&quot;app&quot;); \/\/ returns True\ntrie.insert(&quot;app&quot;);\ntrie.search(&quot;app&quot;);     \/\/ returns True\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Input:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Trie trie = new Trie()<\/span><span style=\"color: #F44747\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">trie.insert(<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><span style=\"color: #F44747\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">trie.search(<\/span><span style=\"color: #CE9178\">&quot;apple&quot;<\/span><span style=\"color: #D4D4D4\">);   \/\/ returns <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">trie.search(<\/span><span style=\"color: #CE9178\">&quot;app&quot;<\/span><span style=\"color: #D4D4D4\">);     \/\/ returns <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">trie.startsWith(<\/span><span style=\"color: #CE9178\">&quot;app&quot;<\/span><span style=\"color: #D4D4D4\">); \/\/ returns <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">trie.insert(<\/span><span style=\"color: #CE9178\">&quot;app&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><span style=\"color: #F44747\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">trie.search(<\/span><span style=\"color: #CE9178\">&quot;app&quot;<\/span><span style=\"color: #D4D4D4\">);     \/\/ returns <\/span><span style=\"color: #569CD6\">True<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Intuition and Approach<\/h2>\n\n\n\n<p>The Trie is built using nodes where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each node represents a character.<\/li>\n\n\n\n<li>Each node can have multiple children (for each possible next character).<\/li>\n\n\n\n<li>A special flag <code>is_end_of_word<\/code> marks whether a complete word ends at that node.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Steps:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Insert(word):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Start from the root.<\/li>\n\n\n\n<li>For each character in the word:\n<ul class=\"wp-block-list\">\n<li>If the character does not exist in the current node\u2019s children, create a new node.<\/li>\n\n\n\n<li>Move to that child node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>After processing all characters, mark the last node as <code>is_end_of_word = True<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Search(word):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Start from the root.<\/li>\n\n\n\n<li>Traverse character by character.<\/li>\n\n\n\n<li>If at any point the character doesn\u2019t exist, return <code>False<\/code>.<\/li>\n\n\n\n<li>At the end, return <code>True<\/code> only if the final node is marked as end of a word.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>startsWith(prefix):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Similar to <code>search<\/code>, but we do not check the <code>is_end_of_word<\/code> flag.<\/li>\n\n\n\n<li>If all characters in the prefix exist, return <code>True<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Implementation<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class TrieNode:\n    def __init__(self):\n        # Each node holds up to 26 children (a\u2013z), using a dict for flexibility\n        self.children = {}\n        # Flag to mark the end of a complete word\n        self.is_end_of_word = False\n\nclass Trie:\n    def __init__(self):\n        # Root node doesn't contain any character itself\n        self.root = TrieNode()\n\n    def insert(self, word: str) -&gt; None:\n        node = self.root\n        for char in word:\n            # Create a new node if this character path doesn't exist\n            if char not in node.children:\n                node.children[char] = TrieNode()\n            node = node.children[char]\n        # Mark the final node as the end of a word\n        node.is_end_of_word = True\n\n    def search(self, word: str) -&gt; bool:\n        node = self.root\n        for char in word:\n            if char not in node.children:\n                return False\n            node = node.children[char]\n        # Return True only if the complete word exists (i.e., is marked as ended here)\n        return node.is_end_of_word\n\n    def startsWith(self, prefix: str) -&gt; bool:\n        node = self.root\n        for char in prefix:\n            if char not in node.children:\n                return False\n            node = node.children[char]\n        # If traversal succeeds, the prefix exists\n        return True\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">TrieNode<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">__init__<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Each node holds up to 26 children (a\u2013z), using a dict for flexibility<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.children = {}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Flag to mark the end of a complete word<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.is_end_of_word = <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Trie<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">__init__<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Root node doesn&#39;t contain any character itself<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.root = TrieNode()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">insert<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">word<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.root<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> char <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> word:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Create a new node if this character path doesn&#39;t exist<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> char <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> node.children:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                node.children[char] = TrieNode()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node = node.children[char]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Mark the final node as the end of a word<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node.is_end_of_word = <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">search<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">word<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #4EC9B0\">bool<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.root<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> char <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> word:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> char <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> node.children:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node = node.children[char]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Return True only if the complete word exists (i.e., is marked as ended here)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> node.is_end_of_word<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">startsWith<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">prefix<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">) -&gt; <\/span><span style=\"color: #4EC9B0\">bool<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        node = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.root<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> char <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> prefix:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> char <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> node.children:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            node = node.children[char]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># If traversal succeeds, the prefix exists<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Explanation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TrieNode class:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Stores children in a dictionary.<\/li>\n\n\n\n<li>Uses <code>is_end_of_word<\/code> to mark if a word ends at this node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Insert method:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Builds the path of characters.<\/li>\n\n\n\n<li>Creates new nodes when required.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Search method:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Traverses character by character.<\/li>\n\n\n\n<li>Ensures the word ends at a valid terminal node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>startsWith method:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Traverses only the prefix.<\/li>\n\n\n\n<li>Does not require the end node to be marked.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Dry Run<\/h2>\n\n\n\n<p>Insert <code>\"apple\"<\/code> into the Trie:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Root \u2192 <code>'a'<\/code> \u2192 <code>'p'<\/code> \u2192 <code>'p'<\/code> \u2192 <code>'l'<\/code> \u2192 <code>'e'<\/code><\/li>\n\n\n\n<li>Mark node <code>'e'<\/code> as end of word.<\/li>\n<\/ul>\n\n\n\n<p>Now:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>search(\"apple\")<\/code> \u2192 True<\/li>\n\n\n\n<li><code>search(\"app\")<\/code> \u2192 False (prefix exists but not marked as word)<\/li>\n\n\n\n<li><code>startsWith(\"app\")<\/code> \u2192 True<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Time and Space Complexity<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Insert:<\/strong> O(L) where L = length of word.<\/li>\n\n\n\n<li><strong>Search:<\/strong> O(L) where L = length of word.<\/li>\n\n\n\n<li><strong>startsWith:<\/strong> O(P) where P = length of prefix.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Each inserted character may create a new node.<\/li>\n\n\n\n<li>Worst case = O(N * L) where N = number of words, L = average word length.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <strong>Implement Trie (Prefix Tree)<\/strong> problem demonstrates how to build and use a Trie for efficient prefix-based searching.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Supports <code>insert<\/code>, <code>search<\/code>, and <code>startsWith<\/code> operations.<\/li>\n\n\n\n<li>Each operation runs in linear time relative to the length of the input word\/prefix.<\/li>\n\n\n\n<li>Widely used in applications like autocomplete, spell checking, and dictionary lookups.<\/li>\n<\/ul>\n\n\n\n<p>This solution is both <strong>optimal and scalable<\/strong>, making it a must-know data structure for coding interviews.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Implement Trie (Prefix Tree) problem asks us to design a Trie data structure that supports the following operations: A Trie (Prefix Tree) is a tree-like data structure that efficiently stores and retrieves strings, especially useful for prefix-based searches. Here&#8217;s the [Problem Link] to begin with. Examples Example 1 Intuition and Approach The Trie is<\/p>\n","protected":false},"author":1,"featured_media":1071,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[19,44],"class_list":{"0":"post-1070","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-medium","10":"tag-tries"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/09\/implement-trie-Prefix-Tree-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/1070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=1070"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/1070\/revisions"}],"predecessor-version":[{"id":1073,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/1070\/revisions\/1073"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/1071"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=1070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=1070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=1070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}