0

retrieve_suggestion function is used for check the next letter of array.

But how could I check next letter of that function by using a new function but used retreive_suggestion. How could I check next letter of that "Mar" as a tree? New result will be in current.

var words = ["Maria", "Marzia", "Mary", "Marphy", "Michael"];
function build_trie(words) {
    var trie = new Object;
    for (var i = 0; i < words.length; i++) {
        insert_word(trie, words[i]);
    }
    return trie;
}

function insert_word(trie, word) {
    var letters = word.split("");
    var current = trie;
    for (var j = 0; j < letters.length; j++) {
        var character = letters[j];
        var position = current[character];
        if (position == null) {
            current = current[character] = j == letters.length - 1 ? 0 : {};
        } else {
            current = current[character];
        }
    }
}

function retrieve_suggestion(trie, search_string) {
    var letters = search_string.split("");
    var current = trie;
    console.log(current);
    for (var j = 0; j < letters.length; j++) {
        var letter = letters[j];
        // console.log(letter);
        var position = current[letter];
        //  console.log(letter);
        if (position == null) {
            current = current[letter] = j == letters.length - 1 ? 0 : {};
        } else {
            current = current[letter];
        }
    }
    var suggestions = [];
    // console.log(suggestions);
    if (current === 0) {
        suggestions = search_string;
    } else {
        var next_letters = Object.keys(current);
        console.log(next_letters);
        var number_of_suggestions = next_letters.length;
        for (var k = 0; k < number_of_suggestions; k++) {
            suggestions.push(search_string + next_letters[k]);
            //console.log(z);
        }
    }
    return suggestions;
    console.log(suggestions);
}
trie = build_trie(words);

console.log(retrieve_suggestion(trie, "Mar"));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
imraihan
  • 43
  • 10
  • what is the wanted result? which part does not work as expected? – Nina Scholz Jan 25 '18 at 07:38
  • @ Nina Scholz these parts are working well. i have to make new function where i mentioned before. do you understand my question? thanks – imraihan Jan 25 '18 at 07:41
  • no, i do not understand the question. what exactly do you want. please add an example of the result, you need, with the given input. – Nina Scholz Jan 25 '18 at 07:42
  • input is "Mar" and then out is i, z , y, p now i have to make new function where check is there any letter after r. and if there it should be come like tree. like trie formate. thanks – imraihan Jan 25 '18 at 07:48
  • 1
    do you need the forthcoming letter/s? the whole words or anything different from that what is diesplayed in the console? please add the **wanted result** in the question. maybe you hve a look [here](https://stackoverflow.com/q/32757760/1447675) or [here](https://stackoverflow.com/q/32595151/1447675) as well. – Nina Scholz Jan 25 '18 at 07:54

0 Answers0