Skip to content

Commit e1e23c7

Browse files
authored
Create main.cpp
1 parent 39bdfb5 commit e1e23c7

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Node {
5+
public:
6+
int data; // Data value of the node
7+
Node* left; // Pointer to the left child
8+
Node* right; // Pointer to the right child
9+
10+
// Constructor to initialize a node with given value
11+
Node(int val) : data(val), left(nullptr), right(nullptr) {}
12+
};
13+
14+
// Function to insert a node into a Binary Search Tree (BST)
15+
Node* insertBST(Node* root, int val) {
16+
if (root == nullptr) // If the tree/subtree is empty, create a new node
17+
return new Node(val);
18+
if (val < root->data) // If value is less, insert in the left subtree
19+
root->left = insertBST(root->left, val);
20+
else // Otherwise, insert in the right subtree
21+
root->right = insertBST(root->right, val);
22+
return root;
23+
}
24+
25+
// Convert a BST to a sorted doubly linked list
26+
void convertIntoSortedDLL(Node* root, Node* &head) {
27+
if (root == nullptr) return; // Base case for recursion
28+
29+
convertIntoSortedDLL(root->right, head); // Process the right subtree first
30+
31+
root->right = head; // Link current node's right to head of sorted DLL
32+
if (head != nullptr) head->left = root; // Set head's left to current node
33+
34+
head = root; // Update head to current node
35+
36+
convertIntoSortedDLL(root->left, head); // Process the left subtree
37+
}
38+
39+
// Merge two sorted doubly linked lists into a single sorted list
40+
Node* mergeLinkedList(Node* head1, Node* head2) {
41+
Node* head = nullptr; // Head of the merged list
42+
Node* tail = nullptr; // Tail of the merged list
43+
44+
// Merge nodes from both lists until one list is exhausted
45+
while (head1 != nullptr && head2 != nullptr) {
46+
if (head1->data < head2->data) { // Add node from head1 if it has smaller data
47+
if (head == nullptr) { // First node in merged list
48+
head = head1;
49+
tail = head1;
50+
head1 = head1->right;
51+
} else { // Subsequent nodes
52+
tail->right = head1;
53+
head1->left = tail;
54+
tail = head1;
55+
head1 = head1->right;
56+
}
57+
} else { // Add node from head2 otherwise
58+
if (head == nullptr) { // First node in merged list
59+
head = head2;
60+
tail = head2;
61+
head2 = head2->right;
62+
} else { // Subsequent nodes
63+
tail->right = head2;
64+
head2->left = tail;
65+
tail = head2;
66+
head2 = head2->right;
67+
}
68+
}
69+
}
70+
71+
// Append remaining nodes of head1
72+
while (head1 != nullptr) {
73+
tail->right = head1;
74+
head1->left = tail;
75+
tail = head1;
76+
head1 = head1->right;
77+
}
78+
79+
// Append remaining nodes of head2
80+
while (head2 != nullptr) {
81+
tail->right = head2;
82+
head2->left = tail;
83+
tail = head2;
84+
head2 = head2->right;
85+
}
86+
87+
return head; // Return the head of merged list
88+
}
89+
90+
// Count the number of nodes in a doubly linked list
91+
int countNodes(Node* head) {
92+
int count = 0;
93+
Node* temp = head;
94+
while (temp != nullptr) { // Traverse the list to count nodes
95+
count++;
96+
temp = temp->right;
97+
}
98+
return count;
99+
}
100+
101+
// Convert sorted doubly linked list to a balanced BST
102+
Node* sortedLLToBST(Node* &head, int n) {
103+
if (n <= 0 || head == nullptr) return nullptr; // Base case for recursion
104+
105+
Node* left = sortedLLToBST(head, n / 2); // Recursively create left subtree
106+
107+
Node* root = head; // Current node becomes root
108+
root->left = left; // Connect left subtree
109+
110+
head = head->right; // Move head to the next node
111+
112+
root->right = sortedLLToBST(head, n - n / 2 - 1); // Recursively create right subtree
113+
114+
return root;
115+
}
116+
117+
// Print the BST in in-order traversal (for verification)
118+
void printInOrder(Node* root) {
119+
if (root == nullptr) return;
120+
121+
printInOrder(root->left); // Visit left subtree
122+
cout << root->data << " "; // Print root node
123+
printInOrder(root->right); // Visit right subtree
124+
}
125+
126+
int main() {
127+
// Create first BST
128+
Node* root1 = nullptr;
129+
root1 = insertBST(root1, 3);
130+
root1 = insertBST(root1, 1);
131+
root1 = insertBST(root1, 5);
132+
133+
// Create second BST
134+
Node* root2 = nullptr;
135+
root2 = insertBST(root2, 4);
136+
root2 = insertBST(root2, 2);
137+
root2 = insertBST(root2, 6);
138+
139+
Node* head1 = nullptr;
140+
Node* head2 = nullptr;
141+
142+
// Convert BSTs to sorted doubly linked lists
143+
convertIntoSortedDLL(root1, head1);
144+
head1->left = nullptr; // Set left of the head to null
145+
146+
convertIntoSortedDLL(root2, head2);
147+
head2->left = nullptr; // Set left of the head to null
148+
149+
// Merge the two sorted doubly linked lists
150+
Node* mergedHead = mergeLinkedList(head1, head2);
151+
152+
// Convert merged sorted linked list back to a balanced BST
153+
Node* mergedBST = sortedLLToBST(mergedHead, countNodes(mergedHead));
154+
155+
// Print the in-order traversal of the merged BST
156+
cout << "In-order traversal of merged BST: ";
157+
printInOrder(mergedBST);
158+
cout << endl;
159+
160+
return 0;
161+
}

0 commit comments

Comments
 (0)