January 2018
Intermediate to advanced
332 pages
7h 36m
English
The search() method is even simpler and highly efficient with a complexity of O(n), where n is the length of the search input. The big O notation is something that will be covered in detail in a later chapter:
search(input) { // get the whole tree var currentNode = this.tree; var curChar = input.slice(0,1); // take first character input = input.slice(1); // keep extracting the sub-tree based on the current character while(currentNode[curChar] && curChar){ currentNode = currentNode[curChar]; curChar = input.slice(0,1); input = input.slice(1); } // reached the end and no sub-tree found // e.g. no data found if (curChar && !currentNode[curChar]) { return {}; } // return the node found return currentNode;}
Read now
Unlock full access