Skip to content

Commit b6a8ed1

Browse files
committed
Problems related to the BT
1 parent 13e5da6 commit b6a8ed1

File tree

18 files changed

+691
-7
lines changed

18 files changed

+691
-7
lines changed

Array/Array.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _ARRAY_H_
2+
#define _ARRAY_H_
3+
4+
#include<iostream>
5+
#include<algorithm>
6+
/*
7+
* PRINT API
8+
*/
9+
#define PRINT_ARRAY(array, size)\
10+
std::for_each(array, array+size, [](int a){ std::cout <<a <<" "; });\
11+
std::cout <<std::endl;
12+
13+
/*
14+
* FIND APIs
15+
*/
16+
void find_k_smallest_elements_using_modified_bubble_sort(int array[], int size, int k);
17+
void find_k_smallest_elements_using_sorting(int array[], int size, int k);
18+
19+
int find_smallest_missing_number(int array[], int size);
20+
21+
void find_all_triplets_with_sum_zero(int array[], int size);
22+
23+
#endif

Array/Find.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include<climits>
2+
#include"Array.hpp"
3+
4+
/*
5+
* Find the k largest (or smallest) elements in an array
6+
* Time Complexity: O(n*k)
7+
* Space Complexity: O(1)
8+
*/
9+
void find_k_smallest_elements_using_modified_bubble_sort(int array[], int size, int k)
10+
{
11+
PRINT_ARRAY(array, size);
12+
for (int i=0; i<k; i++) {
13+
for (int j=0; j<size-1-i; j++) {
14+
if (array[j] < array[j+1]) {
15+
std::swap(array[j], array[j+1]);
16+
}
17+
}
18+
}
19+
20+
for (int i=1; i<=k; i++) {
21+
std::cout <<array[size-i] <<" ";
22+
}
23+
std::cout <<std::endl;
24+
}
25+
26+
/*
27+
* Find the k largest (or smallest) elements in an array
28+
* Time Complexity: O(n(logn))
29+
* Space Complexity: O(1)
30+
*/
31+
void find_k_smallest_elements_using_sorting(int array[], int size, int k)
32+
{
33+
PRINT_ARRAY(array, size);
34+
std::sort(array, array+size); //n*log2(n)
35+
PRINT_ARRAY(array, k);
36+
}
37+
38+
//TODO
39+
/*
40+
* Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n.
41+
*
42+
* Perform the linear search and compare the value of the array with index if both will not match then value of the index will be our smallest missing number
43+
* Time Complexity: O(n))
44+
* Space Complexity: O(1)
45+
*
46+
* Perform the binary search and compare the value of the array with index if both will not match then value of the index will be our smallest missing number
47+
* Time Complexity: O(logn))
48+
* Space Complexity: O(1)
49+
*/
50+
int find_smallest_missing_number(int array[], int size)
51+
{
52+
int l = 0;
53+
int r = size - 1;
54+
while(l < r) {
55+
int m = (l + r)/2;
56+
std::cout <<"m: " <<m <<std::endl;
57+
if (array[m] != m) {
58+
std::cout <<"Smallest missing number: " <<m <<std::endl;
59+
return(m);
60+
} else if (array[m] > m) {
61+
l = m + 1;
62+
} else {
63+
r = m - 1;
64+
}
65+
}
66+
std::cout <<"Smallest missing number is not found" <<std::endl;
67+
return(INT_MIN);
68+
}
69+
70+
/*
71+
* We can use three loops to find all triplets equal to zero
72+
* Time Complexity: O(n3))
73+
* Space Complexity: O(1)
74+
*
75+
* We can optimize the solution by sorting the array and using two loops
76+
* Time Complexity: O(n2))
77+
* Space Complexity: O(1)
78+
*/
79+
void find_all_triplets_with_sum_zero(int array[], int size)
80+
{
81+
PRINT_ARRAY(array, size);
82+
std::sort(array, array+size); //n*log2(n)
83+
84+
for (int i=0; i<size-2; i++) {
85+
int x = array[i];
86+
int l = i+1;
87+
int r = size-1;
88+
while(l < r) {
89+
if ((x + array[l] + array[r]) == 0) {
90+
std::cout <<"(" <<x <<" + " <<array[l] <<" + " <<array[r] <<")" <<std::endl;
91+
l++;
92+
r--;
93+
} else if ((x + array[l] + array[r]) < 0) {
94+
l++;
95+
} else {
96+
r--;
97+
}
98+
}
99+
}
100+
}

