Radix Tree

Developer
Size
3,161 Kb
Views
36,432

How do I make an radix tree?

What is a radix tree? How do you make a radix tree? This script and codes were developed by Keyvan Akbary on 29 September 2022, Thursday.

Radix Tree Previews

Radix Tree - Script Codes HTML Codes

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

Radix Tree - Script Codes JS Codes

(function() { var RadixTree, debug, t; RadixTree = (function() { var createNode, find, isLeaf, isPrefix, path, sharedPrefix, split; function RadixTree() { this.root = createNode(""); } createNode = function(label) { return { label: label, children: [] }; }; RadixTree.prototype.insertAll = function(values) { var j, len, results, value; results = []; for (j = 0, len = values.length; j < len; j++) { value = values[j]; results.push(this.insert(value)); } return results; }; RadixTree.prototype.insert = function(key) { var child, common, index, node, p, prefix, ref, ref1, ref2; ref = path(this.root, key, this.root.label), node = ref[0], prefix = ref[1]; ref1 = node.children; for (index in ref1) { child = ref1[index]; common = sharedPrefix(key, prefix + child.label); if (common === "") { continue; } ref2 = split(node, index, child, common), node = ref2[0], prefix = ref2[1]; break; } p = key.substr(prefix.length); if (p) { return node.children.push(createNode(p)); } }; split = function(parent, index, node, prefix) { node.label = node.label.substr(prefix.length); node = parent.children[index] = { label: prefix, children: [node] }; return [node, prefix]; }; sharedPrefix = function(str1, str2) { var c, i; for (i in str1) { c = str1[i]; if (c !== str2[i]) { return str1.substr(0, i); } } return str1; }; path = function(node, key, prefix, parent, index) { var child, p, ref; ref = node.children; for (index in ref) { child = ref[index]; p = prefix + child.label; if (isPrefix(p, key)) { return path(child, key, p, node, index); } } return [node, prefix, parent, index]; }; isPrefix = function(prefix, word) { return word.indexOf(prefix) === 0 && prefix !== ""; }; RadixTree.prototype.contains = function(key) { return find(this.root, key) !== null; }; find = function(root, key) { var node, prefix, ref, result; result = (ref = path(root, key, root.label), node = ref[0], prefix = ref[1], ref); if (prefix === key && isLeaf(node)) { return result; } return null; }; isLeaf = function(node) { return true; return node.children.length === 0; }; RadixTree.prototype.remove = function(key) { var index, node, parent, prefix, ref, result; result = (ref = find(this.root, key), node = ref[0], prefix = ref[1], parent = ref[2], index = ref[3], ref); if (!(result && isLeaf(node))) { return; } return parent.children.splice(index, 1); }; return RadixTree; })(); debug = function(node, ind) { var child, j, len, ref, results; if (ind == null) { ind = ""; } console.log(ind + node.label); ref = node.children; results = []; for (j = 0, len = ref.length; j < len; j++) { child = ref[j]; results.push(debug(child, ind + "--")); } return results; }; t = new RadixTree(); t.insertAll(["foo", "foox", "fooxy", "f", "food", "foom", "foot", "foor"]); debug(t.root); console.log(t.root);
}).call(this);
Radix Tree - Script Codes
Radix Tree - Script Codes
Home Page Home
Developer Keyvan Akbary
Username keyvanakbary
Uploaded September 29, 2022
Rating 3
Size 3,161 Kb
Views 36,432
Do you need developer help for Radix Tree?

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 video scripts 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!