Skip to content

Commit

Permalink
add new codes
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Mar 10, 2021
1 parent 4d4ef2f commit f9a6b96
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 40 deletions.
23 changes: 23 additions & 0 deletions BST/check_if_BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;

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

bool isBSTUtil(Node *root, int min, int max)
{
if (root == nullptr)
return true;
if (root->data < min || root->data > max)
return 0;
return isBSTUtil(root->left, min, root->data - 1) && isBSTUtil(root->right, root->data + 1, max);
}

bool isBST(Node *root)
{
return isBSTUtil(root, INT_MIN, INT_MAX);
}
40 changes: 40 additions & 0 deletions BST/kth_largest_element_in_bst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;

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

void inorder(Node *root, int k, int &c, int &ans)
{
if (root == nullptr)
return;
else
{
inorder(root->right, k, c, ans);
c++;
if (c == k)
{
ans = root->data;
return;
}
inorder(root->left, k, c, ans);
}
}

int kthLargest(Node *root, int k)
{
//Your code here
if (root == nullptr)
return 0;
else
{
int ans = 0;
int n = 0;
inorder(root, k, n, ans);
return ans;
}
}
31 changes: 21 additions & 10 deletions BST/kth_smallest_element_in_bst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,32 @@ struct Node
Node *right;
};

int solve(Node *root, int &k)
void inorder(Node *root, int k, int &c, int &ans)
{
if (root == nullptr)
return 0;
return;
else
{
int left = solve(root->left, k);
if (left)
return left;
k -= 1;
if (k == 0)
inorder(root->left, k, c, ans);
c++;
if (c == k)
{
return root->data;
ans = root->data;
return;
}
int right = solve(root->right, k);
return right;
inorder(root->right, k, c, ans);
}
}

int solve(Node *root, int k)
{
if (root == nullptr)
return 0;
else
{
int ans = 0;
int n = 0;
inorder(root, k + 1, n, ans);
return ans;
}
}
72 changes: 72 additions & 0 deletions Heap/mergeKSortedLists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <bits/stdc++.h>
using namespace std;

struct ListNode
{
int data;
ListNode *next;
};

struct Node
{
ListNode *curr;
int idx;
Node(ListNode *c, int data)
{
curr = c;
idx = data;
}
};

struct compare
{
bool operator()(Node *a, Node *b)
{
return a->curr->data > b->curr->data;
}
};

ListNode *mergeKSortedList(vector<vector<ListNode *>> &lists)
{
int k = lists.size();
if (k == 0)
return nullptr;
else
{
ListNode *head, *tail;
head = tail = NULL;
priority_queue<Node, vector<Node>, compare> pq;
vector<ListNode *> ptr(k);
for (int i = 0; i < k; i++)
{
ptr[i] = lists[i][0];
if (ptr[i] != nullptr)
{
pq.push(Node(ptr[i], i));
}
}
if (pq.empty())
return nullptr;
head = tail = pq.top().curr;
int idx = pq.top().idx;
pq.pop();
ptr[idx] = ptr[idx]->next;
if (ptr[idx] != nullptr)
{
pq.push(Node(ptr[idx], idx));
}
while (!pq.empty())
{
ListNode *temp = pq.top().curr;
idx = pq.top().idx;
pq.pop();
tail->next = temp;
tail = tail->next;
ptr[idx] = ptr[idx]->next;
if (ptr[idx] != nullptr)
{
pq.push(Node(ptr[idx], idx));
}
}
}
}
60 changes: 60 additions & 0 deletions LinkedList/implement_stack_using_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *next;
Node(int val)
{
data = val;
next = nullptr;
}
};

Node *top = NULL;

void push(int data)
{
Node *temp = new Node(data);
if (!temp)
{
cout << "\nHeap Overflow" << endl;
return;
}
temp->next = top;
top = temp;
}

bool isEmpty()
{
return top == nullptr;
}

int peek()
{
if (!isEmpty())
{
return top->data;
}
else
{
return -1;
}
}

void pop()
{
Node *temp;
if (top == nullptr)
{
cout << "\nStack Underflow" << endl;
return;
}
else
{
temp = top;
top = top->next;
delete temp;
}
}
30 changes: 0 additions & 30 deletions LinkedList/left_view_of_binary_tree.cpp

This file was deleted.

39 changes: 39 additions & 0 deletions LinkedList/queue_using_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *next;
Node(int val)
{
data = val;
next = nullptr;
}
};

Node *front = nullptr;
Node *rear = nullptr;

void enQueue(int data)
{
Node *temp = new Node(data);
if (rear == nullptr)
{
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}

void deQueue()
{
if (front == nullptr)
return;
Node *temp = front;
front = front->next;
if (front == nullptr)
rear = nullptr;
delete temp;
}
34 changes: 34 additions & 0 deletions LinkedList/sort_a_linked_list_0_1_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
Node *next;
};

void sortList(Node *root)
{
vector<int> cnt(3, 0);
Node *curr = root;
while (curr != nullptr)
{
cnt[curr->data]++;
curr = curr->next;
}
int i = 0;
curr = root;
while (curr != nullptr)
{
if (cnt[i] == 0)
{
i++;
}
else
{
curr->data = i;
cnt[i]--;
curr = curr->next;
}
}
}
Loading

0 comments on commit f9a6b96

Please sign in to comment.