Skip to content

Commit 82eab37

Browse files
Merge pull request #1 from amritansh22/master
update
2 parents 3c0a384 + 38c0875 commit 82eab37

8 files changed

+548
-0
lines changed

Diff for: Binary Matrix.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
#include<math.h>
4+
#include<vector>
5+
using namespace std;
6+
int main()
7+
{
8+
int n,m;
9+
cin>>n>>m;
10+
string s[n],s1[n];
11+
for(int i=0;i<n;i++)
12+
{
13+
cin>>s[i];
14+
s1[i]=s[i];
15+
}
16+
sort(s,s+n,greater<string>());
17+
string temp=s[0];
18+
for(int i=0;i<n;i++)
19+
{
20+
if(s1[i]==temp)
21+
{cout<<i+1<<endl;
22+
break;
23+
}
24+
}
25+
26+
27+
}

Diff for: BinarySearchTree.cpp

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
//Implement Binary Search Tree and Perform Operations : Insertion, Deletion, Searching (Issue #107)
2+
//https://github.com/amritansh22/Data-Structures-and-Algorithms-in-cpp/issues/107
3+
//Contributed by @tauseefmohammed2 : https://github.com/tauseefmohammed2
4+
5+
#include<iostream>
6+
using namespace std;
7+
8+
class BinarySearchTree {
9+
10+
struct BSTnode {
11+
int data;
12+
BSTnode* left;
13+
BSTnode* right;
14+
};
15+
16+
BSTnode* root;
17+
18+
//Function to Clear a Node
19+
BSTnode* clear(BSTnode* t) {
20+
if(t == NULL)
21+
return NULL;
22+
{
23+
clear(t->left);
24+
clear(t->right);
25+
delete t;
26+
}
27+
return NULL;
28+
}
29+
30+
//Function to Insert a Node
31+
BSTnode* insert(int x, BSTnode* t)
32+
{
33+
if(t == NULL)
34+
{
35+
t = new BSTnode;
36+
t->data = x;
37+
t->left = t->right = NULL;
38+
}
39+
else if(x < t->data)
40+
t->left = insert(x, t->left);
41+
else if(x > t->data)
42+
t->right = insert(x, t->right);
43+
return t;
44+
}
45+
46+
//Function to Obtain the Node with the Minimum Value
47+
BSTnode* min(BSTnode* t)
48+
{
49+
if(t == NULL)
50+
return NULL;
51+
else if(t->left == NULL)
52+
return t;
53+
else
54+
return min(t->left);
55+
}
56+
57+
//Function to Obtain the Node with the Maximum Value
58+
BSTnode* max(BSTnode* t) {
59+
if(t == NULL)
60+
return NULL;
61+
else if(t->right == NULL)
62+
return t;
63+
else
64+
return max(t->right);
65+
}
66+
67+
//Function to Delete/Remove a Node from the BST
68+
BSTnode* remove(int x, BSTnode* t) {
69+
BSTnode* temp;
70+
if(t == NULL)
71+
return NULL;
72+
else if(x < t->data)
73+
t->left = remove(x, t->left);
74+
else if(x > t->data)
75+
t->right = remove(x, t->right);
76+
else if(t->left && t->right)
77+
{
78+
temp = min(t->right);
79+
t->data = temp->data;
80+
t->right = remove(t->data, t->right);
81+
}
82+
else
83+
{
84+
temp = t;
85+
if(t->left == NULL)
86+
t = t->right;
87+
else if(t->right == NULL)
88+
t = t->left;
89+
delete temp;
90+
}
91+
92+
return t;
93+
}
94+
95+
//Function to Display Inorder Traversal of the BST
96+
void inorder_traversal(BSTnode* t) {
97+
if(t == NULL)
98+
return;
99+
inorder_traversal(t->left);
100+
cout << t->data << " ";
101+
inorder_traversal(t->right);
102+
}
103+
104+
//Function to Search and Check whether a Given Node is Present in the BST or Not
105+
BSTnode* search(BSTnode* t, int x) {
106+
if(t == NULL)
107+
return NULL;
108+
else if(x < t->data)
109+
return search(t->left, x);
110+
else if(x > t->data)
111+
return search(t->right, x);
112+
else
113+
return t;
114+
}
115+
116+
public:
117+
//Creating Corresponding Functions for the Object to Acccess Outside the Class
118+
BinarySearchTree() {
119+
root = NULL;
120+
}
121+
122+
~BinarySearchTree() {
123+
root = clear(root);
124+
}
125+
126+
void insert(int x) {
127+
root = insert(x, root);
128+
}
129+
130+
void remove(int x) {
131+
root = remove(x, root);
132+
}
133+
134+
void display() {
135+
inorder_traversal(root);
136+
cout << endl;
137+
}
138+
139+
void search(int x) {
140+
root = search(root, x);
141+
if(root == NULL)
142+
cout << endl << x <<" is Not Present in the BST";
143+
else
144+
cout<< endl << x <<" is Present in the BST";
145+
}
146+
};
147+
148+
int main() {
149+
//Creating Object of BinarySearchTree Class
150+
BinarySearchTree t;
151+
152+
//Inserts the Following Nodes into the BST
153+
t.insert(20);
154+
t.insert(25);
155+
t.insert(15);
156+
t.insert(10);
157+
t.insert(30);
158+
159+
//Displays Inorder Traversal of BST
160+
t.display();
161+
162+
//Remove 20 and 25 From the BST and Display Corresponding Inorder Traversal
163+
t.remove(20);
164+
t.display();
165+
t.remove(25);
166+
t.display();
167+
168+
t.search(15);
169+
return 0;
170+
}
171+
172+
//Output :
173+
//10 15 20 25 30
174+
//10 15 25 30
175+
//10 15 30
176+
//15 is Present in the BST
177+
//20 is Not Present in the BST
178+
179+
//Time Complexity :
180+
//Insertion : O(h) [h = Height of BST]
181+
//Deletion : O(h) [h = Height of BST]
182+
//Searching : O(h) [h = Height of BST]
183+
184+
//Space Complexity :
185+
//O(n) [n = Number of Nodes in BST]

