写trie实现

时间:2011-09-12 23:16:35

标签: algorithm data-structures

这个问题已被多次询问,我搜索了许多地方,但仍然找不到一个地方,我可以逐步指导编写trie实现。请帮帮我 首选语言为JavaPython
谢谢

5 个答案:

答案 0 :(得分:2)

我已尝试在java中搜索字符串。它非常简单: 以下是步骤:

Node类是这样的:

public class Trienode {
    char c;
    Trienode parent;
    ArrayList<Trienode> childs;
}

Trienode addString{ String str, Trienode root ){
      if(str.length == 0) return root;
      String newstr = [str without the first char];
      char c = str[0];
      Trienode newnode = root[c - '0'];
       if(newnode == null){
            newnode = new Trienode();
            newnode.c = c;
            newnode.parent = root;
       }
       return addString(newstr, newnode);
  }

您可以在同一行创建搜索等。

答案 1 :(得分:1)

答案 2 :(得分:1)

#!/usr/bin/env python
import sys

class Node:
    def __init__(self):
        self.next = {}  #Initialize an empty hash (python dictionary)
        self.word_marker = False 
        # There can be words, Hot and Hottest. When search is performed, usually state transition upto leaf node is peformed and characters are printed. 
        # Then in this case, only Hottest will be printed. Hot is intermediate state. Inorder to mark t as a state where word is to be print, a word_marker is used


    def add_item(self, string):
        ''' Method to add a string the Trie data structure'''

        if len(string) == 0:
            self.word_marker = True 
            return 

        key = string[0] #Extract first character
        string = string[1:] #Create a string by removing first character

        # If the key character exists in the hash, call next pointing node's add_item() with remaining string as argument
        if self.next.has_key(key):
            self.next[key].add_item(string)
        # Else create an empty node. Insert the key character to hash and point it to newly created node. Call add_item() in new node with remaining string.
        else:
            node = Node()
            self.next[key] = node
            node.add_item(string)


    def dfs(self, sofar=None):
        '''Perform Depth First Search Traversal'''

        # When hash of the current node is empty, that means it is a leaf node. 
        # Hence print sofar (sofar is a string containing the path as character sequences through which state transition occured)
        if self.next.keys() == []:
            print "Match:",sofar
            return

        if self.word_marker == True:
            print "Match:",sofar

        # Recursively call dfs for all the nodes pointed by keys in the hash
        for key in self.next.keys():
            self.next[key].dfs(sofar+key)

    def search(self, string, sofar=""):
        '''Perform auto completion search and print the autocomplete results'''
        # Make state transition based on the input characters. 
        # When the input characters becomes exhaused, perform dfs() so that the trie gets traversed upto leaves and print the state characters
        if len(string) > 0:
            key = string[0]
            string = string[1:]
            if self.next.has_key(key):
                sofar = sofar + key
                self.next[key].search(string,sofar)

            else:
                print "No match"
        else:
            if self.word_marker == True:
                print "Match:",sofar

            for key in self.next.keys():
                self.next[key].dfs(sofar+key)


def fileparse(filename):
    '''Parse the input dictionary file and build the trie data structure'''
    fd = open(filename)

    root = Node()   
    line = fd.readline().strip('\r\n') # Remove newline characters \r\n

    while line !='':
        root.add_item(line)
        line = fd.readline().strip('\r\n')

    return root



if __name__ == '__main__':

    if len(sys.argv) != 2:
        print "Usage: ", sys.argv[0], "dictionary_file.txt"
        sys.exit(2)

    root  = fileparse(sys.argv[1])

    print "Input:",
    input=raw_input()
    root.search(input)

答案 3 :(得分:0)

我不是Java或Python编码器,但可以为您提供一个非常简单的c#trie实现。它非常简单,所以我相信你可以把它映射到Java。

这是:

public class Trie<T> : Dictionary<T, Trie<T>>
{
}

完成。告诉你这很简单。但这是一个特里,它的确有效。

答案 4 :(得分:0)

这是实施: -

import java.util.HashMap;

public class Tries {

$names = [
    'Mike' => true,
    'John' => true,
    'Dave' => true,
    'Tony' => true
];

$gotDave = isset($names['Dave']);

}

只需使用hashmap并跟踪单词结尾。