zoukankan      html  css  js  c++  java
  • 208. Implement Trie (Prefix Tree)

    Implement a trie with insertsearch, and startsWith methods.

    Example:

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

    Note:

    • You may assume that all inputs are consist of lowercase letters a-z.
    • All inputs are guaranteed to be non-empty strings.
    class Trie {
        private int SIZE = 26;
        private TrieNode root;
        /** Initialize your data structure here. */
        public Trie() {
            root = new TrieNode();
        }
        
        private class TrieNode{
            private int num;
            private TrieNode[] son;
            private boolean isEnd;
            private char val;
            private boolean haveSon;
            
            TrieNode(){
                num = 1;
                son = new TrieNode[SIZE];
                isEnd = false;
                haveSon = false;
            }
        }
        
        /** Inserts a word into the trie. */
        public void insert(String word) {
            if(word.length() == 0 || word == null) return;
            TrieNode node = root;
            char[] letters = word.toCharArray();
            for(int i = 0; i < word.length(); i++){
                int pos = letters[i] - 'a';
                if(node.son[pos] == null){
                    node.son[pos] = new TrieNode();
                    node.son[pos].val = letters[i];
                }
                else node.son[pos].num++;
                node = node.son[pos];
            }
            node.isEnd = true;
        }
        
        /** Returns if the word is in the trie. */
        public boolean search(String word) {
            if(word.length() == 0 || word == null) return false;
            TrieNode node = root;
            char[] letters = word.toCharArray();
            for(int i = 0; i < word.length();i++){
                int pos = letters[i] - 'a';
                if(node.son[pos] == null) return false;
                else node = node.son[pos];
            }
            return node.isEnd;
        }
        
        /** Returns if there is any word in the trie that starts with the given prefix. */
        public boolean startsWith(String prefix) {
            if(prefix == null || prefix.length() == 0) return false;
            TrieNode node = root;
            char[] letters = prefix.toCharArray();
            for(int i = 0; i < prefix.length(); i++){
                int pos = letters[i] - 'a';
                if(node.son[pos] == null) return false;
                else node = node.son[pos];
            }
            return true;
        }
    }

    很有意义的题目,prefix tree,root不包含任何val。

    https://baike.baidu.com/item/%E5%AD%97%E5%85%B8%E6%A0%91/9825209?fr=aladdin

    详细讲解在这里。

    一个trie里面应该要有TrieNode,TrieNode里面有son[], num, val, haveSon, isEnd,其中son是儿子数组,num是由根经过此点的单词数量,isEnd是是否到底了。

    然后是各种method,无论是insert还是search都要从root开始,所以初始化都要先把root提出来,insert如果当前char不存在,就新建一个trieNode。如果存在就num++。

    search更简单,从头到尾找有没有对应的TrieNode,最后返回是不是到底了。找prefix是search的简单版,如果中间没有任何差错,遍历完就可以返回true。

  • 相关阅读:
    窗体的扩展样式GWL_EXSTYLE用于SetWindowLong
    内存映射对于大文件的使用
    Delphi实现全局鼠标钩子
    全局键盘钩子(WH_KEYBOARD)
    实现拦截API的钩子(Hook)
    JBoss + EJB3 + MySql : 开发第一个EJB
    取PE文件的引入表和导出表
    Webbrowser中模拟连接点击(非鼠标模拟)
    打造无DLL版穿透防火墙Downloader
    Delphi 常用属性+方法+事件+代码+函数
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12892496.html
Copyright © 2011-2022 走看看