Diff for: ExtendedEuclidean.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int extended_Euclidean(int a, int b, int* x, int *y){
5+
6+
if(a == 0){
7+
*y = 1;
8+
*x = 0;
9+
return b;
10+
}
11+
12+
else {
13+
int x1, y1;
14+
int gcd = extended_Euclidean(b%a, a, &x1, &y1);
15+
*x = y1 - (b/a) * x1;
16+
*y = x1;
17+
return gcd;
18+
}
19+
}
20+
21+
int main(){
22+
23+
int x, y, n1, n2;
24+
cin >> n1 >> n2;
25+
26+
int gcd = extended_Euclidean(n1, n2, &x, &y);
27+
28+
printf("(%d)*%d + (%d)*%d = %d", x, n1, y, n2, gcd);
29+
}

Diff for: Find length circular linked list.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Node {
5+
int data;
6+
struct Node* next;
7+
};
8+
9+
void push(struct Node** head_ref, int data)
10+
{
11+
struct Node* ptr1 = (struct Node*)malloc(sizeof(struct Node));
12+
struct Node* temp = *head_ref;
13+
ptr1->data = data;
14+
ptr1->next = *head_ref;
15+
16+
if (*head_ref != NULL) {
17+
while (temp->next != *head_ref)
18+
temp = temp->next;
19+
temp->next = ptr1;
20+
} else
21+
ptr1->next = ptr1;
22+
23+
*head_ref = ptr1;
24+
}
25+
26+
int countNodes(struct Node* head)
27+
{
28+
struct Node* temp = head;
29+
int result = 0;
30+
if (head != NULL) {
31+
do {
32+
temp = temp->next;
33+
result++;
34+
} while (temp != head);
35+
}
36+
37+
return result;
38+
}
39+
40+
int main()
41+
{
42+
struct Node* head = NULL;
43+
push(&head, 12);
44+
push(&head, 56);
45+
push(&head, 2);
46+
push(&head, 11);
47+
48+
printf("%d", countNodes(head));
49+
50+
return 0;
51+
}
52+

Diff for: Reverse an array.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
int n;
6+
cin>>n;
7+
int arr[n];
8+
for (int i=0;i<n;i++){
9+
cin>>arr[i];
10+
}
11+
12+
int start=0, end=n-1;
13+
14+
while(end>start){
15+
int temp=arr[start];
16+
arr[start]=arr[end];
17+
arr[end]=temp;
18+
start++;
19+
end--;
20+
}
21+
22+
for (int i=0;i<n;i++){
23+
cout<<arr[i]<<" ";
24+
}
25+
26+
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)