Skip to content

Commit a5ddb5b

Browse files
authored
Merge pull request #185 from me-ydv-5/master
Added Flattening of Binary Tree to Linked List and Range search of an element in a sorted array.
2 parents 44c019d + 04af1b4 commit a5ddb5b

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Find the range of a particular element occuring in a given sorted array.
2+
// If the element doesn't exist, return 1.
3+
4+
#include <bits/stdc++.h>
5+
6+
using namespace std;
7+
8+
int findleft(const vector<int>& A, int B, int low, int high){
9+
int res = -1;
10+
while(low <= high){
11+
int mid = low + (high - low)/2;
12+
if(A[mid] == B){
13+
res = mid;
14+
high = mid-1;
15+
}else if(A[mid] < B){
16+
low = mid+1;
17+
}else high = mid-1;
18+
}
19+
if(res == -1)
20+
return res;
21+
return low;
22+
}
23+
24+
int findright(const vector<int>& A, int B, int low, int high){
25+
int res = -1;
26+
while(low <= high){
27+
int mid = low + (high - low)/2;
28+
if(A[mid] == B){
29+
res = mid;
30+
low = mid+1;
31+
}else if(A[mid] > B){
32+
high = mid-1;
33+
}else low = mid+1;
34+
}
35+
if(res == -1)return -1;
36+
return low-1;
37+
}
38+
39+
int main(int argc, char const *argv[])
40+
{
41+
std::vector<int> v({1,2,2,3,3,3,4,4,4,4,5,6,7,8,8,8,8,8});
42+
43+
cout << "The input vector is: " << endl;
44+
for(auto i: v){
45+
cout << i << " ";
46+
}
47+
cout << endl;
48+
49+
cout << "The first and last index of occurance of element '8' is : " << findleft(v, 8, 0, v.size()-1)
50+
<< " "
51+
<< findright(v, 8, 0, v.size()-1)
52+
<< endl;
53+
54+
cout << "The first and last index of occurance of element '1' is : " << findleft(v, 1, 0, v.size()-1)
55+
<< " "
56+
<< findright(v, 1, 0, v.size()-1)
57+
<< endl;
58+
59+
cout << "The first and last index of occurance of element '10' is : " << findleft(v, 10, 0, v.size()-1)
60+
<< " "
61+
<< findright(v, 10, 0, v.size()-1)
62+
<< endl;
63+
64+
return 0;
65+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Node{
6+
public:
7+
Node* left;
8+
Node* right;
9+
int data;
10+
Node(int x){
11+
data = x;
12+
left = NULL;
13+
right = NULL;
14+
}
15+
};
16+
17+
class Solution{
18+
public:
19+
Node* insert(Node* root, int data) {
20+
if(root == NULL) {
21+
return new Node(data);
22+
} else {
23+
Node* cur;
24+
if(data <= root->data) {
25+
cur = insert(root->left, data);
26+
root->left = cur;
27+
} else {
28+
cur = insert(root->right, data);
29+
root->right = cur;
30+
}
31+
32+
return root;
33+
}
34+
}
35+
36+
Node* flatten(Node* A) {
37+
if(!A)return NULL;
38+
39+
// Take the left and right subtree
40+
Node* tempr = A->right;
41+
Node* templ = A->left;
42+
43+
// Recursively Flatten them
44+
flatten(templ);
45+
flatten(tempr);
46+
47+
// If left subtree exists, place it as the right subtree and make the right subtree
48+
// as the child of this new right subtree
49+
if(templ){
50+
A->right = templ;
51+
Node* x = templ;
52+
// Go to the end of the right subtree (which was earlier left subtree)
53+
while(x->right){
54+
x = x->right;
55+
}
56+
// Place the earlier right subtree as the right subtree of the current right subtree
57+
x->right = tempr;
58+
59+
// Make the new left subtree as NULL
60+
templ->left = NULL;
61+
}
62+
63+
// Nullify the left subtree.
64+
A->left = NULL;
65+
66+
// Return the tree.
67+
return A;
68+
}
69+
70+
void inOrder(Node* tree){
71+
if(!tree) return;
72+
73+
inOrder(tree->left);
74+
cout << tree->data << " " ;
75+
inOrder(tree->right);
76+
}
77+
};
78+
79+
// Initial Tree:
80+
// 4
81+
// / \
82+
// 2 7
83+
// / \ /
84+
// 1 3 6
85+
86+
// Final Tree:
87+
// 4
88+
// / \
89+
// N 2
90+
// / \
91+
// N 1
92+
// / \
93+
// N 3
94+
// / \
95+
// N 7
96+
// / \
97+
// N 6
98+
// N - NULL
99+
100+
101+
int main() {
102+
Solution myTree;
103+
Node* root = NULL;
104+
root = myTree.insert(root,4);
105+
root = myTree.insert(root,2);
106+
root = myTree.insert(root,3);
107+
root = myTree.insert(root,1);
108+
root = myTree.insert(root,7);
109+
root = myTree.insert(root,6);
110+
111+
cout << "InOrder Traversal is of the tree is as follows: " << endl;
112+
myTree.inOrder(root);
113+
cout << endl;
114+
115+
116+
myTree.flatten(root);
117+
cout << "\n\nInOrder Traversal is of the flattened tree is as follows: " << endl;
118+
myTree.inOrder(root);
119+
cout << endl;
120+
return 0;
121+
}
122+

0 commit comments

Comments
 (0)