Binary Search Tree
How do I make an binary search tree?
What is a binary search tree? How do you make a binary search tree? This script and codes were developed by Beau Carnes on 09 September 2022, Friday.
Binary Search Tree - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Binary Search Tree</title> <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="container"> <div class="row"> <br><br><br><br><img src="https://www.cpp.edu/~ftang/courses/CS241/notes/images/trees/03.jpg"> <br> <br> <br> <img src="https://www.cpp.edu/~ftang/courses/CS241/notes/images/trees/tree1.bmp"> <br> <br> <br> <img src="https://2.bp.blogspot.com/-CApCcTOe1A0/TwppaUWiQsI/AAAAAAAABX4/sJv92_ZJzhE/s1600/Tree+Term.gif" class="img-responsive"> <br> <br> <br> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/Tree-data-structure.svg/800px-Tree-data-structure.svg.png" class="img-responsive"> <br> <br> <br> <img src="http://www.zentut.com/wp-content/uploads/2013/04/bst-remove-node-with-2-children.png" class="img-responsive"> <br> <br> <br> <img src="https://cramster-image.s3.amazonaws.com/definitions/computerscience-5-img-1.png" class="img-responsive"> </div>
</div> <script src="js/index.js"></script>
</body>
</html>
Binary Search Tree - Script Codes CSS Codes
img { width: 100%; display: block; }
Binary Search Tree - Script Codes JS Codes
/* Binary Search Tree */
class Node { constructor(data, left = null, right = null) { this.data = data; this.left = left; this.right = right; }
}
class BST { constructor() { this.root = null; } add(data) { const node = this.root; if (node === null) { this.root = new Node(data); return; } else { const searchTree = function(node) { if (data < node.data) { if (node.left === null) { node.left = new Node(data); return; } else if (node.left !== null) { return searchTree(node.left); } } else if (data > node.data) { if (node.right === null) { node.right = new Node(data); return; } else if (node.right !== null) { return searchTree(node.right); } } else { return null; } }; return searchTree(node); } } findMin() { let current = this.root; while (current.left !== null) { current = current.left; } return current.data; } findMax() { let current = this.root; while (current.right !== null) { current = current.right; } return current.data; } find(data) { let current = this.root; while (current.data !== data) { if (data < current.data) { current = current.left; } else { current = current.right; } if (current === null) { return null; } } return current; } isPresent(data) { let current = this.root; while (current) { if (data === current.data) { return true; } if (data < current.data) { current = current.left; } else { current = current.right; } } return false; } remove(data) { const removeNode = function(node, data) { if (node == null) { return null; } if (data == node.data) { // node has no children if (node.left == null && node.right == null) { return null; } // node has no left child if (node.left == null) { return node.right; } // node has no right child if (node.right == null) { return node.left; } // node has two children var tempNode = node.right; while (tempNode.left !== null) { tempNode = tempNode.left; } node.data = tempNode.data; node.right = removeNode(node.right, tempNode.data); return node; } else if (data < node.data) { node.left = removeNode(node.left, data); return node; } else { node.right = removeNode(node.right, data); return node; } } this.root = removeNode(this.root, data); } isBalanced() { return (this.findMinHeight() >= this.findMaxHeight() - 1) } findMinHeight(node = this.root) { if (node == null) { return -1; }; let left = this.findMinHeight(node.left); let right = this.findMinHeight(node.right); if (left < right) { return left + 1; } else { return right + 1; }; } findMaxHeight(node = this.root) { if (node == null) { return -1; }; let left = this.findMaxHeight(node.left); let right = this.findMaxHeight(node.right); if (left > right) { return left + 1; } else { return right + 1; }; } inOrder() { if (this.root == null) { return null; } else { var result = new Array(); function traverseInOrder(node) { node.left && traverseInOrder(node.left); result.push(node.data); node.right && traverseInOrder(node.right); } traverseInOrder(this.root); return result; }; } preOrder() { if (this.root == null) { return null; } else { var result = new Array(); function traversePreOrder(node) { result.push(node.data); node.left && traversePreOrder(node.left); node.right && traversePreOrder(node.right); }; traversePreOrder(this.root); return result; }; } postOrder() { if (this.root == null) { return null; } else { var result = new Array(); function traversePostOrder(node) { node.left && traversePostOrder(node.left); node.right && traversePostOrder(node.right); result.push(node.data); }; traversePostOrder(this.root); return result; } } levelOrder() { let result = []; let Q = []; if (this.root != null) { Q.push(this.root); while(Q.length > 0) { let node = Q.shift(); result.push(node.data); if (node.left != null) { Q.push(node.left); }; if (node.right != null) { Q.push(node.right); }; }; return result; } else { return null; }; };
}
const bst = new BST();
bst.add(9);
bst.add(4);
bst.add(17);
bst.add(3);
bst.add(6);
bst.add(22);
bst.add(5);
bst.add(7);
bst.add(20);
console.log(bst.findMinHeight());
console.log(bst.findMaxHeight());
console.log(bst.isBalanced());
bst.add(10);
console.log(bst.findMinHeight());
console.log(bst.findMaxHeight());
console.log(bst.isBalanced());
console.log('inOrder: ' + bst.inOrder());
console.log('preOrder: ' + bst.preOrder());
console.log('postOrder: ' + bst.postOrder());
console.log('levelOrder: ' + bst.levelOrder());
Developer | Beau Carnes |
Username | beaucarnes |
Uploaded | September 09, 2022 |
Rating | 3 |
Size | 2,920 Kb |
Views | 26,312 |
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 |
Trie | 2,086 Kb |
Camper Leaderboard | 4,550 Kb |
Maps | 1,729 Kb |
Bar Chart - D3 | 3,411 Kb |
Const vs Let vs Var | 1,500 Kb |
String Methods | 1,672 Kb |
Functions | 1,490 Kb |
Game of Life | 5,915 Kb |
This | 1,717 Kb |
Singleton Design Pattern | 1,761 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 |
Vue.js Starter | Andymerskin | 1,268 Kb |
Minimelephant | Smashlee | 2,507 Kb |
Flat UI - Checkbox FIX | ARS | 2,663 Kb |
CSS3 Form | Tusharbandal | 1,836 Kb |
Rows with image hover effect | Amit-webdesigner | 12,875 Kb |
Nice textured background | Hans | 2,659 Kb |
NeeilTimer | Neeilan | 2,836 Kb |
Cut and Paste Roll Link | BottomlineInteractive | 2,546 Kb |
A Pen by Moeid Saleem | Moeidsaleem | 1,862 Kb |
Two joint circles - One element | Berdejitendra | 1,704 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!