Java Trie(詞典樹)實現

來源:互聯網
上載者:User

標籤:string   hash   hashmap   entry   rem   ash   get   stringbu   尋找   

實現Trie tree,可用作實現詞典。可用來儲存,尋找及刪除string, 同時實現返回首碼為指定字元所有結果的功能。

每個node存所有child節點與及對應path上的字元所組成的map,利用count來記錄每個節點的子樹種存在多少word,便於刪除的操作。

findAllWithPrefix 用到了DFS的思想,遍曆所有帶有首碼的結果並輸出。常見應用於搜尋引擎中。

  1 class TrieNode {  2     int count;  3     Map<Character, TrieNode> children;  4     boolean isWord;  5     public TrieNode() {  6         count=0;  7         isWord=false;  8         children=new HashMap<Character,TrieNode>();  9     } 10 } 11  12 public class MyTrie { 13     TrieNode root; 14  15     public MyTrie() { 16         root = new TrieNode(); 17     } 18  19     public boolean search(String s) { 20         if (this.root == null || s == null || s.length() == 0) { 21             return false; 22         } 23         TrieNode cur=root; 24         for (int i = 0; i < s.length(); i++) { 25             TrieNode next=cur.children.get(s.charAt(i)); 26             if(next == null) { 27                 return false; 28             } 29             cur=next; 30         } 31         return cur.isWord; 32     } 33     public void insert(String s) { 34         if(search(s) || s == null || s.length() == 0) { 35             return; 36         } 37         TrieNode cur=root; 38         for (int i = 0; i < s.length(); i++) { 39             TrieNode next=cur.children.get(s.charAt(i)); 40             if(next == null) { 41                   next=new TrieNode(); 42                   cur.children.put(s.charAt(i), next); 43             } 44             cur=next; 45             cur.count++; 46         } 47         cur.isWord=true; 48     } 49     public boolean delete(String s) { 50         if(!search(s)) { 51             return false; 52         } 53         TrieNode cur=root; 54         for(int i=0; i<s.length(); i++) { 55             TrieNode next=cur.children.get(s.charAt(i)); 56             if(next.count == 1) { 57                 cur.children.remove(s.charAt(i)); 58                 return true; 59             } 60             next.count--; 61             cur=next; 62         } 63         cur.isWord=false; 64         return true; 65     } 66      67     public List<String>findAllWithPrefix(String s){ 68         TrieNode matchNode=searchNode(s); 69         if(matchNode == null) { 70             return null; 71         } 72         List<String>result=new ArrayList<String>(); 73         DFS(result, matchNode, new StringBuilder(s)); 74         return result; 75     } 76     private void DFS(List<String>result, TrieNode matchNode, StringBuilder sb) { 77         if(matchNode.isWord == true) { 78             result.add(sb.toString()); 79         } 80         for(Map.Entry<Character, TrieNode>child : matchNode.children.entrySet()) { 81             sb.append(child.getKey()); 82             DFS(result,child.getValue(),sb); 83             sb.deleteCharAt(sb.length()-1); 84         } 85     } 86     private TrieNode searchNode(String s) { 87         if(s == null || s.length() == 0) { 88             return null; 89         } 90         TrieNode cur=root; 91         for(int i=0; i<s.length(); i++) { 92             TrieNode next=cur.children.get(s.charAt(i)); 93             if(next == null) { 94                 return cur; 95             } 96             cur=next; 97         } 98         return cur; 99     }100 }

 

Java Trie(詞典樹)實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.