LeetCode 208: Implementing Trie (Prefix Tree) - C# Implementation

Because to understand the red dot system to understand the prefix tree, here is a concise article reproduced


topic:

Implement a Trie (prefix tree) that includes three operations: insert, search, and startsWith.

Example:

Trie trie = new Trie();
 
trie.insert("apple");
trie.search("apple");   // 返回 true
trie.search("app");     // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");   
trie.search("app");     // 返回 true

illustrate:

  • You can assume that all input consists of lowercase letters az.
  • All inputs are guaranteed to be non-empty strings.

I didn't hear about a prefix tree at first. I thought this problem was very simple. I implemented it directly using the string function. I found that for a large number of string processing, the processing performance of the string encapsulation function was completely insufficient. Later, Baidu learned that the original prefix tree is A noun, please refer to the link for a detailed explanation: trie tree (prefix tree) This is explained very clearly.

After reading the explanation of the prefix tree, and then reading the comments of everyone on LeetCode, the prefix tree implemented by C# is given.

执行用时 : 344 ms,Implement Trie (Prefix Tree)的C#提交中击败了67.74% 的用户
 
内存消耗 : 46.8 MB,Implement Trie (Prefix Tree)的C#提交中击败了22.22% 的用户
class Trie
    {
    
    
        private class Node
        {
    
    
            public Node[] children;
            public bool isEnd = false;
            public Node()
            {
    
    
                children = new Node[26];
                isEnd = false;
            }
        }
 
        private Node rootNode = null;
        /** Initialize your data structure here. */
        public Trie()
        {
    
    
            rootNode = new Node();
        }
 
        /** Inserts a word into the trie. */
        public void Insert(String word)
        {
    
    
            Node currNode = rootNode;
            for (int i = 0; i < word.Length; i++)
            {
    
    
                if (currNode.children[word[i] - 'a'] == null)
                {
    
    
                    currNode.children[word[i] - 'a'] = new Node();
                }
                currNode = currNode.children[word[i] - 'a'];
            }
            currNode.isEnd = true;                    
        }
 
        /** Returns if the word is in the trie. */
        public bool Search(String word)
        {
    
    
            Node currNode = rootNode;
            for (int i = 0; i < word.Length; i++)
            {
    
    
                if (currNode.children[word[i] - 'a'] == null)
                {
    
    
                    return false;
                }
                currNode = currNode.children[word[i] - 'a'];
            }
            return currNode.isEnd;          
        }
 
        /** Returns if there is any word in the trie that starts with the given prefix. */
        public bool StartsWith(String prefix)
        {
    
    
            Node currNode = rootNode;
            for (int i = 0; i < prefix.Length; i++)
            {
    
    
                if (currNode.children[prefix[i] - 'a'] == null)
                {
    
    
                    return false;
                }
                currNode = currNode.children[prefix[i] - 'a'];
            }
            return true;
        }
    }

おすすめ

転載: blog.csdn.net/weixin_43149049/article/details/122839609