Trie

Developer
Size
2,756 Kb
Views
40,480

How do I make an trie?

What is a trie? How do you make a trie? This script and codes were developed by Keyvan Akbary on 27 September 2022, Tuesday.

Trie Previews

Trie - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Trie</title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Trie - Script Codes JS Codes

(function() { var Trie, debug, t; Trie = (function() { var hasChildren, path, removeChild, values; function Trie() { this.root = { char: "", children: [] }; } Trie.prototype.insert = function(key) { var char, j, len, length, newNode, node, ref, ref1; ref = path(this.root, key), node = ref[0], length = ref[1]; ref1 = key.substr(length); for (j = 0, len = ref1.length; j < len; j++) { char = ref1[j]; newNode = { char: char, children: [], word: false }; node.children.push(newNode); node = newNode; } return node.word = true; }; path = function(node, key) { var char, child, i, index, j, len, length, parent, ref; length = 0; parent = node; index = 0; for (j = 0, len = key.length; j < len; j++) { char = key[j]; ref = node.children; for (i in ref) { child = ref[i]; if (child.char === char) { parent = node; node = child; index = i; length++; } } } return [node, length, parent, index]; }; Trie.prototype.remove = function(key) { var index, length, node, parent, ref; ref = path(this.root, key), node = ref[0], length = ref[1], parent = ref[2], index = ref[3]; if (key.length !== length) { return; } if (hasChildren(node)) { return removeChild(parent, index); } else { return node.word = false; } }; hasChildren = function(node) { return node.children.length === 0; }; removeChild = function(node, index) { console.log(node); return node.children.splice(index, 1); }; Trie.prototype.vals = function() { return values(this.root, ""); }; values = function(node, word) { var arr, child, j, len, ref, vs; word += node.char; arr = []; if (node.word) { arr.push(word); } ref = node.children; for (j = 0, len = ref.length; j < len; j++) { child = ref[j]; vs = values(child, word); arr.concat(vs); } console.log(arr); return arr; }; return Trie; })(); debug = function(node, tab) { var child, j, len, ref, results; console.log(tab + node.char + (node.word ? "*" : "")); ref = node.children; results = []; for (j = 0, len = ref.length; j < len; j++) { child = ref[j]; results.push(debug(child, tab + "--")); } return results; }; t = new Trie(); t.insert("one"); t.insert("onez"); t.insert("onx"); console.dir(t.vals()); debug(t.root, "");
}).call(this);
Trie - Script Codes
Trie - Script Codes
Home Page Home
Developer Keyvan Akbary
Username keyvanakbary
Uploaded September 27, 2022
Rating 3
Size 2,756 Kb
Views 40,480
Do you need developer help for Trie?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Keyvan Akbary (keyvanakbary) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!