Skip to content

Commit

Permalink
add khushal87 codes
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed May 2, 2021
1 parent 6b0427a commit e5b9540
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 7 deletions.
File renamed without changes.
32 changes: 32 additions & 0 deletions BST/find_pair_sum_in_bst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left;
Node *right;
};

bool findPairUtil(Node *root, int target, set<int> &st)
{
if (root == nullptr)
return false;
if (findPairUtil(root->left, target, st))
{
return true;
}
if (st.find(target - root->data) != st.end())
{
cout << root->data << " " << target - root->data << endl;
return true;
}
else
{
st.insert(root->data);
}
if (findPairUtil(root->right, target, st))
{
return true;
}
}
35 changes: 35 additions & 0 deletions BST/floor_ceil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left;
Node *right;
};

pair<int, int> floorCeil(Node *root, int key)
{
if (root == nullptr)
return {};
else
{
pair<int, int> p;
if (root->data == key)
{
p.first = root->data;
p.second = root->data;
}
else if (root->data < key)
{
p.first = root->data;
root = root->right;
}
else
{
p.second = root->data;
root = root->left;
}
return p;
}
}
51 changes: 51 additions & 0 deletions BST/inorder_predecessor_and_successor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *left;
Node *right;
};

pair<Node *, Node *> preSuc(Node *root, int data)
{
if (root == nullptr)
return {};
else
{
pair<Node *, Node *> p;
if (root->data == data)
{
if (root->left)
{
Node *temp = root->left;
while (temp->right)
{
temp = temp->right;
}
p.first = temp;
}
if (root->right)
{
Node *temp = root->right;
while (temp->left)
{
temp = temp->left;
}
p.second = temp;
}
}
else if (root->data > data)
{
p.second = root;
preSuc(root->left, data);
}
else
{
p.first = root;
preSuc(root->right, data);
}
return p;
}
}
23 changes: 23 additions & 0 deletions BST/number_of_nodes_in_a_range.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

class Tree
{
public:
int val;
Tree *left;
Tree *right;
};

int answer(Tree *root, int lo, int hi)
{
if (root == nullptr)
return 0;
else
{
if (root->val >= lo && root->val <= hi)
return 1 + answer(root->left, lo, hi) + answer(root->right, lo, hi);
else if (root->val < lo)
return answer(root->right, lo, hi);
else
return answer(root->left, lo, hi);
}
}
11 changes: 4 additions & 7 deletions Graph/undirected_graph_has_cycle_or_not.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ bool dfs(vector<vector<int>> &adj, vector<bool> &vis, int i, int parent)
{
return true;
}
else
{
if (j != parent)
{
return true;
}
}
}
else if (j != parent)
{
return true;
}
}
return false;
Expand Down
1 change: 1 addition & 0 deletions Heap/heap_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ int main()
{
cin >> arr[i];
}
heapSort(arr, n);
reverse(arr, arr + n);
for (int i = 0; i < n; i++)
{
Expand Down
36 changes: 36 additions & 0 deletions LinkedList/remove_duplicate_In_unsorted_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node *next;
Node(int x)
{
data = x;
next = NULL;
}
};

Node *removeDuplicates(Node *head)
{
// your code goes here
unordered_set<int> st;
Node *curr = head;
Node *prev = nullptr;
while (curr != nullptr)
{
if (st.find(curr->data) != st.end())
{
prev->next = curr->next;
delete curr;
}
else
{
st.insert(curr->data);
prev = curr;
}
curr = prev->next;
}
return head;
}
37 changes: 37 additions & 0 deletions LinkedList/remove_duplicate_in_sorted_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node *next;
Node(int x)
{
data = x;
next = NULL;
}
};

//Function to remove duplicates from sorted linked list.
Node *removeDuplicates(Node *head)
{
// your code goes here
if (head == nullptr)
return nullptr;
Node *curr = head;
Node *next;
while (curr->next != nullptr)
{
if (curr->data == curr->next->data)
{
next = curr->next->next;
free(curr->next);
curr->next = next;
}
else
{
curr = curr->next;
}
}
return head;
}

0 comments on commit e5b9540

Please sign in to comment.