Array/Main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include"Array.hpp"
2+
3+
int main()
4+
{
5+
int array[] = {1, 7, 2, -1, 2, 3, 0, 6, 5, 4};
6+
find_k_smallest_elements_using_modified_bubble_sort(array, sizeof(array)/sizeof(int), 3);
7+
8+
int array2[] = {1, 7, 2, -1, 2, 3, 0, 6, 5, 4};
9+
find_k_smallest_elements_using_sorting(array2, sizeof(array2)/sizeof(int), 3);
10+
11+
int array3[] = {0, -1, 2, -3, 1, -4, 5};
12+
find_all_triplets_with_sum_zero(array3, sizeof(array3)/sizeof(int));
13+
14+
int array4[] = {0, 1, 2, 6, 9};
15+
find_smallest_missing_number(array4, sizeof(array4)/sizeof(int));
16+
17+
return(0);
18+
}

Array/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# define compiler
2+
COMPILER=g++
3+
4+
# define compiler flags
5+
FLAGS=-std=c++11 -g -Wall
6+
7+
# define executable target
8+
OBJECTS=Main.o\
9+
Find.o
10+
11+
BINARY=Array
12+
13+
all: clean $(BINARY)
14+
15+
Array: $(OBJECTS)
16+
$(COMPILER) $(FLAGS) $(OBJECTS) -o $(BINARY)
17+
18+
Main.o:
19+
$(COMPILER) -c $(FLAGS) Main.cpp
20+
21+
Find.o:
22+
$(COMPILER) -c $(FLAGS) Find.cpp
23+
24+
clean:
25+
rm -f $(BINARY) *.o
26+
27+
.PHONY: clean

BST/BST.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stack>
2+
#include "BST.hpp"
3+
4+
Node* AddNode(Node* root, int data) {
5+
if (!root) {
6+
return (new Node(data));
7+
}
8+
9+
if (root->data > data) {
10+
root->left = AddNode(root->left, data);
11+
}
12+
else {
13+
root->right = AddNode(root->right, data);
14+
}
15+
return root;
16+
}
17+

BST/BST.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef _BST_H_
2+
#define _BST_H_
3+
4+
#include <iostream>
5+
6+
struct Node {
7+
int data;
8+
Node* left;
9+
Node* right;
10+
11+
Node(int data) {
12+
this->data = data;
13+
this->left = this->right = NULL;
14+
}
15+
};
16+
17+
/*
18+
* APIs
19+
*/
20+
Node* AddNode(Node* root, int data);
21+
22+
#endif

BST/Main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "BST.hpp"
2+
3+
int main()
4+
{
5+
Node* root = NULL;
6+
root = AddNode(root, 50);
7+
AddNode(root, 30);
8+
AddNode(root, 20);
9+
AddNode(root, 40);
10+
AddNode(root, 70);
11+
AddNode(root, 60);
12+
AddNode(root, 80);
13+
14+
return(0);
15+
}

BST/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# define compiler
2+
COMPILER=g++
3+
4+
# define compiler flags
5+
FLAGS=-std=c++11 -g -Wall
6+
7+
# define executable target
8+
OBJECTS=Main.o\
9+
BST.o
10+
11+
BINARY=BST
12+
13+
all: clean $(BINARY)
14+
15+
BST: $(OBJECTS)
16+
$(COMPILER) $(FLAGS) $(OBJECTS) -o $(BINARY)
17+
18+
Main.o:
19+
$(COMPILER) -c $(FLAGS) Main.cpp
20+
21+
BST.o:
22+
$(COMPILER) -c $(FLAGS) BST.cpp
23+
24+
clean:
25+
rm -f $(BINARY) *.o
26+
27+
.PHONY: clean

0 commit comments

Comments
 (0)