First know they iterate over the entire tree. By iterating over the entire tree they are guaranteed to find the most similar neighbor.
To be more efficient in the average case use a DFS graph traversal for the tree. Notice that since it is a tree, there will be no need for a coloring scheme for visited nodes.
Start with the closest object as null and at the root of the tree.
For each node you should traverse the children in the order of how much they add to the edit distance and only if the added edit distance would not be greater than the distance to the closest object. For example, with hamming distance, first traverse the child that would add "O" to the overall distance, then afterwards traverse the child that would add "1" to the overall distance. But do not traverse into the "1" child if that would make the edit distance greater than the current closest distance.
When you encounter a "word" within the prefix tree check to see if it has a distance to the query object that is less than the closest object and assign it to closest.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…