Implementing a simple Trie for efficient Levenshtein Distance calculation - Java

后端 未结 11 841
不思量自难忘°
不思量自难忘° 2020-12-22 18:51

UPDATE 3

Done. Below is the code that finally passed all of my tests. Again, this is modeled after Murilo Vasconcelo\'s modified version of Steve

11条回答
  •  不思量自难忘°
    2020-12-22 19:20

    The function walk takes a testitem (for example a indexable string, or an array of characters) and a trie. A trie can be an object with two slots. One specifying the node of the trie, the other the children of that node. The children are tries as well. In python it would be something like:

    class Trie(object):
        def __init__(self, node=None, children=[]):
            self.node = node
            self.children = children
    

    Or in Lisp...

    (defstruct trie (node nil) (children nil))
    

    Now a trie looks something like this:

    (trie #node None
          #children ((trie #node f
                           #children ((trie #node o
                                            #children ((trie #node o
                                                             #children None)))
                                      (trie #node u
                                            #children ((trie #node n
                                                             #children None)))))))
    

    Now the internal function (which you also can write separately) takes the testitem, the children of the root node of the tree (of which the node value is None or whatever), and an initial distance set to 0.

    Then we just recursively traverse both branches of the tree, starting left and then right.

提交回复
热议问题