Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions bst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <bits/stdc++.h>
using namespace std;

class BST
{
public:
BST()
{
root = NULL;
}

struct node
{
int data;
node *l;
node *r;
node(int a)
{
data = a;
l = r = NULL;
}
};

node *root;

void insert(int a)
{
node *temp = new node(a);
node *prev = root;
if (root == NULL)
root = temp;
else
{
while (1)
{
if (prev->data > a)
{
if (prev->l == NULL)
{
prev->l = temp;
break;
}
else
prev = prev->l;
}
else
{
if (prev->r == NULL)
{
prev->r = temp;
break;
}
else
prev = prev->r;
}
}
}
}

void del(int a)
{
node *temp = root;
node **prev = &root;
while (temp->data != a)
{
if (temp->data < a)
prev = &(temp->r), temp = temp->r;
else
prev = &(temp->l), temp = temp->l;
}
node *rep = NULL;

if (temp->r == NULL)
{
if (temp->l == NULL)
*prev = NULL;
else
{
rep = temp->l;
while (rep->r != NULL)
rep = rep->r;
int rd = rep->data;
del(rep->data);
(*prev)->data = rd;
}
}
else
{
rep = temp->r;
while (rep->l != NULL)
rep = rep->l;
int rd = rep->data;
del(rep->data);
(*prev)->data = rd;
}
}

void inorder(node *root)
{
if (root == NULL)
return;
inorder(root->l);
cout << root->data << " ";
inorder(root->r);
}
};


int main(){
BST bst;
bst.insert(29);
bst.insert(30);
bst.insert(1);
bst.insert(-15);
bst.insert(99);
bst.insert(1025);
bst.inorder(bst.root);
bst.del(1);
}
85 changes: 85 additions & 0 deletions topological_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define endl '\n'
#define int long long int
using namespace std;
#define pii pair <int, int>
#define mii map<int, int>
#define pb push_back
#define deb(x) cout << #x << " " << x << endl;
#define deb2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define pt(a) cout << a << endl;
#define ptt(a, b) cout << a << " " << b << endl;
#define ptf(a) cout << a << " ";
#define rd(a) cin >> a;
#define rdd(a, b) cin >> a >> b;
#define loop(s, e, itr) for (int itr = s; itr < e; itr++)
#define lop(n) for(int i = 0; i < n; i++)
#define lopj(n) for(int j = 0; j < n; j++)
#define lopk(n) for(int k = 0; k < n; k++)
#define vin vector<int>
#define w(flag) int tc = 1; if(flag) cin >> tc; for(int t = 1; t <= tc; t++)
#define vec vector
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define elif else if
#define nline cout << endl;


int ar[1000006] = {0};
int n = 0, m;
int tp(0), tp1(0), tp2(0), tp3(0), tp4(0);
string str;
int len;
void pre_processing(){}

vin adj[100005];
int vis[100005];

stack<int> stk;

void dfs(int node)
{
vis[node] = true;
for(auto i : adj[node])
if(!vis[i])
dfs(i);

stk.push(node);
}

void solve()
{
cin >> n >> m;
lop(m)
{
rdd(tp, tp1)
adj[tp].pb(tp1);
}
memset(vis, 0, sizeof(vis));
loop(1, n+1, i)
if(!vis[i])
dfs(i);

vin sorted_nodes;
while(!stk.empty())
{
sorted_nodes.push_back(stk.top());
stk.pop();
}
for(auto i : sorted_nodes)
cout << i << " ";
cout << endl;
}

int32_t main()
{
fastio;
pre_processing();
w(0)
{
// cout << "Case #" << t << ": ";
solve();
}
return 0;
}
75 changes: 75 additions & 0 deletions trie.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include<bits/stdc++.h>
using namespace std;

class Trie
{
public:
struct node
{
node *child[26];
int count;
node()
{
for (int i = 0; i < 26; i++)
child[i] = NULL;
count = 0;
}
};

node *head;
Trie()
{
node *temp = new node();
head = temp;
}

void insert(string str)
{
int len = str.length();
node *temp = head;
for (int i = 0; i < len; i++)
{
int p = str[i] - 'a';
if (temp->child[p] == NULL)
{
node *n = new node();
temp->child[p] = n;
}
temp = temp->child[p];
}
temp->count++;
}

int count(node *hd)
{
int ans = 0;
if (hd->count != 0)
ans++; // to count distinct strings
//ans += hd->count; // uncomment this line to count total strings (and comment just above line)
for (int i = 0; i < 26; i++)
{
if (hd->child[i] != NULL)
{
ans += count(hd->child[i]);
}
}
return ans;
}
};

int main(){
Trie trie;
trie.insert("arpit");
trie.insert("arpitgupta");
trie.insert("arpit");
trie.insert("arpitgupta");
trie.insert("arpitcodingtrie");
trie.insert("arpitgpta");
trie.insert("arpitgpta");
trie.insert("arpitgupta");
trie.insert("arpitgpta");
trie.insert("arpitcodingtrie");
trie.insert("arpitgpta");
trie.insert("arpitcodingtrie");
trie.count(trie.head);
}