src/components/Trie/index.js
// Uses Breadth First Search to look for all next closest words of the input string
Uses Breadth First Search to look for all next closest words of the input string
Uses Breadth First Search to look for all next closest words of the input string
autocomplete ( str ) {
        var results = Array();
        if ( dict[ map[ str ] ] == str ) {
            console.log(str);
            return str;
        }
        var { cur: root } = this.search( str );
        if ( !root.value ) {
            return null;
        }
        var queue = Array();
        var path = String();
        var words = Array();
        path = str;
        queue.push( { root, path } );
        while ( queue.length > 0 ) {
            var { root, path : new_path } = queue.shift();
            if ( root.terminal )
                words.push( new_path );
            var children = root.children;
            for ( let key in children ) {
                var root = children[ key ];
                var path = new_path + children[ key ].value;
                var new_obj = { root, path };
                queue.push( new_obj );
            }
        }
        for ( let word of words ) {
            if ( word ) {
                var str_in_dict = dict[ map[ word ] ].split(' ');
                if ( word == str_in_dict[0] ) {
                    console.log( 'dict[ map[ word ] ]', dict[ map[ word ] ] );
                    results.push( dict[ map[ word ] ] );
                }
            }
        }
        return results;
    }
autocomplete ( str ) { var results = Array ( ) ; if ( dict [ map [ str ] ] == str ) { console . log ( str ) ; return str ; } var { cur : root } = this . search ( str ) ; if ( ! root . value ) { return null ; } var queue = Array ( ) ; var path = String ( ) ; var words = Array ( ) ; path = str ; queue . push ( { root , path } ) ; while ( queue . length > 0 ) { var { root , path : new_path } = queue . shift ( ) ; if ( root . terminal ) words . push ( new_path ) ; var children = root . children ; for ( let key in children ) { var root = children [ key ] ; var path = new_path + children [ key ] . value ; var new_obj = { root , path } ; queue . push ( new_obj ) ; } } for ( let word of words ) { if ( word ) { var str_in_dict = dict [ map [ word ] ] . split ( ' ' ) ; if ( word == str_in_dict [ 0 ] ) { console . log ( 'dict[ map[ word ] ]' , dict [ map [ word ] ] ) ; results . push ( dict [ map [ word ] ] ) ; } } } return results ; }
Uses Breadth First Search to look for all next closest words of the input string
Uses Breadth First Search to look for all next closest words of the input string
// Pushes onto the queue all possible next characters that the current path can absorb // For each closest word found, finds the corresponding entry in the dictionary it belongs to // For example, if the word is "Head", then its corresponding entry in the dictionary is "Head of Shruberry" // Pushes all such entries onto the results array and returns the results array.