terasum/js-bktree

Having different results when the Order of words in Dictionary changed!

SupervisionT opened this issue · 1 comments

As in the example, try to add the word "anic" at the end of the words array and run the code as follow:

import BKTree from "../lib/bktree";

const words = [
"Aani", "aardvark", "aardwolf", "Aaron", "Aaronic",
"Aaronical", "Aaronite", "Aaronitic", "Aaru", "Ab",  "aba", "Ababdeh", "Ababua", "abac",
"abaca", "abacate", "abacay", "abacinate", "abacination", "abaciscus", "abacist", "aback",
"abactinal", "abactinally", "abaction", "abactor","abaculus", "abacus", "Abadite", "abaff",
"abaft", "abaisance", "abaiser", "abaissed", "abalienate", "abalienation", "abalone",
"Abama", "abampere", "abandon", "abandonable","abandoned", "abandonedly", "abandonee",
"abandoner", "abandonment","Abanic", "Abantes","abaptiston", "Abarambo", "Abaris",
"abarthrosis", "abarticular","abarticulation", "abas", "abase", "abased", "abasedly",
"abasedness", "abasement","abaser", "Abasgi", "abash", "abashed", "abashedly", "abashedness",
"abashless","abashlessly", "abashment", "abasia", "abasic", "abask", "Abassin",
"abastardize","abatable", "abate", "abatement", "abater", "abatis", "abatised", "abaton",
"abator","abattoir", "Abatua", "abature", "abave", "abaxial", "abaxile", "abaze", "abb",
"Abba","abbacomes", "abbacy", "Abbadide" , "anic"] ;

const bktree = new BKTree(words.length);
bktree.add(words);
console.log(bktree.simWords("anic", 1));

the expected result is [ 'anic' ] but the output is [] !

Now try to change the location of the word "anic" to the beginning of the words array then run the same code and the result is [ 'anic' ] !

Would you please help by explaining why the order of the word in the dictionary array give different results?

One of the key principal to build tree is to have sorted data. That's why the BKTree doesn't works when I added data to it. BKTree, doesn't sort data so you need to sort it before pass it to the function.