zoukankan      html  css  js  c++  java
  • 三叉Trie树

    public class TernarySearchTrie {
        
        private static class TSTNode{
            private String value=null;
            private TSTNode left;
            private TSTNode mid;
            private TSTNode right;
            private char splitChar;
            
            private TSTNode()
            {}
            
            private TSTNode(char c){
                splitChar=c;
            }
        }
            
        private TSTNode root=new TSTNode();
        
        public void addWord(String word)
        {
            TSTNode nodeRoot=root;
            for (int i = 0; i < word.length(); i++)
            {
                nodeRoot=getOrCreate(word.charAt(i),nodeRoot);
            }
            nodeRoot.value=word;
        }
        
        private TSTNode getOrCreate(char key,TSTNode node)
        {
            if(node.mid==null)
            {
                node.mid=new TSTNode(key);
                return node.mid;
            }
            TSTNode current=node.mid;
            while(true)
            {
                int dis=key-current.splitChar;
                if(dis==0)
                {
                    return current;
                }
                else if(dis<0)
                {
                    if(current.left==null)
                    {
                        current.left=new TSTNode(key);
                        return current.left;
                    }
                    current=current.left;
                }
                else
                {
                    if(current.right==null)
                    {
                        current.right=new TSTNode(key);
                        return current.right;
                    }
                    current=current.right;
                }
            }
        }
        
        public String getNode(String word)
        {
            TSTNode nodeRoot=root;
            for (int i = 0; i < word.length(); i++)
            {
                nodeRoot=getNode(word.charAt(i),nodeRoot);
                if(nodeRoot==null)
                {
                    return null;
                }
            }        
            return nodeRoot.value;
        }
        
        private TSTNode getNode(char key,TSTNode node)
        {
            if(node.mid==null)
            {
                return null;
            }
            TSTNode current=node.mid;
            while(true)
            {
                int dis=key-current.splitChar;
                if(dis==0)
                {
                    return current;
                }
                else if(dis<0)
                {
                    if(current.left==null)
                    {
                        return null;
                    }
                    current=current.left;
                }
                else
                {
                    if(current.right==null)
                    {
                        return null;
                    }
                    current=current.right;
                }
            }
        }
        
    }
    
     
    
     
    
            TernarySearchTrie tree=new TernarySearchTrie();
            tree.addWord("is");
            tree.addWord("in");
            tree.addWord("it");
            
            tree.addWord("be");
            tree.addWord("by");
    
            tree.addWord("as");
            tree.addWord("at");
            
            tree.addWord("he");
            
            tree.addWord("on");
            tree.addWord("or");
            tree.addWord("of");
            
            tree.addWord("to");
            
            
            System.out.println(tree.getNode("is"));
            System.out.println(tree.getNode("on"));
            System.out.println(tree.getNode("to"));


    
    
  • 相关阅读:
    [转][黄忠成]Object Builder Application Block (1)
    C#.NET里面抽象类和接口有什么区别
    MVC中AOP思想的体现(四种过滤器)并结合项目案例说明过滤器的实际用法
    NHibernate之(24):探索NHibernate二级缓存(下)
    使用 ES (elasticsearch) 搜索中文
    elasticsearch 中文 term & completion suggester
    uwsgs loading shared libraries: libicui18n.so.58 异常处理
    tensorflow with gpu 环境配置
    Java 多线程执行
    SpringBoot log4j2 异常
  • 原文地址:https://www.cnblogs.com/i80386/p/2486593.html
Copyright © 2011-2022 走看看