diff --git a/codes/binary_tree.cpp b/codes/binary_tree.cpp new file mode 100644 index 0000000..cdb4108 --- /dev/null +++ b/codes/binary_tree.cpp @@ -0,0 +1,206 @@ +#include +using namespace std; + +struct node{ + int key; + node *left; + node* right; + node* parent; +}; + +struct node_avl +{ + int key; + int balance; + node_avl *left; + node_avl *right; + node_avl *parent; +}; + +node * insert_tree(node *n,int a) +{ + if(n==NULL) + { + node *temp; + temp = new node; + temp->key = a; + temp->left = NULL; + temp->right = NULL; + temp->parent = NULL; + return temp; + } + else if(a < n->key) + n->left = insert_tree(n->left,a); + else + n->right = insert_tree(n->right,a); + + return n; +} + +node * delete_tree(node *root,int a) +{ + if(root->key < a) + { + root->right = delete_tree(root->right,a); + } + else if(root->key > a) + { + root->left = delete_tree(root->left,a); + } + else + { + if(root->left == NULL) + { + node *temp; + temp = root->right; + free(root); + return temp; + } + else if(root->right == NULL) + { + node *temp; + temp = root->left; + free(root); + return temp; + } + else + { + node *temp; + temp = root; + temp = temp->right; + + while(temp->left!=NULL) + { + temp = temp->left; + } + + root->key = temp->key; + + root->right = delete_tree(root->right,temp->key); + } + } + + return root; +} + +node_avl *insert_avl(node *root,int a) +{ + if(root==NULL) + { + node_avl *temp; + temp = new node_avl; + temp->key = a; + temp->balance = 0; + temp->right = NULL; + temp->left = NULL; + temp->parent = root; + return temp; + } + if(a < root->key) + { + if(root->left == NULL) + { + node_avl *temp; + temp = new node_avl; + temp->key = a; + temp->balance = 0; + temp->right = NULL; + temp->left = NULL; + temp->parent = root; + + while(temp1!=NULL) + { + if(a < temp1->key) + { + if(temp1->balance == 1) + { + temp1->balance = 0; + break; + } + else if(temp1->balance == 0) + { + temp1->balance = -1; + } + else + { + // rotate right + } + } + else + { + if(temp1->balance == -1) + { + temp1->balance = 0; + break; + } + else if(temp1->balance == 0) + { + temp1->balance = 1; + } + else + { + // rotate left + } + } + + } + else + { + root->left = insert_avl(root->left,a); + } + } + else + { + if(root->right == NULL) + { + node_avl *temp; + temp = new node_avl; + temp->key = a; + temp->balance = 0; + temp->right = NULL; + temp->left = NULL; + temp->parent = root; + } + else + { + root->right - insert_avl(root->right,a); + } + } +} + +void inorder(node *root) +{ + if(root == NULL) + return; + + inorder(root->left); + cout<key<right); +} + +int main() +{ + int n; + cout<<"No. of nodes : "; + cin>>n; + + cout<<"enter key values "<>new_node; + + root = insert_tree(root,new_node); + } + + inorder(root); + + root = delete_tree(root,2); + + inorder(root); + +} \ No newline at end of file diff --git a/codes/kmp_algo.cpp b/codes/kmp_algo.cpp new file mode 100644 index 0000000..83dab5e --- /dev/null +++ b/codes/kmp_algo.cpp @@ -0,0 +1,77 @@ +#include +using namespace std; + +void compute_lps(int *lps,string pat) +{ + int prev =0; + int i=1; + lps[0] = 0; + + while(i +using namespace std; + +struct node_ +{ + int len; + node_ *next; + node_ *head; + int key; + node_ *tail; +}; + +struct edge +{ + int first; + int second; + int w; +}; + +void merge(edge *E,int from, int to) +{ + int n; + n = to-from+1; + edge A[n]; + for(int i=0;ilen > B->len) + { + (A->tail)->next = B; + (A->tail) = B->tail; + A->len += B->len; + + temp = B; + while(temp!=NULL) + { + temp->head = A; + temp = temp->next; + } + } + else + { + (B->tail)->next = A; + (A->tail) = A->tail; + B->len += A->len; + + temp = A; + while(temp!=NULL) + { + temp->head = B; + temp = temp->next; + } + } +} + +int main() +{ + int n,e; + cout<<"No. of Nodes and no. of edges"<>n>>e; + + cout<<"Describe edges : from to weight"<>E[i].first>>E[i].second>>E[i].w; + } + + merge_sort(E,0,e-1); + + // + + node_ N[n]; + + for (int i = 0; i < n; ++i) + { + N[i].next = NULL; + N[i].head = &N[i]; + N[i].key = i; + N[i].len = 1; + N[i].tail = &N[i]; + } + + for(int i=0;ikey; + b = (N[E[i].second].head)->key; + + if(a!=b) + { + cout< +using namespace std; + +struct edge +{ + int first; + int second; + int w; +}; + +struct node_ +{ + node_ *parent; + int key; + int height; +}; + +void merge(edge *E,int from, int to) +{ + int n; + n = to-from+1; + edge A[n]; + for(int i=0;ikey != E.key) + { + E = *(E.parent); + } + + return E.key; +} + +void union_set(node_ *A, node_ *B) +{ + if(A->height > B->height) + { + B->parent = A; + } + else if(A->height < B->height) + { + A->parent = B; + } + else + { + A->parent = B; + (B->height)++; + } +} + +int main() +{ + int n,e; + cout<<"No. of Nodes and no. of edges"<>n>>e; + + cout<<"Describe edges : from to weight"<>E[i].first>>E[i].second>>E[i].w; + } + + merge_sort(E,0,e-1); + + // + + node_ N[n]; + + for(int i=0;i + +using namespace std; + +int main() +{ + int n,e; + cout<<"No. of Nodes and no. of edges"<>n>>e; + + cout<<"Describe edges : from to weight"<>a>>b>>w; + adjmat[a][b] = 1; + adjmat[b][a] = 1; + weight[a][b] = w; + weight[b][a] = w; + } + + bool inP[n]; + + int shortest_d_to_P[n]; + int parent[n]; + + for(int i=0;i