統計

          留言簿(1)

          DB

          Others

          QA

          Tech Website

          閱讀排行榜

          評論排行榜

          Trie Tree

          #Trie Tree的基本特點
          1)根節點不包含字符,除根節點外每個節點只包含一個字符
          2)從根節點到某一個節點,路徑上經過的字符連接起來,為該節點對應的字符串
          3)每個節點的所有子節點包含的字符串不相同

          原理圖:


          參考:
          數據結構之Trie樹
          Trie Tree on Answers.com

          #Trie Tree簡單的Python實現
          最近在學習Python,下面的代碼就用來學習下語法了 :)

          #coding:utf-8
          '''
          Created on 2011-6-14

          @author: Dokie
          '''
          ALPHABET_SIZE 
          = 26

          class Node:
              
              
          def __init__(self, is_end = False):
                  self.is_end 
          = is_end
                  self.prefix_count 
          = 0
                  self.children 
          = [None for child in range(ALPHABET_SIZE)]

          class Trie:
              
          def __init__(self):
                  self.head 
          = Node()
                  
              
          def insert(self, word):
                  current 
          = self.head
                  count 
          = 0
                  
          for letter in word:
                      int_value 
          = ord(letter) - ord('a')
                      
          try:
                          
          assert ( 0 <= int_value <= 26)
                      
          except Exception:
                          
          raise Exception("invalid Word")
                      
                      
          if current.children[int_value] is None:
                          current.children[int_value] 
          = Node()
                          current 
          = current.children[int_value]
                          count 
          += 1
                          current.prefix_count 
          = count
                  current.is_end 
          = True
                  
              
          def search(self, word):
                  
                  current 
          = self.head
                  
          for letter in word:
                      int_value 
          = ord(letter) - ord('a')
                      
          try:
                          
          assert (0 <= int_value <= 26)
                      
          except Exception:
                          
          return False
                      
          if current.children[int_value] is None:
                          
          return False
                      
          else:
                          current 
          = current.children[int_value]
                          
                      
          return current.is_end


                  
                  
                   
              
          def count_words_with_prefix(self, word):
                  current 
          = self.head
                  
          for letter in word:
                      int_value 
          = ord(letter) - ord('a')
                      
          try:
                          
          assert (0 <= int_value <= 26)
                      
          except Exception:
                          
          return None
                      
          if current.children[int_value] is None:
                          
          return None
                      
          else:
                          current 
          =  current.children[int_value]
                  
          return current.prefix_count
                          

                  
          if __name__ == '__main__':
              
          import doctest
              doctest.testmod()
              t 
          = Trie()
              t.insert(
          "apple")
              t.insert(
          "good")
              
          print t.search("apple")
              
          print t.search("fff")
              
          print t.count_words_with_prefix("apple")

          附:TrieMap Javap實現


          posted on 2011-06-14 16:57 XXXXXX 閱讀(1091) 評論(0)  編輯  收藏 所屬分類: Algorithm

          主站蜘蛛池模板: 柞水县| 霍城县| 始兴县| 建昌县| 容城县| 祁阳县| 孟州市| 林周县| 崇礼县| 铁力市| 泌阳县| 隆德县| 油尖旺区| 肥乡县| 藁城市| 蒙城县| 上杭县| 平乐县| 靖西县| 古丈县| 壤塘县| 神农架林区| 小金县| 抚州市| 承德县| 大足县| 洪泽县| 江城| 石泉县| 金乡县| 苏尼特右旗| 土默特右旗| 凯里市| 无为县| 毕节市| 尤溪县| 陆丰市| 定西市| 印江| 闸北区| 云梦县|