diff --git a/bst.cpp b/bst.cpp new file mode 100644 index 0000000..494b72c --- /dev/null +++ b/bst.cpp @@ -0,0 +1,119 @@ +#include +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); +} \ No newline at end of file diff --git a/topological_sort.cpp b/topological_sort.cpp new file mode 100755 index 0000000..6ea2a40 --- /dev/null +++ b/topological_sort.cpp @@ -0,0 +1,85 @@ +#include +#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 +#define mii map +#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 +#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 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; +} \ No newline at end of file diff --git a/trie.cpp b/trie.cpp new file mode 100644 index 0000000..d91cb8a --- /dev/null +++ b/trie.cpp @@ -0,0 +1,75 @@ +#include +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); +} \ No newline at end of file