Trinary Tree

Developer
Size
2,282 Kb
Views
54,648

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 Previews

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;
Trinary Tree - Script Codes
Trinary Tree - Script Codes
Home Page Home
Developer Mnicpt
Username mnicpt
Uploaded July 30, 2022
Rating 3
Size 2,282 Kb
Views 54,648
Do you need developer help for Trinary 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!

Mnicpt (mnicpt) 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!