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

    Implement a trie with insertsearch, and startsWith methods.

    Trie,又称单词查找树或键树,是一种形结构。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计或是前缀匹配。

    它有3个基本性质:

    1. 根节点不包含字符,除根节点外每一个节点都只包含一个字符
    2. 根节点到某一节点路径上经过的字符连接起来,为该节点对应的字符串
    3. 每个节点的所有子节点包含的字符都不相同。
     
    下面这个图就是Trie的表示,每一条边表示一个字符,如果结束,就用星号表示。在这个Trie结构里,我们有下面字符串,比如do, dork, dorm等,但是Trie里没有ba, 也没有sen,因为在a, 和n结尾,没有结束符号(星号)。
     

    有了这样一种数据结构,我们可以用它来保存一个字典,要查询改字典里是否有相应的词,是否非常的方便呢?我们也可以做智能提示,我们把用户已经搜索的词存在Trie里,每当用户输入一个词的时候,我们可以自动提示,比如当用户输入 ba, 我们会自动提示 bat 和 baii.

    转自:http://blog.csdn.net/beiyetengqing/article/details/7856113

    实现代码如下:

    class TrieNode {
        
        char item;//节点存储的字符
        
        LinkedList<TrieNode> childNodes;//所有子节点集合
        
        boolean isEnd = false;//单词结束标记
        
        // Initialize your data structure here.
        public TrieNode() {
            item = ' ';
            childNodes = new LinkedList<TrieNode>();
        }
        
        //查询子节点
        public TrieNode getChildNode(char c) {
            for(TrieNode childNode:childNodes) {
                if(childNode.item==c) return childNode;
            }
            return null;
        }
        
        //增加子节点
        public void addChildNode(char c) {
            TrieNode childNode = new TrieNode();
            childNode.item = c;
            childNodes.add(childNode);
        }
    }
    
    public class Trie {
        private TrieNode root;
    
        public Trie() {
            root = new TrieNode();
        }
    
        // Inserts a word into the trie.
        public void insert(String word) {
            
            if(search(word)) return;//若该单词已经存在直接返回
            
            TrieNode cur = root;
            for(int i=0;i<word.length();i++) {
                if(cur.getChildNode(word.charAt(i))==null) 
                    cur.addChildNode(word.charAt(i));//插入子节点
                cur = cur.getChildNode(word.charAt(i));
            }
            cur.isEnd = true;//标记单词结束
        }
    
        // Returns if the word is in the trie.
        public boolean search(String word) {
            TrieNode cur = root;
            for(int i=0;i<word.length();i++) {
                if(cur.getChildNode(word.charAt(i))==null) return false;
                cur = cur.getChildNode(word.charAt(i));
            }
            return cur.isEnd==true;
        }
    
        // Returns if there is any word in the trie
        // that starts with the given prefix.
        public boolean startsWith(String prefix) {
            TrieNode cur = root;
            for(int i=0;i<prefix.length();i++) {
                if(cur.getChildNode(prefix.charAt(i))==null) return false;
                cur = cur.getChildNode(prefix.charAt(i));
            }
            return true;
        }
    }
    
    // Your Trie object will be instantiated and called as such:
    // Trie trie = new Trie();
    // trie.insert("somestring");
    // trie.search("key");
  • 相关阅读:
    [转载] $CF652B$ 题解
    [转载] $Luogu$ $P4951$ 题解
    luoguP3723 HNOI2017 礼物
    动态dp学习笔记
    noip级别模板小复习
    noip2017简要题解。
    noip考前抱佛脚 数论小总结
    HDU 6071 Lazy Running
    POI2012 ODL-Distance
    [NOI2002]荒岛野人
  • 原文地址:https://www.cnblogs.com/mrpod2g/p/4490482.html
Copyright © 2011-2022 走看看