Balanced Binary Search Tree

Developer
Size
2,313 Kb
Views
34,408

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 Previews

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;
Balanced Binary Search Tree - Script Codes
Balanced Binary Search Tree - Script Codes
Home Page Home
Developer Mnicpt
Username mnicpt
Uploaded July 30, 2022
Rating 3
Size 2,313 Kb
Views 34,408
Do you need developer help for Balanced Binary Search 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 marketing copy 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!