January 2018
Intermediate to advanced
332 pages
7h 36m
English
Now, our tree is ready to have its first method implemented. Our tree starts off with no elements in it (that is, an empty object). You can use any data structure for your implementation, but we will use objects as our data store for simplicity:
add(input) { // set to root of tree var currentNode = this.tree; // init next value var nextNode = null; // take 1st char and trim input // adam for ex becomes a and dam var curChar = input.slice(0,1); input = input.slice(1); // find first new character, until then keep trimming input while(currentNode[curChar] && curChar){ currentNode = currentNode[curChar]; // trim input curChar = input.slice(0,1); input = input.slice(1); } // while next character is available keep ...
Read now
Unlock full access