Balanced Binary Search Tree
How do I make an balanced binary search tree?
Created a balanced binary search tree from a sorted array.. What is a balanced binary search tree? How do you make a balanced binary search tree? This script and codes were developed by Mnicpt on 30 July 2022, Saturday.
Balanced Binary Search Tree - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Balanced Binary Search Tree</title> <script src="http://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</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>
Balanced Binary Search Tree - Script Codes JS Codes
var Node = function(value) { this.left = {}; this.right = {}; this.visited = false; this.value = value;
};
function balancedBinarySearchTree(arr) { if (arr.length == 0) return; var middle = Math.floor(arr.length / 2), left = arr.slice(0, middle), right = arr.slice(middle + 1), node = arr[middle]; node.left = balancedBinarySearchTree(left); node.right = balancedBinarySearchTree(right); return node;
}
/* Depth-first search */
function dfs(parent, node) { var found; if (parent.value == node) { return parent; } else if (parent.left && parent.right) { if (found = dfs(parent.left, node)) { return found; } if( found = dfs(parent.right, node)) { return found; } } else if (parent.left) { if (found = dfs(parent.left, node)) { return found; } } else if (parent.right) { if( found = dfs(parent.right, node)) { return found; } }
}
/* Breadth-first search */
var children = [];
function bfs(parents, node) { var i = 0, j = 0, found = false, child; // root node check if (parents.length == 1 && parents[0] == node) { return parents[0]; } else if (children.length > 0) { while (children.length != 0) { if (children.shift(child).value == node) { return children[child]; } } } // store children in queue for (j; j < parents.length; j++) { parent = parents[j]; console.log("Parent: " +parent.value) if(parent.left && parent.right) { children.push(parent.left); children.push(parent.right); } else if (parent.left) { children.push(parent.left); } else if (parent.right) { children.push(parent.right); } else { throw ("Node of value " +node+ " was not found."); } } if (found = bfs(children, node)) { return found; }
}
var output = document.querySelector(".console"), node1 = new Node(3), node2 = new Node(5), node3 = new Node(15), node4 = new Node(25), node5 = new Node(35), node6 = new Node(44), node7 = new Node(46), node8 = new Node(54), node9 = new Node(55), node10 = new Node(60), node11 = new Node(75), node12 = new Node(77), node13 = new Node(89);
var rootNode = balancedBinarySearchTree([node1, node2, node3, node4, node5, node6, node7, node8, node9, node10, node11, node12, node13]);
var forNum = 77;
var dfsNode = dfs(rootNode.left, forNum);
if (!dfsNode) { dfsNode = dfs(rootNode.right, forNum);
}
output.innerHTML = dfsNode.value;
var bfsNode = bfs([rootNode], forNum);
console.innerHTML += " " + bfsNode;
Developer | Mnicpt |
Username | mnicpt |
Uploaded | July 30, 2022 |
Rating | 3 |
Size | 2,313 Kb |
Views | 34,408 |
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 |
Model View Sync | 1,733 Kb |
Sketchy Box | 3,033 Kb |
JavaScript Aspect | 1,648 Kb |
Trinary Tree | 2,282 Kb |
Solve Intersecting Lines | 1,792 Kb |
Quick Sort | 7,584 Kb |
Mastermind | 5,601 Kb |
Contact Snippet | 1,815 Kb |
First 100 Prime Numbers | 1,615 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 |
Simple jQuery Slider | Jurbank | 2,874 Kb |
Funny menu | AxeLVaisper | 4,671 Kb |
Basic HTML5 Structure | YuvarajTana | 1,289 Kb |
Email Marketing Mock | Kristenzirkler | 8,224 Kb |
Multi-step GSAP animation | Acacheung | 2,668 Kb |
Arrow Navigation | Hinducows | 1,973 Kb |
Testing Portfolio Page | Sideshowli | 3,395 Kb |
CSS Link Icons with jQuery Titles | Nicwinn | 2,312 Kb |
Carousel Test | Sbirchall | 1,509 Kb |
Angular Route | Arun_v606 | 1,837 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!