Generate two BSTs from the given array such that maximum height among them is minimum
Given an array of n integers, the task is to make two Binary Search Tree from the given array (in any order) such that the maximum height among the two trees is minimum possible and print the maximum height.
Examples:
Input: arr[] = {1, 2, 3, 4, 6}
Output: 1
Input: arr[] = { 74, 25, 23, 1, 65, 2, 8, 99 }
Output: 2
Approach: The aim is to minimize the height of the maximum height binary search tree and to do so we need to divide the array elements equally among both the trees. And since the order doesn’t matter, we can easily choose any element for the first or second binary tree. Now, to minimize the height of the two tree, the trees will have to be almost complete and as equal in heights as possible. And the maximum (minimized) height will be log(n/2) or log(n/2 + 1).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to calculate the log() int cal_log(int n) { int ans = 0; while (n) { n /= 2; ans++; } return ans; } // Function to return the maximum // height of the tree int maximumHeight(int n, int arr[]) { int level = cal_log(n / 2 + n % 2); return (level - 1); } // Driven code int main() { int arr[] = { 1, 2, 3, 4, 6 }; int n = sizeof(arr) / sizeof(arr[0]); cout << maximumHeight(n, arr); return 0; } |
Java
// Java implementation of the approach import java.io.*; class GFG { // Function to calculate the log() static int cal_log(int n) { int ans = 0; while (n > 0) { n /= 2; ans++; } return ans; } // Function to return the maximum // height of the tree static int maximumHeight(int n, int arr[]) { int level = cal_log(n / 2 + n % 2); return (level - 1); } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 3, 4, 6 }; int n =arr.length; System.out.print( maximumHeight(n, arr)); } } // This code is contributed by anuj_67.. |
C#
// C# implementation of the approach using System; class GFG { // Function to calculate the log() static int cal_log(int n) { int ans = 0; while (n > 0) { n /= 2; ans++; } return ans; } // Function to return the maximum // height of the tree static int maximumHeight(int n, int []arr) { int level = cal_log(n / 2 + n % 2); return (level - 1); } // Driver code public static void Main () { int []arr = { 1, 2, 3, 4, 6 }; int n =arr.Length; Console.WriteLine( maximumHeight(n, arr)); } } // This code is contributed by anuj_67.. |
Python3
# Python implementation of the approach # Function to calculate the log() def cal_log(n): ans = 0; while (n): n //= 2; ans+=1; return ans; # Function to return the maximum # height of the tree def maximumHeight(n, arr): level = cal_log(n // 2 + n % 2); return (level - 1); # Driver code arr = [ 1, 2, 3, 4, 6 ]; n = len(arr); print(maximumHeight(n, arr)); # This code is contributed by Princi Singh |
1
Recommended Posts:
- Generate array with minimum sum which can be deleted in P steps
- Maximum height of triangular arrangement of array values
- Maximum and minimum of an array using minimum number of comparisons
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Product of maximum in first array and minimum in second
- Rearrange an array in maximum minimum form | Set 1
- Minimum and Maximum prime numbers in an array
- Sum and Product of minimum and maximum element of an Array
- Minimum and Maximum element of an array which is divisible by a given number k
- Minimize the maximum minimum difference after one removal from array
- Choose k array elements such that difference of maximum and minimum is minimized
- Rearrange an array in maximum minimum form | Set 2 (O(1) extra space)
- Minimize the difference between the maximum and minimum values of the modified array
- Leftmost and rightmost indices of the maximum and the minimum element of an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





