You have to define a distance measure for trees to apply KNN
algorithm. There are many possible metrices for trees to use, one of the most popular choices is tree edit distance
( How do I calculate tree edit distance? )
KNN is not a search algorithm. It is not used for finding the closest object, but rather - classifing object to one of the predefined labels. It simply searches for K nearest neighbours of given point X, and returns a label which have most of the neighbours.
For finding a closest tree, assuming that you already have defined the TED
(tree edit distance) would be simply iterate through all tree
in trees
and select the one that minimizes TED(tree,X)
.
Very good resource regarding Tree Edit Distance is also located here:
http://www.inf.unibz.it/dis/projects/tree-edit-distance/tree-edit-distance.php