
class TrieNode(dict) :

    def __init__(self, *args, **kw):
        super(Node,self).__init__(*args, **kw)
        self.itemlist = super(odict,self).keys()

    def __init__(self) :
        self.word = None;
        self.nodes = {}

    def insert_word(self, word) :
        self.insert_prefix(word, 0)
        
    def insert_prefix(self, word, index) :
        end_index = len(word) - 1
        if index <= end_index :
            char = word[index]
            if not char in current :
                self[char] = Node()
            current = current[char]

            if i == end_index :
                current.word = word

    #def __iter__(self):
    #def __next__(self):


# class Trie(object) :


#     head = Node()
#     self.current = head

#     def __setitem__(self, key, value):
#     # TODO: what should happen to the order if
#     #       the key is already in the dict       
#     self.itemlist.append(key)
#     super(odict,self).__setitem__(key, value)
#     def __iter__(self):
#     return iter(self.itemlist)
#     def keys(self):
#     return self.itemlist
#     def values(self):
#     return [self[key] for key in self]  
#     def itervalues(self):
#     return (self[key] for key in self)



#     def __init__(self, words=None) :
#         self.head = Node()
#         self.current = head

#         if words is not None :
#             for word in words :
#                 self.insert_word(word)


#     def walk_trie(self) :
#         for key in head :
#             node = head[key]
#     # def __iter__(self):
#     #     self.current = head
#     #     return self

#     # def __next__(self):
#     #     if self.current > self.high:
#     #         raise StopIteration
#     #     else:
#     #         self.current += 1
#     #         return self.current - 1


if __name__ == '__main__':

    with open('/usr/share/dict/words') as f :
        words = f.read().splitlines()

    trie = TrieNode()

    for i, word in enumerate(words) :
        trie[word] = word
        if i == 9 :
            break

    print trie

    # trie = Trie(words)


@@ -293,16 +293,24 @@
 fix(word
+.lower()
 , 0)%0A   
@@ -486,158 +486,1344 @@
  in 
-current :%0A                self%5Bchar%5D = Node()%0A            current = current%5Bchar%5D%0A%0A            if i == end_index :%0A                current.
+self :%0A                self%5Bchar%5D = TrieNode()%0A%0A            if index == end_index :%0A                self%5Bchar%5D.word = word%0A            else :%0A                self%5Bchar%5D.insert_prefix(word, index + 1)%0A%0A    def suggest(self, prefix, count) :%0A        if prefix == None or len(prefix) == 0 :%0A            return None%0A%0A        key = prefix%5B0:1%5D.lower()%0A        if key in self :%0A            if len(prefix) %3E 1 :%0A                return self%5Bkey%5D.suggest(prefix%5B1:%5D, count)%0A            else :%0A                suggestions = %5B%5D%0A                for key in self.keys() :%0A                    node = self%5Bkey%5D%0A                    suggestions.extend(node.get_all()%5B:count%5D)%0A                    if len(suggestions) == count :%0A                        break%0A                return suggestions%0A        return None%0A%0A    def get_all(self) :%0A        
 word
+s
  = 
-word
+%5B%5D%0A        if self.word is not None :%0A            words.append(self.word)%0A%0A        for key in self.keys() :%0A            words.extend(self%5Bkey%5D.get_all())%0A%0A        return words%0A%0A    def has_prefix(self, prefix) :%0A        if prefix == None or len(prefix) == 0 :%0A            return False%0A%0A        key = prefix%5B0:1%5D.lower()%0A        if key in self :%0A            if len(prefix) %3E 1 :%0A                return self%5Bkey%5D.has_prefix(prefix%5B1:%5D)%0A            else :%0A                return True%0A        return False
 %0A%0A  
@@ -3179,25 +3179,64 @@
     
-trie%5Bword%5D = word
+print %22inserting: %22, word%0A        trie.insert_word(word)
 %0A   
@@ -3295,26 +3295,36 @@
     
-# trie = Trie(words
+print trie.suggest(%22aard%22, 10
 )%0A%0A

Work on find, has_prefix and get_all