src/components/Trie/index.js
// Insert each character of the input str into the trie if it's not already there
Insert each character of the input str into the trie if it's not already there
Insert each character of the input str into the trie if it ' s not already there
insert( str ) {
        if ( !this.root )
            this.root = new TrieNode ();
        var cur = this.root;
        var len = str.length;
        for ( let i = 0 ; i < len; i++ ) {
            if ( !cur.children[ str[i] ] ) {
                var node = new TrieNode( str[i] );    
                cur.children[ str[i] ] = node;
            }
            cur = cur.children[ str[i] ];
        }
        if (cur.terminal)
            return false;
        else {
            cur.terminal = true;
            return true;
        }
    }
insert ( str ) { if ( ! this . root ) this . root = new TrieNode ( ) ; var cur = this . root ; var len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { if ( ! cur . children [ str [ i ] ] ) { var node = new TrieNode ( str [ i ] ) ; cur . children [ str [ i ] ] = node ; } cur = cur . children [ str [ i ] ] ; } if ( cur . terminal ) return false ; else { cur . terminal = true ; return true ; } }
Insert each character of the input str into the trie if it's not already there
Insert each character of the input str into the trie if it ' s not already there