
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Searching for minimum and maximum values in an Javascript Binary Search Tree
In this article, we will explain how to find the minimum and maximum values in a binary search tree (BST), with implementation in JavaScript.
A binary search tree is a data structure that stores data in a sorted order such that for every node, the left subtree contains values less than the node's value, and the right subtree contains values greater than the node's value. So the leftmost node will have the minimum value, and the rightmost node will have the maximum value.
Find Minimum and Maximum in a BST
Given root node of a binary search tree, and your task is to find the node with the minimum value (leftmost node) and the maximum value (rightmost node). Consider the following binary search tree:
Scenario 1
Input: Root node: 10 10 / \ 5 15 / \ \ 3 7 20 Output: Min: 3 Max: 20
Explanation: The value of leftmost node in the given BST is 3, and the value of rightmost node is 20.
Scenario 2
Input: Root node: 8 8 \ 12 / \ 10 14 Output: Min: 8 Max: 14
Explanation: The root node itself is the leftmost node in this case, and it has value 8. The rightmost node is 14, which is the maximum value in the BST.
To solve this problem, we have two approaches:
- Brute Force Approach: Using Inorder Traversal
- Optimized Approach: Traversing Only Left/Right Subtree
In-order Traversal to Find Minimum and Maximum in BST
The inorder traversal of a binary search tree visits nodes in ascending order. Therefore, the first node visited during an in-order traversal will be the node with the minimum value and the last node visited will be the node with the maximum value.
- Define a function inorderTraversal that takes the root node as input.
- Inside the function use recursion to perform inorder traversal, and then return all visited nodes in a vector.
- Call the inorderTraversal function and store the result in a vector visitedNodes.
- Print the first and last element of the visitedNodes vector. This will give the minimum and maximum values in the BST.
Example: JavaScript Code
The code below implements the above algorithm to find the node with the minimum and maximum values in a binary search tree:
class Node { constructor(key) { this.left = null; this.right = null; this.value = key; } } function insert(root, key) { if (root === null) { return new Node(key); } if (key < root.value) { root.left = insert(root.left, key); } else { root.right = insert(root.right, key); } return root; } function inorder_traversal(root, visited_nodes) { if (root !== null) { inorder_traversal(root.left, visited_nodes); visited_nodes.push(root.value); inorder_traversal(root.right, visited_nodes); } } function find_min_value_node(root) { if (root === null) { return null; } let current = root; while (current.left !== null) { current = current.left; } return current; } function find_max_value_node(root) { if (root === null) { return null; } let current = root; while (current.right !== null) { current = current.right; } return current; } // Create a sample binary search tree let root = null; const values = [10, 5, 15, 3, 7, 20]; for (let v of values) { root = insert(root, v); } // Call the inorder_traversal function to get visited nodes let visited_nodes = []; inorder_traversal(root, visited_nodes); console.log("Minimum value in the BST:", visited_nodes[0]); console.log("Maximum value in the BST:", visited_nodes[visited_nodes.length - 1]);
The output of the above code will be:
Minimum value in the BST: 3 Maximum value in the BST: 20
Optimal Approach to Find Minimum and Maximum
The optimal way to find the minimum value in a binary search tree is to traverse only the left subtree. Since the left child of a node always contains a value less than its parent, the leftmost node will have the minimum value. Similarly we can find the maximum value by traversing the right subtree. Here is how you can implement this approach:
- Define a function findMinValueNode that takes the root node as input.
- Create a current pointer of type node and initialize it to the root node.
- Check if the root is null. If it is, return null (indicating the tree is empty).
- Use a while loop, that runs until current->left is not null. Inside the loop, update current to point to its left child.
- Once the loop ends, return the current node, which will be the leftmost node and thus the node with the minimum value.
- To find the maximum value, you can follow a similar approach but traverse the right subtree instead.
- Print the value of the node, if tree is not empty.
Example: JavaScript Code
Here is the JavaScript code that implements the optimized approach to find the node with the minimum value in a binary search tree:
class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } function find_min_value_node(root) { if (root === null) { return null; } let current = root; while (current.left !== null) { current = current.left; } return current; } function find_max_value_node(root) { if (root === null) { return null; } let current = root; while (current.right !== null) { current = current.right; } return current; } // Create a sample binary search tree root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); root.right.right = new Node(20); // Call the find_min_value_node function let min_node = find_min_value_node(root); // Print the result if the tree is not empty if (min_node !== null) { console.log("Minimum value in the BST:", min_node.value); } else { console.log("The tree is empty."); } // Call the find_max_value_node function let max_node = find_max_value_node(root); // Print the result if (max_node !== null) { console.log("Maximum value in the BST:", max_node.value); } else { console.log("The tree is empty."); }
The output of the above code will be:
Minimum value in the BST: 3 Maximum value in the BST: 20
Time Complexity and Space Complexity
Here is a comparison of the time complexity and space complexity of the above mentioned approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Brute Force | O(N) | O(N) |
Optimized Traversal | O(H) | O(1) |
Where N is the number of nodes in the BST and H is the height of the BST.