├── C++ Header files and Operators.cpp ├── Graph ├── BFSOfTree.cpp ├── BfsLevelOrderTraversalOnBinaryTree.cpp ├── Graph - BFS DFS.cpp ├── Graph Krushkals.cpp ├── Graph Prims.cpp └── Graph representation using set and hash ├── Hashing ├── Heaps ├── Heap - Heapify.cpp └── Heap- MIN and MAX.cpp ├── Linked List ├── CircularLinkedList.cpp ├── Detect-Loop-In-Linked-List.py ├── DoublyLinkedList.cpp └── SinglyLinkedList.cpp ├── README.md ├── Segment Tree ├── Segment_tree.cpp ├── segmenttree(max).cpp └── segmenttree.cpp ├── Stack ├── Stack.cpp └── stack-using-queue.cpp ├── Trees ├── AVL-Tree.cpp └── BinarySearchTree.cpp └── Trie └── trie.cpp /C++ Header files and Operators.cpp: -------------------------------------------------------------------------------- 1 | // There are two types of header files: 2 | // 1. System header files: It comes with the compiler 3 | #include 4 | // 2. User defined header files: It is written by the programmer 5 | // #include "this.h" //--> This will produce an error if this.h is no present in the current directory 6 | 7 | using namespace std; 8 | 9 | int main(){ 10 | int a=4, b=5; 11 | cout<<"Operators in C++:"< used to assign values to variables 26 | // int a =3, b=9; 27 | // char d='d'; 28 | 29 | // Comparison operators 30 | cout<<"Following are the comparison operators in C++"<= b is "<<(a>=b)< b is "<<(a>b)< 2 | #include 3 | using namespace std; 4 | class node{ 5 | public: 6 | int val; 7 | node * left; 8 | node * right; 9 | }; 10 | node * addNode(int v){ 11 | node * n=new node(); 12 | n->val=v; 13 | n->left=NULL; 14 | n->right=NULL; 15 | } 16 | 17 | int bfs(node * tree){ 18 | int s; 19 | if(tree==NULL) return 0; 20 | queue q; 21 | q.push(tree); 22 | while(q.empty()==false){ 23 | s=q.size(); 24 | for(int i=0;ival<<" "; 28 | if(t->left) q.push(t->left); 29 | if(t->right) q.push(t->right); 30 | } 31 | } 32 | return 0; 33 | } 34 | int main(){ 35 | node *tree; 36 | tree=addNode(1); 37 | tree->left=addNode(2); 38 | tree->right=addNode(3); 39 | tree->left->left=addNode(5); 40 | tree->left->right=addNode(4); 41 | tree->right->left=addNode(6); 42 | bfs(tree); 43 | } 44 | -------------------------------------------------------------------------------- /Graph/BfsLevelOrderTraversalOnBinaryTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | typedef struct node{ 7 | int data; 8 | struct node * left; 9 | struct node * right; 10 | }node; 11 | 12 | node * addNode(int val){ 13 | node *t=(node *) calloc(sizeof(node),1); 14 | t->data=val; 15 | t->left=NULL; 16 | t->right=NULL; 17 | return t; 18 | } 19 | void createTree(node **G,int n){ 20 | while(n){ 21 | int m,p,val; 22 | cout<<"Enter node value :"; 23 | cin>>val; 24 | G[val]=addNode(val); 25 | n--; 26 | cout<<"Enter left node and right node values( -1 for null ) :"; 27 | cin>>m>>p; 28 | if(m==-1) n--; 29 | if(p==-1) n--; 30 | G[val]->left=addNode(m); 31 | G[val]->right=addNode(p); 32 | 33 | } 34 | cout<<"TREE\n"; 35 | for(int i=0;idata<<" : "; 37 | cout<left->data <<" "<right->data< > bfs(node **G,int n){ 43 | vector > res; 44 | queue q; 45 | q.push(G[0]); 46 | cout<<"Bfs\n"; 47 | while(!q.empty()){ 48 | int s=q.size(); 49 | vector v; 50 | for(int i=0;idata<data); 55 | if(t->left) q.push(G[i]->left); 56 | if(t->right) q.push(G[i]->right); 57 | } 58 | res.push_back(v); 59 | } 60 | return res; 61 | 62 | } 63 | 64 | int main(){ 65 | vector > vec; 66 | int i,j,n; 67 | cout<<"Enter number of nodes present :"; 68 | cin>>n; 69 | node **G=(node **)calloc(sizeof(node *),n); 70 | createTree(G,n); 71 | vec=bfs(G,n); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Graph/Graph - BFS DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX 100 4 | 5 | int A[MAX][MAX], visitedB[MAX] = {0},visitedD[MAX] = {0}; 6 | 7 | void BFS(int root,int limit) 8 | { 9 | int u; 10 | cout< q1; 13 | q1.push(root); 14 | while(!q1.empty()) 15 | { 16 | u = q1.front(); 17 | q1.pop(); 18 | for(int i=1;i>vertex_count; 56 | 57 | for(int i=1;i>A[i][j]; 62 | } 63 | 64 | cout<<"\nBFS: "; 65 | BFS(1,vertex_count+1); 66 | 67 | cout<<"\nDFS: "; 68 | DFS(1,vertex_count+1); 69 | } 70 | -------------------------------------------------------------------------------- /Graph/Graph Krushkals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX 100 4 | #define I INT_MAX 5 | 6 | int edge[3][MAX]; 7 | int s[MAX]; 8 | int included[MAX]; 9 | int t[2][MAX]; 10 | 11 | void union1(int u, int v) 12 | { 13 | if(s[u] < s[v]) 14 | { 15 | s[u] = s[u] + s[v]; 16 | s[v] = u; 17 | } 18 | else 19 | { 20 | s[v] = s[u] + s[v]; 21 | s[u] = v; 22 | } 23 | } 24 | 25 | int find(int u) 26 | { 27 | int x = u; 28 | while(s[x] > 0) 29 | { 30 | x = s[x]; 31 | } 32 | return x; 33 | } 34 | 35 | 36 | int main() 37 | { 38 | memset(s,-1,sizeof(s)); 39 | memset(included,0,sizeof(included)); 40 | int vertex_count, edge_count, total_weight = 0; 41 | cout<<"Enter the number of vertex: "; 42 | cin>>vertex_count; 43 | cout<<"Enter the number of edges: "; 44 | cin>>edge_count; 45 | for(int i= 0;i < edge_count;i++) 46 | { 47 | cout<<"Enter vertex 1: "; 48 | cin>>edge[0][i]; 49 | cout<<"Enter vertex 2: "; 50 | cin>>edge[1][i]; 51 | cout<<"Enter edge weight: "; 52 | cin>>edge[2][i]; 53 | } 54 | 55 | int i=0,j,k,min,u,v; 56 | 57 | while(i < vertex_count-1) 58 | { 59 | min = I; 60 | for(j = 0; j < edge_count ; j++) 61 | { 62 | if((included[j]==0) and (edge[2][j] < min)) 63 | { 64 | min = edge[2][j]; 65 | k = j; 66 | u = edge[0][j]; 67 | v = edge[1][j]; 68 | } 69 | } 70 | 71 | if(find(u) != find(v)) 72 | { 73 | t[0][i] = u; 74 | t[1][i] = v; 75 | total_weight += edge[2][k]; 76 | union1(find(u), find(v)); 77 | i++; 78 | } 79 | included[k] = 1; 80 | } 81 | 82 | 83 | for(int i = 0; i < vertex_count-1; i++) 84 | cout< 2 | using namespace std; 3 | #define I INT_MAX 4 | #define MAX 100 5 | 6 | int near[MAX] = {I}; 7 | int t[2][MAX]; 8 | int Cost[MAX][MAX]; 9 | 10 | int main() 11 | { 12 | 13 | 14 | int vertex_count,data,total_weight = 0; 15 | cout<<"Enter the number of vertex:- "; 16 | cin>>vertex_count; 17 | 18 | 19 | memset(near, I , sizeof(near)); 20 | /**********INPUT VALUES***********/ 21 | 22 | 23 | for(int i=1; i <= vertex_count;i++) 24 | for(int j=1; j <= vertex_count;j++) 25 | { 26 | cout<<"If there is edge between "<>data; 28 | if(data!=0) 29 | Cost[i][j] = data; 30 | else 31 | Cost[i][j] = I; 32 | 33 | } 34 | 35 | /**********INITIAL STEP***********/ 36 | 37 | 38 | int minimum = I,u,v; 39 | for(int i=1;i 4 | using namespace std; 5 | 6 | struct Graph { 7 | int V; 8 | set >* adjList; 9 | }; 10 | 11 | // A utility function that creates a graph of V vertices 12 | Graph* createGraph(int V) 13 | { 14 | Graph* graph = new Graph; 15 | graph->V = V; 16 | 17 | // Create an array of sets representing 18 | // adjacency lists. Size of the array will be V 19 | graph->adjList = new set >[V]; 20 | 21 | return graph; 22 | } 23 | 24 | // Adds an edge to an undirected graph 25 | void addEdge(Graph* graph, int src, int dest) 26 | { 27 | // Add an edge from src to dest. A new 28 | // element is inserted to the adjacent 29 | // list of src. 30 | graph->adjList[src].insert(dest); 31 | 32 | // Since graph is undirected, add an edge 33 | // from dest to src also 34 | graph->adjList[dest].insert(src); 35 | } 36 | 37 | // A utility function to print the adjacency 38 | // list representation of graph 39 | void printGraph(Graph* graph) 40 | { 41 | for (int i = 0; i < graph->V; ++i) { 42 | set > lst = graph->adjList[i]; 43 | cout << endl << "Adjacency list of vertex " 44 | << i << endl; 45 | 46 | for (auto itr = lst.begin(); itr != lst.end(); ++itr) 47 | cout << *itr << " "; 48 | cout << endl; 49 | } 50 | } 51 | 52 | // Searches for a given edge in the graph 53 | void searchEdge(Graph* graph, int src, int dest) 54 | { 55 | auto itr = graph->adjList[src].find(dest); 56 | if (itr == graph->adjList[src].end()) 57 | cout << endl << "Edge from " << src 58 | << " to " << dest << " not found." 59 | << endl; 60 | else 61 | cout << endl << "Edge from " << src 62 | << " to " << dest << " found." 63 | << endl; 64 | } 65 | 66 | // Driver code 67 | int main() 68 | { 69 | // Create the graph given in the above figure 70 | int V = 5; 71 | struct Graph* graph = createGraph(V); 72 | addEdge(graph, 0, 1); 73 | addEdge(graph, 0, 4); 74 | addEdge(graph, 1, 2); 75 | addEdge(graph, 1, 3); 76 | addEdge(graph, 1, 4); 77 | addEdge(graph, 2, 3); 78 | addEdge(graph, 3, 4); 79 | 80 | // Print the adjacency list representation of 81 | // the above graph 82 | printGraph(graph); 83 | 84 | // Search the given edge in the graph 85 | searchEdge(graph, 2, 1); 86 | searchEdge(graph, 0, 3); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Hashing: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef struct Dictionary 8 | { 9 | char word[20]; 10 | char meaning[20]; 11 | }dict; 12 | 13 | 14 | class Hashing 15 | { 16 | dict hashtable[26]; 17 | 18 | public: 19 | 20 | int hashfun(char w[20]) 21 | { 22 | int key=0; 23 | if(w[0]>='A' && w[0]<='Z') 24 | key=w[0]-65; 25 | else if(w[0]>='a' && w[0]<='z') 26 | key=w[0]-97; 27 | 28 | return key; 29 | } 30 | 31 | Hashing() 32 | { 33 | for(int i=0;i<=26;i++) 34 | { 35 | strcpy(hashtable[i].word,""); 36 | strcpy(hashtable[i].meaning,""); 37 | } 38 | } 39 | 40 | void readData() 41 | { 42 | dict temp; 43 | 44 | 45 | 46 | cout<<"\nEnter word : "; 47 | cin>>temp.word; 48 | cout<<"\nEnter meaning : "; 49 | cin>>temp.meaning; 50 | 51 | int key=hashfun(temp.word); 52 | assign(temp,key); 53 | cout<<"\nInserted successfully"; 54 | 55 | cout<<"\nkey is : "<>ch; 140 | switch(ch) 141 | { 142 | case 1: 143 | h.readData(); 144 | break; 145 | 146 | case 2: 147 | cout<<"Enter a word to be search : "; 148 | cin>>w; 149 | //h.search(w); 150 | if(!h.search(w)) 151 | cout<<"\nNo such word present"; 152 | break; 153 | 154 | case 3: 155 | h.display(); 156 | break; 157 | case 4: 158 | 159 | exit(0); 160 | break; 161 | } 162 | }while(ch!=4); 163 | return 0; 164 | } 165 | 166 | -------------------------------------------------------------------------------- /Heaps/Heap - Heapify.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX 100 4 | 5 | int heap[MAX]; 6 | 7 | void heapify(int len, int start_index) 8 | { 9 | int i = start_index; 10 | int lc = 2*start_index; 11 | int rc = 2*start_index+1; 12 | int temp; 13 | 14 | 15 | /******checking for left child******/ 16 | 17 | 18 | if (lc < len and heap[i] < heap[lc]) 19 | { 20 | i = lc; 21 | swap(heap[i],heap[start_index]); 22 | } 23 | 24 | /******checking for right child******/ 25 | 26 | if (rc < len and heap[i] < heap[rc]) 27 | { 28 | i = rc; 29 | swap(heap[i],heap[start_index]); 30 | } 31 | 32 | 33 | /******recursively calling heapify for the parent subtree******/ 34 | 35 | if(i != start_index) 36 | heapify(len,i); 37 | } 38 | 39 | /******Display of MAX heap******/ 40 | 41 | void display(int len) 42 | { 43 | cout<<"\n========================================\n"; 44 | cout<<"HEAP: "; 45 | for(int i=1; i <= len; i++) 46 | cout<>len; 56 | heap[ind] = 0; 57 | 58 | /******Inserting values******/ 59 | 60 | for(int ind=1;ind<=len;ind++) 61 | { 62 | cout<<"Enter "<>heap[ind]; 64 | } 65 | 66 | /******getting of index of first non leaf root******/ 67 | int start_index = (len/2); 68 | 69 | /******heapify all elements******/ 70 | for(int i= start_index; i>=1; i--) 71 | { 72 | heapify(len,i); 73 | } 74 | /******display heap******/ 75 | display(len); 76 | } 77 | -------------------------------------------------------------------------------- /Heaps/Heap- MIN and MAX.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAX 100 4 | 5 | int maxheap[MAX], minheap[MAX]; 6 | 7 | 8 | void maxinsert(int index) 9 | { 10 | int temp = maxheap[index]; 11 | while(( index > 1) and (temp > maxheap[index/2])) 12 | { 13 | maxheap[index] = maxheap[index/2]; 14 | index = index/2; 15 | } 16 | maxheap[index] = temp; 17 | } 18 | 19 | void mininsert(int index) 20 | { 21 | int temp = minheap[index]; 22 | while(( index > 1 ) and (temp < minheap[index/2])) 23 | { 24 | minheap[index] = minheap[index/2]; 25 | index = index/2; 26 | } 27 | minheap[index] = temp; 28 | } 29 | 30 | void display(int len) 31 | { 32 | cout<<"\n========================================\n"; 33 | cout<<"MAX HEAP: "; 34 | for(int i=1; i < len; i++) 35 | cout<>len; 48 | /****Initialise the array*****/ 49 | maxheap[0] = 0; 50 | minheap[0] = 0; 51 | for(int i=1;i <= len; i++) 52 | { 53 | cout<<"Enter "<>data; 55 | maxheap[i] = data; 56 | minheap[i] = data; 57 | } 58 | 59 | /*******Organising each element properly in heap******/ 60 | for(int i=2;i<=len;i++) 61 | { 62 | maxinsert(i); 63 | mininsert(i); 64 | } 65 | 66 | int newindex = len+1; 67 | int choice; 68 | 69 | 70 | /*******Printing the MAX Heap Generated**********/ 71 | 72 | display(newindex); 73 | 74 | while(1) 75 | { 76 | cout<<"\nPress 1 to insert a new number: "; 77 | cout<<"\nPress 2 to Display: "; 78 | cout<<"\nPress 3 to Exit: "; 79 | cout<<"\nEnter your choice: "; 80 | cin>>choice; 81 | 82 | switch(choice) 83 | { 84 | case 1: cout<<"Enter "<>data; 86 | maxheap[newindex] = data; 87 | minheap[newindex] = data; 88 | maxinsert(newindex); 89 | mininsert(newindex); 90 | newindex++; 91 | break; 92 | 93 | case 2: display(newindex); 94 | break; 95 | 96 | case 3: return 0; 97 | 98 | default: cout<<"Enter a valid choice !"< 2 | using namespace std; 3 | struct Node 4 | { 5 | 6 | int data; 7 | Node *next; 8 | }*head=NULL; 9 | 10 | void create(int arr[],int size) 11 | { 12 | head = new Node; 13 | Node *p = head; 14 | 15 | head->data = arr[0]; 16 | head->next=head; 17 | Node *temp; 18 | 19 | for(int i=1;idata = arr[i]; 24 | p->next = temp; 25 | temp->next=head; 26 | p=temp; // this is used to move p to next node 27 | 28 | } 29 | 30 | } 31 | void display() 32 | { 33 | Node *p = head; 34 | do{ 35 | cout<data<<" "; 36 | p=p->next; 37 | }while(p!=head); 38 | } 39 | 40 | void insert() 41 | { 42 | cout<<"On Which index you want to insert : "; 43 | int it; 44 | cin>>it; 45 | int d; 46 | int cnt=0; 47 | Node *p=head; 48 | if(it==0) //first node insertion 49 | { 50 | if(head==NULL) 51 | { 52 | cout<<"***FIRSTLY CREATE THE NODE***\n"; 53 | }else{ 54 | Node *temp=new Node; 55 | cout<<"Enter the data you want to insert : "; 56 | cin>>d; 57 | temp->data=d; 58 | while(p->next!=head) 59 | { 60 | p=p->next; 61 | } 62 | temp->next=p->next; 63 | p->next=temp; 64 | head=temp;} 65 | }else // insertion at any place 66 | { 67 | if(head==NULL) 68 | { 69 | cout<<"***FIRSTLY CREATE THE NODE***\n"; 70 | }else{ 71 | Node *temp=new Node; 72 | cout<<"Enter the data you want to insert : "; 73 | cin>>d; 74 | cout<<"After which data you want to insert : "; 75 | int da; 76 | cin>>da; 77 | temp->data=d; 78 | while(da!=p->data) 79 | { 80 | p=p->next; 81 | cnt++; 82 | } 83 | temp->next=p->next; 84 | p->next=temp;} 85 | } 86 | } 87 | 88 | void deleteNode() 89 | { 90 | cout<<"Enter the data of Node which you want to delete :"; 91 | int d; 92 | cin>>d; 93 | Node *p1=head; 94 | Node *p2=NULL; 95 | if(head==NULL) 96 | { 97 | cout<<"Insert Node First\n"; 98 | } 99 | else{ 100 | if(d==head->data) // delete first node 101 | { 102 | while(p1->next!=head) 103 | { 104 | p1=p1->next; 105 | } 106 | p1->next=head->next; 107 | head=head->next; 108 | }else // dlete any node 109 | { 110 | while(p1->data!=d) 111 | { 112 | p2=p1; 113 | p1=p1->next; 114 | } 115 | p2->next=p1->next; 116 | } 117 | } 118 | } 119 | 120 | int main() 121 | { 122 | int arr[] = {100,200,300,400,500}; 123 | int ch; 124 | cout<<"*************************CIRCULAR LINKED LIST********************************"; 125 | while(ch!=5) 126 | { 127 | cout<<"\n1) Create \n2) display \n3) insert \n4) delete\n5) exit \n"; 128 | cin>>ch; 129 | switch(ch) 130 | { 131 | case 1:create(arr,5); 132 | break; 133 | case 2:display(); 134 | break; 135 | case 3:insert(); 136 | break; 137 | case 4:deleteNode(); 138 | break; 139 | case 5:exit(1); 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /Linked List/Detect-Loop-In-Linked-List.py: -------------------------------------------------------------------------------- 1 | #The idea is to keep two pointers one will be incremented by one and other by two, 2 | #If they meet at same node at any point then there is loop in linked list 3 | def detect_loop(head): #returns True if loop is present 4 | pointer1 = head 5 | pointer2 = head 6 | while pointer1 and pointer2 and pointer2.next: 7 | pointer1 = pointer1.next 8 | pointer2 = pointer2.next.next 9 | if pointer1 == pointer2: 10 | return True 11 | return False 12 | -------------------------------------------------------------------------------- /Linked List/DoublyLinkedList.cpp: -------------------------------------------------------------------------------- 1 | // Circular Doubly Linked List is also similar to Doubly Linked List Only differnnce is that Last node next is pointing to first node and first node previous is pointing to last node so IN ORDER TO REACH LAST NODE YOU DONT NEED TO TRAVERSE ALL NODES jiust tpe the code head->prev 2 | #include 3 | using namespace std; 4 | struct Node 5 | { 6 | int data; 7 | Node *prev,*next; 8 | }*head=NULL; 9 | 10 | void create(int arr[],int size) 11 | { 12 | head = new Node; 13 | head->data=arr[0]; 14 | head->prev=head->next=NULL; 15 | Node *p=head; 16 | for(int i=1;idata=arr[i]; 20 | temp->next=p->next; 21 | temp->prev=p; 22 | p->next=temp; 23 | p=temp; 24 | } 25 | } 26 | 27 | void display() 28 | { 29 | Node *ptr=head; 30 | while(ptr) 31 | { 32 | cout<data<<" "; 33 | ptr=ptr->next; 34 | } 35 | } 36 | 37 | void length() 38 | { 39 | int cnt = 0; 40 | Node *p1=head; 41 | while(p1) 42 | { 43 | ++cnt; 44 | p1=p1->next; 45 | } 46 | cout<<"Length : "<>d; 55 | cout<<"Enter the New Node data : "; 56 | int da; 57 | cin>>da; 58 | Node *temp=new Node; 59 | temp->data=da; 60 | Node*p2=NULL; 61 | 62 | { 63 | while(p->data!=d ) 64 | { 65 | p=p->next; 66 | } 67 | temp->next=p->next; 68 | temp->prev=p; 69 | if(p->next!=NULL) // Checking whether the next node is present or not 70 | p->next->prev=temp; 71 | p->next=temp; 72 | } 73 | 74 | } 75 | 76 | void firstNode() 77 | { 78 | Node *p= head; 79 | cout<<"Enter the New Node data : "; 80 | int da; 81 | cin>>da; 82 | Node *temp=new Node; 83 | temp->data=da; 84 | 85 | temp->next=p; 86 | temp->prev=p->prev; 87 | p->prev=temp; 88 | 89 | head=temp; 90 | } 91 | 92 | void deleteNode() 93 | { 94 | Node *p1= head; 95 | cout<<"Enter the Node data which you want to delete : "; 96 | int da; 97 | cin>>da; 98 | Node *p2=NULL; 99 | if(head->data==da) 100 | { 101 | if(head->next!=NULL) 102 | { 103 | head->next->prev=NULL; 104 | head=head->next; 105 | } 106 | }else 107 | { 108 | while(p1->data!=da) 109 | { 110 | p2=p1; 111 | p1=p1->next; 112 | } 113 | p2->next=p1->next; 114 | if(p1->next!=NULL) // Condition for last node that next node is NULL or NOT 115 | p1->next->prev=p2; 116 | } 117 | } 118 | 119 | void reverse() 120 | { 121 | Node *p1=head; 122 | while(p1->next!=NULL) 123 | { 124 | p1=p1->next; 125 | } 126 | while(p1!=NULL) 127 | { 128 | cout<data<<" "; 129 | p1=p1->prev; 130 | } 131 | 132 | } 133 | 134 | int main() 135 | { 136 | int arr[] = {1000,2000,3000,4000,5000}; 137 | cout<<"*************************DOUBLY LINKED LIST********************************"; 138 | int ch; 139 | while(ch!=7) 140 | { 141 | cout<<"\n1) Create \n2) display \n3) Length \n4) Insert\n5) Insert at First \n6) Delete any Node \n7) Reverse\n"; 142 | cin>>ch; 143 | switch(ch) 144 | { 145 | case 1: create(arr,5); 146 | break; 147 | case 2: display(); 148 | break; 149 | case 3: length(); 150 | break; 151 | case 4: insert(); 152 | break; 153 | case 5: firstNode(); 154 | break; 155 | case 6: deleteNode(); 156 | break; 157 | case 7: reverse(); 158 | } 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /Linked List/SinglyLinkedList.cpp: -------------------------------------------------------------------------------- 1 | // STACK SIZE n+1 2 | // TIme : O(n) 3 | // We cannot apply Binary Search in Linked list 4 | #include 5 | using namespace std; 6 | struct Node 7 | { 8 | int rollNo; 9 | string name; 10 | Node *next; // next is a pointer of node type 11 | }*head=NULL; // head is a global pointer 12 | 13 | void insert() 14 | { 15 | if(head == NULL) 16 | { 17 | head = new Node; // head is global so we can direct access it 18 | cout<<"Enter RollNo : "; 19 | int a; 20 | cin>>a; 21 | cout<<"Enter Name : "; 22 | string b; 23 | cin>>b; 24 | head->rollNo = a; 25 | head->name = b; 26 | // head jis node ko point krra hh uska next me NULL dal do 27 | head->next = NULL; 28 | }else 29 | { 30 | Node *temp = new Node; // temp is pointer which is used for new node creation 31 | cout<<"Enter RollNo : "; 32 | int c; 33 | cin>>c; 34 | cout<<"Enter Name : "; 35 | string d; 36 | cin>>d; 37 | temp->rollNo = c; 38 | temp->name = d; 39 | Node *p=head; // p is a pointer which is used only for traversal 40 | while(p->next!=NULL) 41 | { 42 | p = p->next; 43 | } 44 | p->next=temp; 45 | temp->next=NULL; 46 | } 47 | } 48 | 49 | void count() 50 | { 51 | int cnt = 0; 52 | Node *pt=head; 53 | while(pt!=NULL) 54 | { 55 | cnt=cnt+1; 56 | pt=pt->next; 57 | } 58 | cout<<"Number of Nodes : "<name<<" "<<"RollNo : "<rollNo<<"\n"; 69 | pt=pt->next; 70 | } 71 | cout<<"--------------------------------------------------------------------------\n"; 72 | } 73 | 74 | void search() 75 | { 76 | cout<<"Enter the rollNo whose name you want to find : "; 77 | int finds; 78 | cin>>finds; 79 | Node *pt = head; 80 | if(head==NULL) 81 | { 82 | cout<<"Sorry No element present in List\n"; 83 | } 84 | else 85 | { 86 | while(pt->rollNo != finds) 87 | { 88 | pt = pt->next; 89 | } 90 | cout<<"Name : "<name<<"\n"; 91 | } 92 | } 93 | void makeFirstNode() 94 | { 95 | Node *pt = head; 96 | Node *q = NULL; 97 | cout<<"Enter the rollNo whose you want to see in first : "; 98 | int finds; 99 | cin>>finds; 100 | while(pt->rollNo!=finds) 101 | { 102 | q=pt; 103 | pt=pt->next; 104 | } 105 | q->next = pt->next; 106 | pt->next = head; 107 | head = pt; 108 | } 109 | void insertAnywhere() 110 | { 111 | cout<<"\nInsert at any Index accept last node\n"; 112 | 113 | Node *ptr = head; 114 | Node *temp = new Node; 115 | cout<<"Enter Name : "; 116 | string nam; 117 | cin>>nam; 118 | temp->name=nam; 119 | cout<<"Enter RollNo : "; 120 | int roll; 121 | cin>>roll; 122 | temp->rollNo=roll; 123 | int index; 124 | cout<<"After which RollNo you want to insert your Node : "; 125 | cin>>index; 126 | Node *q=NULL; 127 | while(ptr->rollNo!=index) 128 | { 129 | ptr=ptr->next; 130 | } 131 | temp->next=ptr->next; 132 | ptr->next=temp; 133 | 134 | } 135 | 136 | void deleteAnyNode() 137 | { 138 | Node *p1 = head; 139 | Node *p2=NULL; 140 | cout<<"Enter the rollNo which you want to delete : "; 141 | int r; 142 | cin>>r; 143 | if(head->rollNo == r) // Condition for if deleteing node is first one 144 | { 145 | Node *p = head; 146 | head=head->next; 147 | }else // for all other nodes 148 | { 149 | while(p1->rollNo != r) 150 | { 151 | p2=p1; 152 | p1=p1->next; 153 | } 154 | p2->next=p1->next; 155 | } 156 | } 157 | 158 | void dupliRoll() // REMOVED DUPLICATE ELEMNTS FROM SORTED LINKED LISTS) 159 | { 160 | Node *p1 = head; 161 | Node *p2 = head->next; 162 | 163 | while(p1->next!=NULL) 164 | { 165 | if(p1->rollNo!=p2->rollNo) 166 | { 167 | p1=p2; 168 | p2=p2->next; 169 | }else 170 | { 171 | p1->next=p2->next; 172 | p2=p1->next; 173 | } 174 | } 175 | } 176 | 177 | void reverse() 178 | { 179 | Node *p = head; 180 | Node *q = NULL; 181 | Node *r = NULL; 182 | 183 | while(p!=NULL) 184 | { 185 | r=q; 186 | q=p; 187 | p=p->next; 188 | q->next=r; 189 | } 190 | head = q; 191 | 192 | } 193 | int main() 194 | { 195 | cout<<"***********************LINKED LIST***************************\n"; 196 | int ch; 197 | do{ 198 | cout<<"\n1)Insertion\n2)Display\n3)Count Nodes\n4)Search\n5)Make any node as first node\n6)Inserting at any Position\n7)Deleting any node\n8)Remove Duplicate roll numbers\n9)Reverse Linked List\n10)Exit\n"; 199 | cout<<"Enter your Choice : "; 200 | cin>>ch; 201 | switch(ch) 202 | { 203 | case 1 : insert(); 204 | break; 205 | case 2 : display(); 206 | break; 207 | case 3 : count(); 208 | break; 209 | case 4 : search(); 210 | break; 211 | case 5 : makeFirstNode(); 212 | break; 213 | case 6 : insertAnywhere(); 214 | break; 215 | case 7 : deleteAnyNode(); 216 | break; 217 | case 8 : dupliRoll(); 218 | break; 219 | case 9 : reverse(); 220 | break; 221 | case 10 : exit(1); 222 | }while(ch!=10); 223 | } 224 | } 225 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # data-structures-algorithms 2 | Codes for important Data Structures and Algorithms (Mostly C++) 3 | 4 | -------------------------------------------------------------------------------- /Segment Tree/Segment_tree.cpp: -------------------------------------------------------------------------------- 1 | //Segment Tree 2 | //Updation and Finding sum in given range happens in O(logn) 3 | #include 4 | using namespace std; 5 | 6 | int getMid(int s, int e) { 7 | return s + (e -s)/2; 8 | } 9 | 10 | int getSumUtil(int *st, int ss, int se, int qs, int qe, int si) 11 | { 12 | if (qs <= ss && qe >= se) 13 | return st[si]; 14 | 15 | if (se < qs || ss > qe) 16 | return 0; 17 | 18 | int mid = getMid(ss, se); 19 | return getSumUtil(st, ss, mid, qs, qe, 2*si+1) + 20 | getSumUtil(st, mid+1, se, qs, qe, 2*si+2); 21 | } 22 | 23 | void updateValueUtil(int *st, int ss, int se, int i, int diff, int si) 24 | { 25 | 26 | if (i < ss || i > se) 27 | return; 28 | 29 | st[si] = st[si] + diff; 30 | if (se != ss) 31 | { 32 | int mid = getMid(ss, se); 33 | updateValueUtil(st, ss, mid, i, diff, 2*si + 1); 34 | updateValueUtil(st, mid+1, se, i, diff, 2*si + 2); 35 | } 36 | } 37 | 38 | void updateValue(int arr[], int *st, int n, int i, int new_val) 39 | { 40 | 41 | if (i < 0 || i > n-1) 42 | { 43 | cout<<"Invalid Input"; 44 | return; 45 | } 46 | 47 | int diff = new_val - arr[i]; 48 | 49 | arr[i] = new_val; 50 | 51 | updateValueUtil(st, 0, n-1, i, diff, 0); 52 | } 53 | 54 | int getSum(int *st, int n, int qs, int qe) 55 | { 56 | 57 | if (qs < 0 || qe > n-1 || qs > qe) 58 | { 59 | cout<<"Invalid Input"; 60 | return -1; 61 | } 62 | 63 | return getSumUtil(st, 0, n-1, qs, qe, 0); 64 | } 65 | 66 | int constructSTUtil(int arr[], int ss, int se, int *st, int si) 67 | { 68 | 69 | if (ss == se) 70 | { 71 | st[si] = arr[ss]; 72 | return arr[ss]; 73 | } 74 | 75 | int mid = getMid(ss, se); 76 | st[si] = constructSTUtil(arr, ss, mid, st, si*2+1) + 77 | constructSTUtil(arr, mid+1, se, st, si*2+2); 78 | return st[si]; 79 | } 80 | 81 | int *constructST(int arr[], int n) 82 | { 83 | 84 | int x = (int)(ceil(log2(n))); 85 | 86 | int max_size = 2*(int)pow(2, x) - 1; 87 | 88 | int *st = new int[max_size]; 89 | 90 | constructSTUtil(arr, 0, n-1, st, 0); 91 | 92 | return st; 93 | } 94 | 95 | int main() 96 | { 97 | int *arr , n , i , a , b ; 98 | cout << "Enter size of the array" << endl ; 99 | cin >> n ; 100 | arr = new int[n] ; 101 | cout << "Enter the elements of the array" << endl ; 102 | for ( i = 0 ; i < n ; i++ ) 103 | cin >> arr[i] ; 104 | int *st = constructST(arr, n); 105 | int k = 1 ; 106 | do{ 107 | cout << "Enter 1 to find the sum of the values in the given range" << endl ; 108 | cout << "Enter 2 to update a value in the array" << endl ; 109 | cout << "Enter 0 to exit" << endl ; 110 | cout << "Enter your choice" << endl ; 111 | int k ; 112 | cin >> k ; 113 | switch(k){ 114 | case 1 : cout << "Enter the range" << endl ; 115 | cin >> a >> b ; 116 | cout<<"Sum of values in given range = "<> a >> b ; 121 | updateValue(arr, st, n, a, b); 122 | cout << "Updated array is : " ; 123 | for ( i = 0 ; i < n ; i++ ) 124 | cout << arr[i] << " " ; 125 | cout << endl ; 126 | break ; 127 | 128 | case 0 : exit(0) ; 129 | } 130 | } 131 | while (1); 132 | 133 | 134 | 135 | return 0; 136 | } 137 | 138 | /*Sample Output : 139 | Enter size of the array 140 | 5 141 | Enter the elements of the array 142 | 1 3 5 7 9 143 | Enter 1 to find the sum of the values in the given range 144 | Enter 2 to update a value in the array 145 | Enter 0 to exit 146 | Enter your choice 147 | 1 148 | Enter the range 149 | 1 3 150 | Sum of values in given range = 15 151 | Enter 1 to find the sum of the values in the given range 152 | Enter 2 to update a value in the array 153 | Enter 0 to exit 154 | Enter your choice 155 | 2 156 | Enter the position to be updated and number to be inserted 157 | 1 10 158 | Updated array is : 1 10 5 7 9 159 | Enter 1 to find the sum of the values in the given range 160 | Enter 2 to update a value in the array 161 | Enter 0 to exit 162 | Enter your choice 163 | 1 164 | Enter the range 165 | 1 3 166 | Sum of values in given range = 22 167 | Enter 1 to find the sum of the values in the given range 168 | Enter 2 to update a value in the array 169 | Enter 0 to exit 170 | Enter your choice 171 | 0 172 | */ 173 | 174 | 175 | -------------------------------------------------------------------------------- /Segment Tree/segmenttree(max).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void buildtree(int *tree,int *a, int index, int s, int e) 5 | { 6 | 7 | //base case 8 | if(s > e) 9 | return; 10 | 11 | //base class leaf node 12 | if(s == e) 13 | { 14 | tree[index] = a[s]; 15 | return; 16 | } 17 | 18 | //recursive case 19 | int mid = (s + e)/2; 20 | 21 | //build left sub tree 22 | buildtree(tree,a,2*index,s, mid); 23 | 24 | //build right sub tree 25 | buildtree(tree, a, 2*index+1, mid+1, e); 26 | 27 | //getting min value 28 | int left = tree[2*index]; 29 | int right = tree[2*index + 1]; 30 | 31 | tree[index] = min(left, right); 32 | 33 | } 34 | 35 | int query(int *tree,int index, int s, int e, int qs, int qe) 36 | { 37 | //complete overlap condition 38 | if(qs <= s and qe >= e) 39 | return tree[index]; 40 | 41 | //No overlap condition 42 | if(qe < s or qs > e) 43 | return INT_MIN; 44 | 45 | //Partial Overlap 46 | int mid = (s+e)/2; 47 | //getting results from left and right 48 | int left = query(tree, index*2, s, mid,qs,qe); 49 | int right = query(tree, index*2+1, mid+1, e,qs,qe); 50 | 51 | //returning the minimum one 52 | return max(left, right); 53 | } 54 | 55 | //to update a single node 56 | void updatenode(int *tree, int index, int s, int e, int i, int val) 57 | { 58 | //no overlap 59 | if(i < s or i > e) 60 | return; 61 | 62 | //reached leaf node 63 | if(s == e) 64 | { 65 | tree[index] = val; 66 | return; 67 | } 68 | 69 | //partial overlap (i.e. i is lying between s and e) 70 | int mid = (s + e)/2; 71 | updatenode(tree, 2*index, s, mid, i, val); 72 | updatenode(tree, 2*index + 1, mid + 1, e, i, val); 73 | 74 | //updating parent nodes 75 | tree[index] = max(tree[2*index], tree[2*index + 1]); 76 | 77 | return; 78 | } 79 | 80 | //incrementing a range with a particular value 81 | void updaterange(int *tree, int index, int s, int e, int rs, int re, int inc) 82 | { 83 | 84 | //no overlap 85 | if(re < s or rs > e) 86 | return; 87 | 88 | if(s==e) 89 | { 90 | tree[index] += inc; 91 | return; 92 | } 93 | 94 | int mid = (s + e)/2; 95 | updaterange(tree, 2*index, s, mid, rs, re, inc); 96 | updaterange(tree, 2*index + 1, mid + 1, e, rs, re, inc); 97 | 98 | //updating parent nodes 99 | tree[index] = max(tree[2*index], tree[2*index + 1]); 100 | return; 101 | } 102 | 103 | 104 | int main() 105 | { 106 | int n; 107 | cout<<"Enter the no. of elements: "; 108 | cin>>n; 109 | int a[n]; 110 | for(int i=0; i>a[i]; 114 | } 115 | //starting and ending values 116 | int s = 0; 117 | int e = n-1; 118 | 119 | 120 | //making a tree array of size(4*n + 1).[4*n + 1 is an upper bound] 121 | int tree[(4*n) + 1]; 122 | 123 | //building the tree 124 | buildtree(tree, a, 1, s, e); 125 | 126 | int q; 127 | cout<<"Enter the number of queries: "; 128 | cin>>q; 129 | 130 | int l, r, i, val; 131 | 132 | while(q--) 133 | { 134 | int ch; 135 | cout<<"Press 1 to get the maximum value between a range"<>ch; 140 | 141 | switch(ch) 142 | { 143 | case 1: cout<<"Range starts from (0 based indexing): "; 144 | cin>>l; 145 | cout<<"Range ends at (0 based indexing): "; 146 | cin>>r; 147 | cout<<"maximum number in range "<>i; 152 | cout<<"Enter the updated value: "; 153 | cin>>val; 154 | updatenode(tree, 1, s, e, i, val); 155 | cout<<"value updated successfully."<>l; 160 | cout<<"Range ends at (0 based indexing): "; 161 | cin>>r; 162 | cout<<"Range updated from "< 2 | using namespace std; 3 | 4 | void buildtree(int *tree,int *a, int index, int s, int e) 5 | { 6 | 7 | //base case 8 | if(s > e) 9 | return; 10 | 11 | //base class leaf node 12 | if(s == e) 13 | { 14 | tree[index] = a[s]; 15 | return; 16 | } 17 | 18 | //recursive case 19 | int mid = (s + e)/2; 20 | 21 | //build left sub tree 22 | buildtree(tree,a,2*index,s, mid); 23 | 24 | //build right sub tree 25 | buildtree(tree, a, 2*index+1, mid+1, e); 26 | 27 | //getting min value 28 | int left = tree[2*index]; 29 | int right = tree[2*index + 1]; 30 | 31 | tree[index] = min(left, right); 32 | 33 | } 34 | 35 | int query(int *tree,int index, int s, int e, int qs, int qe) 36 | { 37 | //complete overlap condition 38 | if(qs <= s and qe >= e) 39 | return tree[index]; 40 | 41 | //No overlap condition 42 | if(qe < s or qs > e) 43 | return INT_MAX; 44 | 45 | //Partial Overlap 46 | int mid = (s+e)/2; 47 | //getting results from left and right 48 | int left = query(tree, index*2, s, mid,qs,qe); 49 | int right = query(tree, index*2+1, mid+1, e,qs,qe); 50 | 51 | //returning the minimum one 52 | return min(left, right); 53 | } 54 | 55 | //to update a single node 56 | void updatenode(int *tree, int index, int s, int e, int i, int val) 57 | { 58 | //no overlap 59 | if(i < s or i > e) 60 | return; 61 | 62 | //reached leaf node 63 | if(s == e) 64 | { 65 | tree[index] = val; 66 | return; 67 | } 68 | 69 | //partial overlap (i.e. i is lying between s and e) 70 | int mid = (s + e)/2; 71 | updatenode(tree, 2*index, s, mid, i, val); 72 | updatenode(tree, 2*index + 1, mid + 1, e, i, val); 73 | 74 | //updating parent nodes 75 | tree[index] = min(tree[2*index], tree[2*index + 1]); 76 | 77 | return; 78 | } 79 | 80 | //incrementing a range with a particular value 81 | void updaterange(int *tree, int index, int s, int e, int rs, int re, int inc) 82 | { 83 | 84 | //no overlap 85 | if(re < s or rs > e) 86 | return; 87 | 88 | if(s==e) 89 | { 90 | tree[index] += inc; 91 | return; 92 | } 93 | 94 | int mid = (s + e)/2; 95 | updaterange(tree, 2*index, s, mid, rs, re, inc); 96 | updaterange(tree, 2*index + 1, mid + 1, e, rs, re, inc); 97 | 98 | //updating parent nodes 99 | tree[index] = min(tree[2*index], tree[2*index + 1]); 100 | return; 101 | } 102 | 103 | 104 | int main() 105 | { 106 | int n; 107 | cout<<"Enter the no. of elements: "; 108 | cin>>n; 109 | int a[n]; 110 | for(int i=0; i>a[i]; 114 | } 115 | //starting and ending values 116 | int s = 0; 117 | int e = n-1; 118 | 119 | 120 | //making a tree array of size(4*n + 1).[4*n + 1 is an upper bound] 121 | int tree[(4*n) + 1]; 122 | 123 | //building the tree 124 | buildtree(tree, a, 1, s, e); 125 | 126 | int q; 127 | cout<<"Enter the number of queries: "; 128 | cin>>q; 129 | 130 | int l, r, i, val; 131 | 132 | while(q--) 133 | { 134 | int ch; 135 | cout<<"Press 1 to get the minimum value between a range"<>ch; 140 | 141 | switch(ch) 142 | { 143 | case 1: cout<<"Range starts from (0 based indexing): "; 144 | cin>>l; 145 | cout<<"Range ends at (0 based indexing): "; 146 | cin>>r; 147 | cout<<"minimum number in range "<>i; 152 | cout<<"Enter the updated value: "; 153 | cin>>val; 154 | updatenode(tree, 1, s, e, i, val); 155 | cout<<"value updated successfully."<>l; 160 | cout<<"Range ends at (0 based indexing): "; 161 | cin>>r; 162 | cout<<"Range updated from "< 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef struct node 7 | { 8 | char data; 9 | struct node *next; 10 | }node; 11 | 12 | class stack 13 | { 14 | node *top; 15 | public: 16 | stack() 17 | { 18 | top=NULL; 19 | } 20 | 21 | int isEmpty() 22 | { 23 | if(top==NULL) 24 | { 25 | return 1; 26 | } 27 | return 0; 28 | } 29 | 30 | 31 | int push(char data) 32 | { 33 | node *p; 34 | p=new node(); 35 | p->data=data; 36 | p->next=top; 37 | top=p; 38 | return p->data; 39 | } 40 | 41 | 42 | int pop() 43 | { 44 | char x; 45 | node *p; 46 | p=top; 47 | x=top->data; 48 | top=top->next; 49 | delete(p); 50 | return x; 51 | } 52 | 53 | 54 | void display() 55 | { 56 | node *p; 57 | for(p=top;p!=NULL;p=p->next) 58 | { 59 | cout<data<<"-"; 60 | } 61 | } 62 | 63 | int topData() 64 | { 65 | return top->data; 66 | } 67 | 68 | }; 69 | 70 | int main() 71 | { 72 | stack k; 73 | char a,b,data; 74 | int ch; 75 | 76 | while(1){ 77 | 78 | cout<<"\n1-Push\n2-Pop\n3-Display\n4-Exit\nEnter your choice!!!\n"; 79 | cin>>ch; 80 | 81 | switch(ch){ 82 | case 1: cout<<"\nEnter the data-\t"; 83 | cin>>data; 84 | b=k.push(data); 85 | cout<<"Successfully pushed->"<"< // for standard IO operations 8 | #include // defines one macro function (assert) that can be used as a standard debugging tool 9 | #include // This header is part of the containers library, designed to operate in FIFO context 10 | 11 | namespace stack_using_queue { 12 | /** 13 | * @brief Assuming we already have a class implemented for Queue, we will first design the class for Stack. 14 | * It will have the methods push() , pop() , top() , and empty() and one queue. 15 | */ 16 | class MyStack 17 | { 18 | /** initialize queue */ 19 | std::queue q; //empty queue q of int type 20 | 21 | public: 22 | 23 | MyStack() = default; //default constructor for stack 24 | 25 | /** 26 | * @brief Function to Push element onto stack 27 | * @param x number to be pushed 28 | * @returns void 29 | */ 30 | void push(int x) 31 | { 32 | q.push(x); //let's assume x=3,queue=1,2 , then queue will become 1,2,3 33 | int s=q.size(); //get size of queue 34 | for(int i=0;i 2,3,1 -> 3,1,2) 35 | { 36 | int y=q.front(); //because if we want to remove from stack so our element should be at front in queue 37 | q.pop(); 38 | q.push(y); 39 | } 40 | } 41 | /** 42 | * @brief Function to Remove the element on top of the stack and return that element. 43 | * @returns element that is deleted from the stack 44 | */ 45 | int pop() 46 | { 47 | int y=q.front(); 48 | q.pop(); 49 | return y; 50 | } 51 | /** 52 | * @brief Function to Get the topmost element from stack. 53 | * @returns element on the top of stack 54 | */ 55 | int top() 56 | { 57 | return q.front(); 58 | } 59 | /** 60 | * @brief Function to Return whether the stack is empty. 61 | * @return if stack is empty, returns true, else false 62 | */ 63 | bool empty() 64 | { 65 | return q.empty(); 66 | } 67 | }; 68 | } // namespace stack_using_queue 69 | 70 | /** 71 | * @brief Test Implementations 72 | * @returns void 73 | */ 74 | static void tests() 75 | { 76 | stack_using_queue::MyStack obj; // object of stack_using_queue::MyStack class 77 | 78 | std::cout << "Test #1\n"; 79 | obj.push(2); 80 | obj.push(5); 81 | obj.push(0); 82 | assert(obj.top() == 0); 83 | assert(obj.pop() == 0); 84 | assert(obj.top() == 5); 85 | assert(obj.pop() == 5); 86 | assert(obj.top() == 2); 87 | assert(obj.pop() == 2); 88 | assert(obj.empty() == true); 89 | std::cout << "PASSED TEST 1\n"; 90 | 91 | std::cout << "Test #2\n"; 92 | obj.push(-1); 93 | assert(obj.empty() == false); 94 | assert(obj.top() == -1); 95 | assert(obj.pop() == -1); 96 | std::cout << "PASSED TEST 2\n"; 97 | } 98 | /** 99 | * @brief Main function calling tests function 100 | * @returns 0 on exit 101 | */ 102 | int main() 103 | { 104 | tests(); // Execute the Tests 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /Trees/AVL-Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node 5 | { 6 | int data; 7 | int height; 8 | struct node *right; 9 | struct node *left; 10 | }*root,*t1,*t2,*t3,*temp, *p; 11 | 12 | class AVL 13 | { 14 | public: 15 | 16 | node* insert(node*p, int key) 17 | { 18 | if(p == NULL) 19 | { 20 | p = new node; 21 | p->data = key; 22 | p->height = 1; 23 | p->right = NULL; 24 | p->left = NULL; 25 | 26 | return p; 27 | } 28 | if( key < p->data) 29 | p->left = insert(p->left,key); 30 | 31 | else if( key > p->data) 32 | p->right = insert(p->right,key); 33 | 34 | /***********UPDATE THE HEIGHT**********/ 35 | 36 | p->height = NodeHeight(p); 37 | 38 | /**********CHECK FOR BALANCE FACTOR FOR ROTATIONS*********/ 39 | 40 | /***See if Balance factor is +ve than we have to check left side else if -ve we have to check right side***/ 41 | 42 | if((BalanceFactor(p) == 2) and (BalanceFactor(p->left) == 1)) 43 | return LLrotation(p); 44 | else if((BalanceFactor(p) == 2) and (BalanceFactor(p->left) == -1)) 45 | return LRrotation(p); 46 | else if((BalanceFactor(p) == -2) and (BalanceFactor(p->right) == 1)) 47 | return RLrotation(p); 48 | else if((BalanceFactor(p) == -2) and (BalanceFactor(p->right) == -1)) 49 | return RRrotation(p); 50 | 51 | 52 | return p; 53 | } 54 | 55 | int NodeHeight(node *t1) 56 | { 57 | int hl = t1 && t1->left ? t1 -> left -> height : 0; 58 | int hr = t1 && t1->right ? t1 -> right -> height : 0; 59 | 60 | if(hl > hr) 61 | return hl + 1; 62 | else 63 | return hr + 1; 64 | } 65 | 66 | int BalanceFactor(node *t1) 67 | { 68 | int hl = t1 && t1->left ? t1 -> left -> height : 0; 69 | int hr = t1 && t1->right ? t1 -> right -> height : 0; 70 | 71 | return hl - hr; 72 | } 73 | 74 | node* LLrotation(node *t) 75 | { 76 | node *tl = t->left; 77 | node *tlr = tl->right; 78 | 79 | tl->right = t; 80 | t->left = tlr; 81 | 82 | t->height = NodeHeight(t); 83 | tl->height = NodeHeight(tl); 84 | 85 | if(t == root) 86 | root = tl; 87 | 88 | return tl; 89 | } 90 | 91 | node* LRrotation(node *t) 92 | { 93 | node *tl = t->left; 94 | node *tlr = tl->right; 95 | 96 | tl->right = tlr->left; 97 | t->left = tlr->right; 98 | 99 | 100 | tlr->left = tl; 101 | tlr->right = t; 102 | 103 | tl->height=NodeHeight(tl); 104 | t->height=NodeHeight(t); 105 | tlr->height=NodeHeight(tlr); 106 | 107 | if(root==t) 108 | root=tlr; 109 | return tlr; 110 | } 111 | 112 | node* RRrotation(node *t) 113 | { 114 | node *tr = t->right; 115 | node *trl = tr->left; 116 | 117 | tr->left = t; 118 | t->right = trl; 119 | 120 | t->height = NodeHeight(t); 121 | tr->height = NodeHeight(tr); 122 | 123 | if(t == root) 124 | root = tr; 125 | 126 | return tr; 127 | } 128 | 129 | node* RLrotation(node *t) 130 | { 131 | node *tr = t->right; 132 | node *trl = tr->left; 133 | 134 | tr->left = trl->right; 135 | t->right = trl->left; 136 | 137 | 138 | trl->left = t; 139 | trl->right = tr; 140 | 141 | tr->height=NodeHeight(tr); 142 | t->height=NodeHeight(t); 143 | trl->height=NodeHeight(trl); 144 | 145 | if(root==t) 146 | root=trl; 147 | return trl; 148 | } 149 | 150 | node* Search(node *p,int key) 151 | { 152 | 153 | while(p != NULL) 154 | { 155 | if(key == p->data) 156 | return p; 157 | 158 | else if(key < p->data) 159 | p = p->left; 160 | 161 | else if(key > p->data) 162 | p = p->right; 163 | } 164 | return NULL; 165 | } 166 | 167 | 168 | void preorder(node *t2) 169 | { 170 | if(t2 == NULL) 171 | return; 172 | cout<data<<" "; 173 | preorder(t2->left); 174 | preorder(t2->right); 175 | } 176 | 177 | void inorder(node *t2) 178 | { 179 | if(t2 == NULL) 180 | return; 181 | inorder(t2->left); 182 | cout<data<<" "; 183 | inorder(t2->right); 184 | } 185 | 186 | void postorder(node *t2) 187 | { 188 | if(t2 == NULL) 189 | return; 190 | postorder(t2->left); 191 | postorder(t2->right); 192 | cout<data<<" "; 193 | } 194 | 195 | void leftmost(node *t2) 196 | { 197 | while(t2->left!= NULL) 198 | t2 = t2->left; 199 | cout<<"\n========================================\n"; 200 | cout<<"Smallest number(Leftmost) is- "<data<right!= NULL) 207 | t2 = t2->right; 208 | cout<<"\n========================================\n"; 209 | cout<<"Biggest number(rightmost) is- "<data<left); 219 | int s2 = Height(p->right); 220 | 221 | if(s1>s2) 222 | return s1 + 1; 223 | else 224 | return s2 + 1; 225 | } 226 | 227 | return 0; 228 | } 229 | 230 | int Count(node *p) 231 | { 232 | 233 | if(p != NULL) 234 | { 235 | int x = Count(p->left); 236 | int y = Count(p->right); 237 | return x+y+1; 238 | } 239 | return 0; 240 | } 241 | 242 | int Countdouble(node *p) 243 | { 244 | 245 | if(p != NULL) 246 | { 247 | int x = Countdouble(p->left); 248 | int y = Countdouble(p->right); 249 | 250 | if((p->left != NULL) and (p->right != NULL)) 251 | return x+y+1; 252 | else 253 | return x+y; 254 | } 255 | return 0; 256 | } 257 | 258 | int Countleaf(node *p) 259 | { 260 | 261 | if(p != NULL) 262 | { 263 | int x = Countleaf(p->left); 264 | int y = Countleaf(p->right); 265 | 266 | if((p->left == NULL) and (p->right == NULL)) 267 | return x+y+1; 268 | else 269 | return x+y; 270 | } 271 | return 0; 272 | } 273 | 274 | 275 | 276 | }; 277 | int main() 278 | { 279 | class AVL b1; 280 | int ch,newdata; 281 | root = NULL; 282 | cout<<"Enter the value of root node: "; 283 | cin>>newdata; 284 | root = b1.insert(root,newdata); 285 | while(1) 286 | { 287 | cout<<"\nPress 1 to insert new element"<>ch; 299 | switch(ch) 300 | { 301 | case 1: cout<<"\nEnter the value of the element to be inserted:- "; 302 | cin>>newdata; 303 | b1.insert(root,newdata); 304 | cout<<"\n=======element entered successfully========\n"; 305 | break; 306 | case 2: cout<<"\nEnter the value of the element to be searched:- "; 307 | cin>>newdata; 308 | temp = b1.Search(root,newdata); 309 | if(temp != NULL) 310 | cout<<"Found!\n"; 311 | else 312 | cout<<"Not Found!\n"; 313 | break; 314 | case 3: cout<<"\n========================================\n"; 315 | cout<<"Preoder travelsal:- "; 316 | b1.preorder(root); 317 | cout<<"\n========================================\n"; 318 | break; 319 | case 4: cout<<"\n========================================\n"; 320 | cout<<"Inoder travelsal:- "; 321 | b1.inorder(root); 322 | cout<<"\n========================================\n"; 323 | break; 324 | case 5: cout<<"\n========================================\n"; 325 | cout<<"Postoder travelsal:- "; 326 | b1.postorder(root); 327 | cout<<"\n========================================\n"; 328 | break; 329 | case 6: b1.leftmost(root); 330 | break; 331 | case 7: b1.rightmost(root); 332 | break; 333 | case 8: cout<<"\n========================================\n"; 334 | cout<<"Height of tree is: "< 2 | using namespace std; 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node *right; 8 | struct node *left; 9 | }*root,*t1,*t2,*t3,*temp, *p; 10 | 11 | class binarytree 12 | { 13 | public: 14 | 15 | node* insert(node*p, int key) 16 | { 17 | if(p == NULL) 18 | { 19 | p = new node; 20 | p->data = key; 21 | p->right = NULL; 22 | p->left = NULL; 23 | 24 | return p; 25 | } 26 | if( key < p->data) 27 | p->left = insert(p->left,key); 28 | 29 | else if( key > p->data) 30 | p->right = insert(p->right,key); 31 | 32 | return p; 33 | } 34 | 35 | node* Search(node *p,int key) 36 | { 37 | 38 | while(p != NULL) 39 | { 40 | if(key == p->data) 41 | return p; 42 | 43 | else if(key < p->data) 44 | p = p->left; 45 | 46 | else if(key > p->data) 47 | p = p->right; 48 | } 49 | return NULL; 50 | } 51 | 52 | 53 | void preorder(node *t2) 54 | { 55 | if(t2 == NULL) 56 | return; 57 | cout<data<<" "; 58 | preorder(t2->left); 59 | preorder(t2->right); 60 | } 61 | 62 | void inorder(node *t2) 63 | { 64 | if(t2 == NULL) 65 | return; 66 | inorder(t2->left); 67 | cout<data<<" "; 68 | inorder(t2->right); 69 | } 70 | 71 | void postorder(node *t2) 72 | { 73 | if(t2 == NULL) 74 | return; 75 | postorder(t2->left); 76 | postorder(t2->right); 77 | cout<data<<" "; 78 | } 79 | 80 | void leftmost(node *t2) 81 | { 82 | while(t2->left!= NULL) 83 | t2 = t2->left; 84 | cout<<"\n========================================\n"; 85 | cout<<"Smallest number(Leftmost) is- "<data<right!= NULL) 92 | t2 = t2->right; 93 | cout<<"\n========================================\n"; 94 | cout<<"Biggest number(rightmost) is- "<data<left); 104 | int s2 = Height(p->right); 105 | 106 | if(s1>s2) 107 | return s1 + 1; 108 | else 109 | return s2 + 1; 110 | } 111 | 112 | return 0; 113 | } 114 | 115 | int Count(node *p) 116 | { 117 | 118 | if(p != NULL) 119 | { 120 | int x = Count(p->left); 121 | int y = Count(p->right); 122 | return x+y+1; 123 | } 124 | return 0; 125 | } 126 | 127 | int Countdouble(node *p) 128 | { 129 | 130 | if(p != NULL) 131 | { 132 | int x = Countdouble(p->left); 133 | int y = Countdouble(p->right); 134 | 135 | if((p->left != NULL) and (p->right != NULL)) 136 | return x+y+1; 137 | else 138 | return x+y; 139 | } 140 | return 0; 141 | } 142 | 143 | int Countleaf(node *p) 144 | { 145 | 146 | if(p != NULL) 147 | { 148 | int x = Countleaf(p->left); 149 | int y = Countleaf(p->right); 150 | 151 | if((p->left == NULL) and (p->right == NULL)) 152 | return x+y+1; 153 | else 154 | return x+y; 155 | } 156 | return 0; 157 | } 158 | 159 | 160 | 161 | }; 162 | int main() 163 | { 164 | class binarytree b1; 165 | int ch,newdata; 166 | root = NULL; 167 | cout<<"Enter the value of root node: "; 168 | cin>>newdata; 169 | root = b1.insert(root,newdata); 170 | while(1) 171 | { 172 | cout<<"\nPress 1 to insert new element"<>ch; 184 | switch(ch) 185 | { 186 | case 1: cout<<"\nEnter the value of the element to be inserted:- "; 187 | cin>>newdata; 188 | b1.insert(root,newdata); 189 | cout<<"\n=======element entered successfully========\n"; 190 | break; 191 | case 2: cout<<"\nEnter the value of the element to be searched:- "; 192 | cin>>newdata; 193 | temp = b1.Search(root,newdata); 194 | if(temp != NULL) 195 | cout<<"Found!\n"; 196 | else 197 | cout<<"Not Found!\n"; 198 | break; 199 | case 3: cout<<"\n========================================\n"; 200 | cout<<"Preoder travelsal:- "; 201 | b1.preorder(root); 202 | cout<<"\n========================================\n"; 203 | break; 204 | case 4: cout<<"\n========================================\n"; 205 | cout<<"Inoder travelsal:- "; 206 | b1.inorder(root); 207 | cout<<"\n========================================\n"; 208 | break; 209 | case 5: cout<<"\n========================================\n"; 210 | cout<<"Postoder travelsal:- "; 211 | b1.postorder(root); 212 | cout<<"\n========================================\n"; 213 | break; 214 | case 6: b1.leftmost(root); 215 | break; 216 | case 7: b1.rightmost(root); 217 | break; 218 | case 8: cout<<"\n========================================\n"; 219 | cout<<"Height of tree is: "< 2 | using namespace std; 3 | 4 | struct node{ 5 | node *next[150] = {NULL}; 6 | int eow = 0; 7 | string meaning = "kothariji"; 8 | }*head = NULL,t1; 9 | 10 | void insert(string s, string mean) 11 | { 12 | if(head == NULL) 13 | head = new node; 14 | 15 | node *temp = head; 16 | int i = 0; 17 | while(i< s.length() and temp -> next[s[i]-'a'] != NULL) 18 | { 19 | temp = temp -> next[s[i]-'a']; 20 | i++; 21 | } 22 | while(i next[s[i]-'a'] = t1; 26 | temp = t1; 27 | i++; 28 | } 29 | temp -> meaning = mean; 30 | } 31 | 32 | void search(string s) 33 | { 34 | int i = 0,flag = 0; 35 | node *temp = head; 36 | while(i < s.length()) 37 | { 38 | if(temp -> next[s[i]-'a'] == NULL) 39 | { 40 | flag =1; 41 | break; 42 | } 43 | temp = temp -> next[s[i]-'a']; 44 | i++; 45 | } 46 | if(flag == 0 and temp -> meaning != "kothariji") 47 | cout<<"Meaning: "<<" "< meaning<<"\n\n"<>ch; 62 | switch(ch) 63 | { 64 | case 1: cout<<"Enter Word: "; 65 | cin>>w; 66 | cout<<"Enter meaning: "; 67 | cin>>m; 68 | insert(w,m); 69 | cout<<"\n\nWord inserted successfully\n\n"<>w; 74 | search(w); 75 | break; 76 | 77 | case 3: cout<<"Thanks for using our service!"<