Implementing a simple Trie for efficient Levenshtein Distance calculation - Java

后端 未结 11 830
不思量自难忘°
不思量自难忘° 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:31

    As I see it right, you want to loop over all branches of the trie. That's not that difficult using a recursive function. I'm using a trie as well in my k-nearest neighbor algorithm, using the same kind of function. I don't know Java, however but here's some pseudocode:

    function walk (testitem trie)
       make an empty array results
       function compare (testitem children distance)
         if testitem = None
            place the distance and children into results
         else compare(testitem from second position, 
                      the sub-children of the first child in children,
                      if the first item of testitem is equal to that 
                      of the node of the first child of children 
                      add one to the distance (! non-destructive)
                      else just the distance)
            when there are any children left
                 compare (testitem, the children without the first item,
                          distance)
        compare(testitem, children of root-node in trie, distance set to 0)
        return the results
    

    Hope it helps.

提交回复
热议问题