Trie
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 - 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);
Developer | Keyvan Akbary |
Username | keyvanakbary |
Uploaded | September 27, 2022 |
Rating | 3 |
Size | 2,756 Kb |
Views | 40,480 |
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!
Name | Size |
Functional Inheritance | 2,266 Kb |
A Pen by Keyvan Akbary | 3,541 Kb |
A Pen by Keyvan Akbary | 2,866 Kb |
Radix Tree | 3,161 Kb |
A Pen by Keyvan Akbary | 3,684 Kb |
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!
Name | Username | Size |
Touch Carousel - last, no gaps. | Berkin | 4,332 Kb |
Pericles mi loro... | Judag | 4,125 Kb |
CSS3 Selectables with information rollover | Jasonmayes | 9,565 Kb |
CSS Variables | Jdsteinbach | 4,759 Kb |
Profile box | Daniesy | 2,766 Kb |
Navier Stoke Fluid Simulation | Esimov | 8,584 Kb |
Bezier Animation with straight paths | Rhernando | 2,087 Kb |
Responsive scrolling text | Ashdurham | 2,259 Kb |
HEXAGON | Aurumlux | 1,684 Kb |
Twitch JSON API | Jvhti | 2,808 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!