Front-end implementation of dictionary tree
This article demonstrates the front-end implementation of dictionary tree, which is also LeetCode No. 208, medium difficulty.
Summary of topics
Implement a Trie (prefix tree), including insert, search, and startsWith.
Example:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // returns true
trie.search("app"); // returns false
trie.startsWith("app"); // Returns true
trie.insert("app");
trie.search("app"); // returns true
Explain:
You can assume that all input is made up of lowercase letters a-z.
Ensure that all inputs are non-empty strings.
code implementation
Tree node
Firstly, tree nodes are declared. Each node has a value and 26 child nodes, which are represented by {} objects, where each object has TreeNode.
There is also an isWord attribute to store words that currently end with the val character of the current node.
class TreeNode{ constructor(val) { this.val = val this.isWord = false; this.chilrden = {} } }
Dictionary tree constructor
Initialize dictionary tree, only root element, root element is also a node, so use TreeNode ()
At the same time, the root element has no val, so TreeNode does not need to pass parameters.
For the time being, there are no child elements, so its children attribute is for the time being: {} empty object
There is no word that does not contain any letters, so the isWord attribute of the root is also false by default.
/** * Initialize your data structure here. */ var Trie = function() { this.root = new TreeNode(); };
Implementing insert method
/** * Inserts a word into the trie. * @param {string} word * @return {void} */ Trie.prototype.insert = function(word) { let curNode = this.root; let arr = word.split('') for(let i = 0; i < arr.length; i++){ let isHasChildNode = curNode.chilrden[arr[i]] // If there is no child node, create a child node with the current character as val if(!isHasChildNode){ curNode.chilrden[arr[i]] = new TreeNode(arr[i]) } curNode = curNode.chilrden[arr[i]] // Traverse to the node corresponding to the last character, and set the isWord attribute of this node to true. if(i === arr.length - 1){ curNode.isWord = true; } } };
Implementing search method
/** * Returns if the word is in the trie. * @param {string} word * @return {boolean} */ Trie.prototype.search = function(word) { let curNode = this.root; let arr = word.split('') for(let i = 0; i < arr.length; i++){ if(!curNode.chilrden[arr[i]]){ return false; } curNode = curNode.chilrden[arr[i]] // Search for the last character and determine if the word has ever been saved based on the isWord attribute if(i === arr.length - 1){ return curNode.isWord === true } } };
Implementation of startswith Method
/** * Returns if there is any word in the trie that starts with the given prefix. * @param {string} prefix * @return {boolean} */ Trie.prototype.startsWith = function(prefix) { let curNode = this.root; let arr = prefix.split('') for(let i = 0; i < arr.length; i++){ // If a character in the middle of the word is found and no node is found, false is returned. if(!curNode.chilrden[arr[i]]){ return false; } curNode = curNode.chilrden[arr[i]] } // Each character finds the node and returns true return true };
Test code
let trie = new Trie();
trie.insert("apple");
console.log(trie.search("apple"); // returns true
console.log(trie.search("app"); // returns false
console.log(trie.startsWith("app"); // Returns true
trie.insert("app");
console.log(trie.search("app"); // returns true
My Front-end Algorithms Library
Address: https://github.com/cunzaizhuy...
At present, more than 200 questions have been brushed, and the solutions are all in them.
Welcome to star and Exchange.