{"id":25490176,"url":"https://github.com/fix2015/structure_trie","last_synced_at":"2025-11-08T12:30:35.105Z","repository":{"id":273082060,"uuid":"918656541","full_name":"fix2015/structure_trie","owner":"fix2015","description":"A simple implementation of the **Trie (Prefix Tree)** data structure in JavaScript. This repository demonstrates how to create a trie class with essential methods and explains its functionality with practical examples.  ","archived":false,"fork":false,"pushed_at":"2025-01-18T14:16:32.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-18T15:25:45.199Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fix2015.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-18T14:13:04.000Z","updated_at":"2025-01-18T14:41:19.000Z","dependencies_parsed_at":"2025-01-18T15:25:48.425Z","dependency_job_id":"94c2728a-67e1-4460-b0eb-b616b0cbe38a","html_url":"https://github.com/fix2015/structure_trie","commit_stats":null,"previous_names":["fix2015/structure_trie"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_trie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_trie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_trie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_trie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fix2015","download_url":"https://codeload.github.com/fix2015/structure_trie/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239552863,"owners_count":19658003,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-18T21:27:26.456Z","updated_at":"2025-11-08T12:30:35.074Z","avatar_url":"https://github.com/fix2015.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Trie (Prefix Tree) Data Structure in JavaScript 🚀  \n\nA simple implementation of the **Trie (Prefix Tree)** data structure in JavaScript. This repository demonstrates how to create a trie class with essential methods and explains its functionality with practical examples.  \n\n---\n\n## What is a Trie (Prefix Tree)?  \nA **Trie**, also known as a **Prefix Tree**, is a tree-like data structure used to store strings in a way that allows for efficient searching, insertion, and deletion of words, especially when dealing with large sets of strings. Each node represents a single character of a word, and words are stored along paths from the root to the leaves. It is particularly useful for tasks like autocomplete, spell checking, and IP routing.  \n\n---\n\n## Features  \n- **Insert**: Add a word to the trie.  \n- **Search**: Check if a word exists in the trie.  \n- **StartsWith**: Check if there is any word in the trie that starts with a given prefix.  \n- **Delete**: Remove a word from the trie.  \n\n---\n\n## Code Implementation  \n\nHere’s the JavaScript implementation of the trie:  \n\n```javascript\nclass TrieNode {\n    constructor() {\n        this.children = {}; // Stores child nodes\n        this.isEndOfWord = false; // Marks if it's the end of a word\n    }\n}\n\nclass Trie {\n    constructor() {\n        this.root = new TrieNode(); // Root of the trie\n    }\n\n    // Insert a word into the trie\n    insert(word) {\n        let currentNode = this.root;\n        for (let char of word) {\n            if (!currentNode.children[char]) {\n                currentNode.children[char] = new TrieNode();\n            }\n            currentNode = currentNode.children[char];\n        }\n        currentNode.isEndOfWord = true;\n    }\n\n    // Search for a word in the trie\n    search(word) {\n        let currentNode = this.root;\n        for (let char of word) {\n            if (!currentNode.children[char]) {\n                return false;\n            }\n            currentNode = currentNode.children[char];\n        }\n        return currentNode.isEndOfWord;\n    }\n\n    // Check if any word in the trie starts with the given prefix\n    startsWith(prefix) {\n        let currentNode = this.root;\n        for (let char of prefix) {\n            if (!currentNode.children[char]) {\n                return false;\n            }\n            currentNode = currentNode.children[char];\n        }\n        return true;\n    }\n\n    // Delete a word from the trie\n    delete(word) {\n        this.deleteHelper(this.root, word, 0);\n    }\n\n    // Helper function to delete a word\n    deleteHelper(node, word, index) {\n        if (index === word.length) {\n            if (!node.isEndOfWord) return false;\n            node.isEndOfWord = false;\n            return Object.keys(node.children).length === 0;\n        }\n\n        const char = word[index];\n        if (!node.children[char]) return false;\n\n        const shouldDeleteCurrentNode = this.deleteHelper(node.children[char], word, index + 1);\n\n        if (shouldDeleteCurrentNode) {\n            delete node.children[char];\n            return Object.keys(node.children).length === 0;\n        }\n\n        return false;\n    }\n}\n```\n\n---\n\n## Example Usage  \n\n```javascript\n// Initialize the trie\nconst trie = new Trie();\n\n// Insert words into the trie\ntrie.insert(\"apple\");\ntrie.insert(\"app\");\n\n// Search for words\nconsole.log(trie.search(\"apple\")); // Output: true\nconsole.log(trie.search(\"app\")); // Output: true\nconsole.log(trie.search(\"appl\")); // Output: false\n\n// Check if any word starts with a given prefix\nconsole.log(trie.startsWith(\"app\")); // Output: true\nconsole.log(trie.startsWith(\"appl\")); // Output: true\nconsole.log(trie.startsWith(\"banana\")); // Output: false\n\n// Delete a word from the trie\ntrie.delete(\"app\");\nconsole.log(trie.search(\"app\")); // Output: false\nconsole.log(trie.search(\"apple\")); // Output: true\n```\n\n---\n\n## Real-World Applications  \n1. **Autocomplete**: Suggesting words based on a prefix.  \n2. **Spell Checking**: Finding correct spelling suggestions.  \n3. **IP Routing**: Efficient routing for network addresses.  \n4. **Dictionary Implementation**: Storing a dictionary for fast word lookups.  \n\n---\n\n## TikTok Tutorial 🎥  \nWant to see a quick tutorial on how to build this? Check out this TikTok video:  \n[]()  \n\n---\n\n## How to Run the Code  \n1. Clone the repository:  \n   ```bash\n   git clone https://github.com/fix2015/structure_trie\n   cd structure_trie\n   ```\n2. Open the file `index.js` in your favorite code editor.  \n3. Run the file using Node.js:  \n   ```bash\n   node index.js\n   ```\n\n---\n\n## Contributing  \nContributions are welcome! If you have suggestions or want to add new features, feel free to create a pull request.  \n\n---\n\n## License  \nThis project is licensed under the MIT License.  \n\n---\n\n## Connect with Me:\n- [LinkedIn - Vitalii Semianchuk](https://www.linkedin.com/in/vitalii-semianchuk-9812a786/)\n- [Telegram - @jsmentorfree](https://t.me/jsmentorfree) - We do a lot of free teaching on this channel! Join us to learn and grow in web development.\n- [Tiktok - @jsmentoring](https://www.tiktok.com/@jsmentoring) Everyday new videos\n- [Youtube - @jsmentor-uk](https://www.youtube.com/@jsmentor-uk) Mentor live streams\n- [Dev.to - fix2015](https://dev.to/fix2015) Javascript featured, live, experience but about Trie (Prefix Tree)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffix2015%2Fstructure_trie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffix2015%2Fstructure_trie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffix2015%2Fstructure_trie/lists"}