-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1520 from Kamini8707/Merge2BSTs
Implement a Function to Merge Two Binary Search Trees (BSTs) #1441
- Loading branch information
Showing
2 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
Merge Two Binary Search Trees | ||
|
||
Description | ||
|
||
This project provides a solution to merge two Binary Search Trees (BSTs) into a single balanced BST. Merging two BSTs is a common operation in data structures, allowing the combination of two ordered datasets while maintaining efficient search properties. | ||
|
||
The algorithm involves: | ||
1. Performing in-order traversal on each BST to obtain sorted arrays of values. | ||
2. Merging the two sorted arrays into one sorted array. | ||
3. Building a balanced BST from the merged sorted array. | ||
This solution ensures that the final merged tree remains balanced, allowing efficient operations such as search, insertion, and deletion. | ||
|
||
Problem Statement | ||
|
||
Given two Binary Search Trees, we aim to merge them into a single BST that: | ||
- Contains all elements from both trees. | ||
- Maintains the properties of a Binary Search Tree. | ||
- Is balanced to optimize search efficiency. | ||
This solution is useful for applications where merging ordered data structures is required while preserving search efficiency. | ||
|
||
Approach and Solution | ||
|
||
Steps to Merge Two BSTs | ||
1. In-Order Traversal: | ||
- Perform an in-order traversal on both BSTs to extract elements in sorted order. | ||
- This gives two sorted arrays of values from the two trees. | ||
|
||
2. Merge Sorted Arrays: | ||
- Merge the two sorted arrays into one single sorted array. | ||
- This ensures that all elements from both trees are combined in sorted order. | ||
|
||
3. Build a Balanced BST: | ||
- Convert the merged sorted array into a balanced BST. | ||
- We achieve this by choosing the middle element of the array as the root and recursively building left and right subtrees, ensuring the tree is height-balanced. | ||
|
||
|
||
Algorithm Complexity | ||
|
||
- Time Complexity: | ||
- O(m + n) for in-order traversal (where m and n are the sizes of the two BSTs). | ||
- O(m + n) for merging two sorted arrays. | ||
- O(m + n) for building the balanced BST from a sorted array. | ||
- Overall Complexity: O(m + n). | ||
|
||
- Space Complexity: O(m + n), as we store values in arrays and construct a new BST. | ||
|
||
|
||
Example | ||
This example demonstrates the merging process of two BSTs, showing the trees before merging and the structure of the final merged BST. | ||
|
||
Input: | ||
1. BST 1: | ||
3 | ||
/ \ | ||
1 5 | ||
- In-order traversal of BST 1: 1 3 5 | ||
|
||
2. BST 2: | ||
4 | ||
/ \ | ||
2 6 | ||
- In-order traversal of BST 2: 2 4 6 | ||
|
||
|
||
Process: | ||
1. In-order Traversal: | ||
- First, we perform in-order traversal on both BSTs to obtain sorted arrays. | ||
- BST 1 in-order traversal gives: [1, 3, 5] | ||
- BST 2 in-order traversal gives: [2, 4, 6] | ||
|
||
2. Merge Sorted Arrays: | ||
- Next, we merge the two sorted arrays from the BSTs. | ||
- Merged array: [1, 2, 3, 4, 5, 6] | ||
|
||
3. Build Balanced BST: | ||
- We use the merged array to create a balanced BST: | ||
- Middle element 3 becomes the root. | ||
- Elements [1, 2] are on the left subtree, with 2 as the right child of 1. | ||
- Elements [4, 5, 6] are on the right subtree, with 5 as the right child of 4. | ||
-The final balanced BST is: | ||
3 | ||
/ \ | ||
1 5 | ||
\ / \ | ||
2 4 6 | ||
|
||
Output: | ||
After merging, the final in-order traversal of the balanced merged BST is: | ||
|
||
1 2 3 4 5 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct TreeNode { | ||
int value; | ||
struct TreeNode *left, *right; | ||
} TreeNode; | ||
|
||
// Create a new tree node | ||
TreeNode* createNode(int value) { | ||
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); | ||
newNode->value = value; | ||
newNode->left = newNode->right = NULL; | ||
return newNode; | ||
} | ||
|
||
// In-order traversal to store values in a sorted array | ||
void inorderTraversal(TreeNode* root, int* arr, int* index) { | ||
if (root == NULL) return; | ||
inorderTraversal(root->left, arr, index); | ||
arr[(*index)++] = root->value; | ||
inorderTraversal(root->right, arr, index); | ||
} | ||
|
||
// Merge two sorted arrays into one | ||
int* mergeSortedArrays(int* arr1, int size1, int* arr2, int size2, int* mergedSize) { | ||
*mergedSize = size1 + size2; | ||
int* mergedArr = (int*)malloc(*mergedSize * sizeof(int)); | ||
int i = 0, j = 0, k = 0; | ||
|
||
while (i < size1 && j < size2) { | ||
if (arr1[i] < arr2[j]) { | ||
mergedArr[k++] = arr1[i++]; | ||
} else { | ||
mergedArr[k++] = arr2[j++]; | ||
} | ||
} | ||
|
||
// Copy any remaining elements from arr1 | ||
while (i < size1) { | ||
mergedArr[k++] = arr1[i++]; | ||
} | ||
|
||
// Copy any remaining elements from arr2 | ||
while (j < size2) { | ||
mergedArr[k++] = arr2[j++]; | ||
} | ||
|
||
return mergedArr; | ||
} | ||
|
||
// Convert a sorted array to a balanced BST | ||
TreeNode* sortedArrayToBST(int* arr, int start, int end) { | ||
if (start > end) return NULL; | ||
|
||
int mid = (start + end) / 2; | ||
TreeNode* root = createNode(arr[mid]); | ||
|
||
root->left = sortedArrayToBST(arr, start, mid - 1); | ||
root->right = sortedArrayToBST(arr, mid + 1, end); | ||
|
||
return root; | ||
} | ||
|
||
// Merge two BSTs into a single balanced BST | ||
TreeNode* mergeTwoBSTs(TreeNode* root1, TreeNode* root2) { | ||
int arr1[100], arr2[100]; // Assuming max 100 nodes in each tree | ||
int index1 = 0, index2 = 0; | ||
|
||
// Get in-order traversal of both trees | ||
inorderTraversal(root1, arr1, &index1); | ||
inorderTraversal(root2, arr2, &index2); | ||
|
||
// Merge the two sorted arrays | ||
int mergedSize; | ||
int* mergedArr = mergeSortedArrays(arr1, index1, arr2, index2, &mergedSize); | ||
|
||
// Convert the merged sorted array into a balanced BST | ||
TreeNode* mergedBST = sortedArrayToBST(mergedArr, 0, mergedSize - 1); | ||
|
||
// Free the merged array after use | ||
free(mergedArr); | ||
|
||
return mergedBST; | ||
} | ||
|
||
// Utility function to print in-order traversal of a BST | ||
void printInOrder(TreeNode* root) { | ||
if (root == NULL) return; | ||
printInOrder(root->left); | ||
printf("%d ", root->value); | ||
printInOrder(root->right); | ||
} | ||
|
||
// Driver code to test the merge function | ||
int main() { | ||
// Creating two sample BSTs | ||
|
||
// BST 1: | ||
// 3 | ||
// / \ | ||
// 1 5 | ||
TreeNode* root1 = createNode(3); | ||
root1->left = createNode(1); | ||
root1->right = createNode(5); | ||
|
||
// BST 2: | ||
// 4 | ||
// / \ | ||
// 2 6 | ||
TreeNode* root2 = createNode(4); | ||
root2->left = createNode(2); | ||
root2->right = createNode(6); | ||
|
||
// Merge the two BSTs | ||
TreeNode* mergedBST = mergeTwoBSTs(root1, root2); | ||
|
||
// Print the in-order traversal of the merged BST | ||
printf("In-order traversal of the merged BST: "); | ||
printInOrder(mergedBST); | ||
|
||
return 0; | ||
} |