Trinary Tree
How do I make an trinary tree?
What is a trinary tree? How do you make a trinary tree? This script and codes were developed by Mnicpt on 30 July 2022, Saturday.
Trinary Tree - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Trinary Tree</title>
</head>
<body> <div class="console"></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Trinary Tree - Script Codes JS Codes
function TrinaryTree() { this.rootNode = undefined; this.size = 0; this.initWithNodes = function(nodes) { for (node in nodes) { this.insert(nodes[node]); } }; this.insert = function(node, parentNode) { if (!parentNode) { // set the root node if (this.rootNode == undefined) { this.rootNode = node; this.size++; return; } parentNode = this.rootNode; } // Node value is less than parent if (node.value < parentNode.value) { if (parentNode.leftChild) { this.insert(node, parentNode.leftChild); } else { parentNode.leftChild = node; node.parent = parentNode; this.size++; } // Node value is equal to parent } else if (node.value === parentNode.value) { if (parentNode.middleChild) { this.insert(node, parentNode.middleChild); } else { parentNode.middleChild = node; node.parent = parentNode; this.size++; } // Node value is greather than parent } else { if (parentNode.rightChild) { this.insert(node, parentNode.rightChild); } else { parentNode.rightChild = node; node.parent = parentNode; this.size++; } } }; this.delete = function(node, parentNode) { if (this.rootNode === node) { throw "Cannot delete the root node."; } // Set parent node to root if not passed in if (!parentNode) { parentNode = this.rootNode; } // we found it; wire up parent and child nodes if (node.value == parentNode.value) { if (noChildrenNodes(parentNode)) { delete parentNode; this.size--; } else { if (parentNode.middleChild) { this.delete(node, parentNode.middleChild); } if (parentNode.leftChild && parentNode.rightChild) { if (parentNode.value > parentNode.parent.value) { parentNode.parent.rightChild = parentNode.leftChild; parentNode.leftChild.rightChild = parentNode.rightChild; } else { parentNode.parent.leftChild = parentNode.leftChild; parentNode.leftChild.rightNode = parentNode.rightChild; } delete parentNode; this.size--; } else if (parentNode.leftChild) { parentNode.parent.leftChild = parentNode.leftChild; delete parentNode; this.size--; } else if (parentNode.rightChild) { parentNode.parent.rightChild = parentNode.rightChild; delete parentNode; this.size--; } } // Node is less than parent } else if (node.value < parentNode.value) { if (parentNode.leftChild) { this.delete(node, parentNode.leftChild); } else { throw "Cannot delete node. Node does not exist in tree."; } // Node is greater than parent } else if (node.value > parentNode.value) { if (parentNode.rightChild) { this.delete(node, parentNode.rightChild); } else { throw "Cannot delete node. Node does not exist in tree."; } } }; var noChildrenNodes = function(node) { return node.leftChild == undefined && node.middleChild == undefined && node.rightChild == undefined; };
};
function Node(value) { this.parent = undefined; this.leftChild = undefined; this.middleChild = undefined; this.rightChild = undefined; this.value = value;
};
var node1 = new Node(5), node2 = new Node(4), node3 = new Node(9), node4 = new Node(5), node5 = new Node(7), node6 = new Node(10), node7 = new Node(2), node8 = new Node(2);
var tree = new TrinaryTree();
tree.initWithNodes([node1, node2, node3, node4, node5, node6, node7, node8]);
var console = document.querySelector(".console");
/** * Tests * * 5 * 4 5 9 * 2 7 8 * 2 */
/* should throw exception if node doesn't exit */
//tree.delete(new Node(12));
/* should not be able to delete root node */
//tree.delete(node1);
/* should decrement size when deleting node */
tree.delete(node3);
console.innerHTML = tree.size;
Developer | Mnicpt |
Username | mnicpt |
Uploaded | July 30, 2022 |
Rating | 3 |
Size | 2,282 Kb |
Views | 54,648 |
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 |
IOS 7 Layering | 2,931 Kb |
Graph API | 4,456 Kb |
Contact Snippet | 1,815 Kb |
A Pen by mnicpt | 3,851 Kb |
Mastermind | 5,601 Kb |
Apple Playing Cards | 4,618 Kb |
Balanced Binary Search Tree | 2,313 Kb |
Rotate Image | 1,843 Kb |
Plain JavaScript Calculator | 3,600 Kb |
Move Cell in Grid | 3,290 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 |
Mini Profile | Frytyler | 3,828 Kb |
Bootstrap Responsive Menu Drawer | JesseGlacken | 3,777 Kb |
Particle Motion trajectories | Sniejadlik | 5,899 Kb |
Portfolio Page | HuffmanJ25 | 5,240 Kb |
Foundation 5 Menu - Accessibility | Xporter | 1,999 Kb |
Twinkling Stars | Ripley6811 | 2,750 Kb |
Sample Profile Screen | OurDailyBread | 5,375 Kb |
Spin | Elalemanyo | 8,262 Kb |
We Simplify animation | Meodai | 48,169 Kb |
Zeichensatz | Moklick | 2,058 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!