diff --git a/drivercode/BSTDriver b/drivercode/BSTDriver new file mode 100755 index 0000000..667ea48 Binary files /dev/null and b/drivercode/BSTDriver differ diff --git a/drivercode/BSTDriver.cpp b/drivercode/BSTDriver.cpp index 5bc7709..c2cb560 100644 --- a/drivercode/BSTDriver.cpp +++ b/drivercode/BSTDriver.cpp @@ -3,7 +3,8 @@ #include #include #include - +#include +#include using namespace std; @@ -11,68 +12,203 @@ class TreeNode { public: int val; TreeNode * left; - TreeNode * right; - // Add constructor. + TreeNode * right; + + // Add constructor. + TreeNode(){ + + this->right = this->left = NULL; + } + TreeNode(int val){ + this->val = val; + this->right = this->left = NULL; + } }; vector generateRandomArray(int size) { - return vector(size); + vector arr(size); + for(int i=0;ileft),height(root->right)) + 1; + +} +void inorder(TreeNode* root){ + if(root==NULL) + return; + inorder(root->left); + cout<val<<" "; + inorder(root->right); + return; +} +void preorder(TreeNode* root){ + if(root == NULL) + return; + cout<val<<" "; + preorder(root->left); + preorder(root->right); + return; +} +void postorder(TreeNode * root){ + if(root == NULL) + return; + postorder(root->left); + postorder(root->right); + cout<val<<" "; + return; } - void printTraversals(TreeNode * root) { // print inorder + cout<<"Inorder"<val == val) + return root; + if(root->val > val) + return search(root->left, val); + + return search(root->right, val); } // Return parent even if val is not present in tree. // Basically return the parent of the location where val should have been. TreeNode* searchParent(TreeNode * root, int val) { - return NULL; + if(root == NULL) + return root; + + if(root->left != NULL and root->left->val == val) + return root; + if(root->right != NULL and root->right->val == val) + return root; + if(root->val>val) + return searchParent(root->left,val); + return searchParent(root->right,val); } TreeNode * insert(TreeNode * root, int val) { - return root; + if (root == NULL) { + TreeNode* t=new TreeNode(val); + return t; + } + + + if (val< root->val) { + root->left = insert(root->left, val); + } + else { + root->right = insert(root->right, val); + } + + return root; + + } int maxValue(TreeNode * root) { - return INT_MAX; + if(root->right == NULL) + return root->val; + + return maxValue(root->right); } int minValue(TreeNode * root) { - return INT_MIN; + if(root->left == NULL) + return root->val; + + return minValue(root->left); } // Return NULL if val has no successor, ie, val is greater than maxValue(root); // Successor should be returned even if val is not present in the tree. -TreeNode * findSuccessor(TreeNode * root) { - return NULL; +TreeNode * findSuccessor(TreeNode * root,TreeNode* Root) { + + if(root == NULL) + { return root;} + if(root->right) + return search(root->right,minValue(root->right)); + TreeNode * t = searchParent(Root,root->val); + while( t!=NULL and root == t->right){ + root = t; + t = searchParent(Root,root->val); + } + return t; } // Return NULL if val has no predecessor, ie, val is smaller than minValue(root); // Predecessor should be returned even if val is not present in the tree. -TreeNode * findPredecessor(TreeNode * root) { - return NULL; +TreeNode * findPredecessor(TreeNode * root,TreeNode* Root) { + if(root == NULL) + return root; + if(root->left) + return search(root->left,maxValue(root->left)); + TreeNode * t = searchParent(Root,root->val); + while( t!=NULL and root == t->left){ + root = t; + t = searchParent(Root,root->val); + } + return t; } // First generate random array. Then build this tree by calling insert() on each element of array. TreeNode * buildTree(vector & v) { // Call insert() repeatedly to build the tree. - TreeNode * root = new TreeNode(); - return root; + TreeNode* root = NULL; + for(auto it:v) + { + root = insert(root,it); + } + return root; } +void LevelOrderBFS(TreeNode* root){ + if(root == NULL) + return ; + queue q; + q.push(root); + q.push(NULL); + while(!q.empty()){ + TreeNode* temp = q.front(); + q.pop(); + if(temp == NULL) + { + cout<<"\n"; + if(!q.empty()) + q.push(NULL); + } + else{ + cout<val<<" "; + if(temp->left) + q.push(temp->left); + if(temp->right) + q.push(temp->right); + } + + } + return; +} int main() { @@ -80,20 +216,51 @@ int main() { // https://www.youtube.com/watch?v=K6nw5TvhX2s&list=PLrYpW0KwQ3sMemsZOzhawxMpFet3Hb3SN&index=39 // 1. Generate a random array. - // 2. Create an empty tree and insert one value from the array at a time. - // 3. Inorder traversal of this binary search tree should be equal to the sorted value of random array. + vector arr = generateRandomArray(10); + for(auto it:arr) + cout<val<<" is "<val<<" \n"; + temp = succ; + succ = findSuccessor(temp,root); + } + // of the value just obtained. Print again and so on. // 7. This list should be equal to the sorted value of the array. // 8. Find the maximum value in the array. + int mx = maxValue(root); + cout<<"Max value in tree : "<val<<" is "<val<<" \n"; + temp = psucc; + psucc = findPredecessor(temp,root); + } } diff --git a/drivercode/graphdriver b/drivercode/graphdriver new file mode 100755 index 0000000..6afcdc0 Binary files /dev/null and b/drivercode/graphdriver differ diff --git a/drivercode/graphdriver.cpp b/drivercode/graphdriver.cpp index 76edb5d..f5cfe6c 100644 --- a/drivercode/graphdriver.cpp +++ b/drivercode/graphdriver.cpp @@ -1,57 +1,230 @@ #include +#include #include #include +#include +#include +#include using namespace std; class Graph { public: + bool bi = false; + int N; + int E; - int N; vector> adj; // adjacency matrix. + vector> edges; // weighted adjacency matrix - Graph(int nodes) { + Graph(int nodes,bool dir,int m) { N = nodes; - adj.resize(N, vector ()); + adj.resize(N, vector ()); + bi = dir; + E = m; } // Directed graph. Make sure nodes are zero indexed. void addEdge(int u, int v) { - + adj[u].push_back(v); + if(bi) + adj[v].push_back(u); + } + + // added wieghted edge + void addEdgeW(int u, int v, int w){ + edges.push_back({u,v,w}); } // Write BFS Code. void bfs(int start) { + queue q; + q.push(start); + vector visited(N,false); + while(!q.empty()){ + + int v = q.front(); + visited[v] = true; + q.pop(); + for(auto nbr : adj[v]){ + if(visited[nbr] == true) + continue; + cout< parent (N,-1); + parent[start] = start; + vector vis(N,false); + queue q; + q.push(start); + while(!q.empty()){ + int u = q.front(); + q.pop(); + vis[u] = true; + for(int v:adj[u]){ + if(!vis[v]){ + parent[v] = u; + q.push(v); + } + + } + } + + while(parent[end] != end){ + cout< "; + end = parent[end]; + } + cout< getShortestDistanceToAllNodes(int start) { + vector dis(N,-1); + dis[start] = 0; + queue q; + q.push(start); + vector vis(N); + while(!q.empty()){ + int u = q.front(); + q.pop(); + vis[u] = true; + for(int nbr: adj[u]){ + if(vis[nbr]) continue; + if(dis[nbr]==-1) dis[nbr] = dis[u]+1; + q.push(nbr); + } + } + return dis; + } // Why dfsForest? Because the graph may not be connected. void dfsForest(int start) { + vector vis(N); + for(int i=0;i &vis) { + if(vis[start]){ + return; + } + vis[start] = true; + cout< &vis){ + vis[start] = true; + for(int nbr : adj[start]){ + if(!vis[nbr]){ + bool t = dfsCyclic(nbr, start, vis); + if(t) + return t; + } + else if(nbr!=parent){ + return true; + } + } + + return false; + } bool isCyclic() { + vector vis(N); + for(int i=0;i &vis, vector &topoStack){ + vis[start] = true; + for(auto nbr: adj[start]){ + if(!vis[nbr]) topoSort(nbr,vis,topoStack); + } + topoStack.push_back(start); + return ; + } vector topologicalSort() { + vector vis(N); + vector topoStack; + for(int i=0;i bellmanFord(int start) { + vector d(N,INT_MAX); + d[start] = 0; + vector parent(N,-1); + parent[start] = start; + for(int i=0;i "; + end = parent[end]; + } + cout<>n; + int m; cin>>m; + // cout<>u>>v; + adj.addEdge(u,v); + } + + adj.bfs(0); + int source = 0; + cout<<"Distance of all nodes from "< d = adj.getShortestDistanceToAllNodes(source); + for(int i=0;i>n>>m; + Graph graphW = Graph(n,false,m); + for(int i=0;i>u>>v>>w; + graphW.addEdgeW(u,v,w); + } + + vectorwd = graphW.bellmanFord(0); + for(int i=0;inext; + } + cout< generateRandomArray(int size ){ + vector arr(size); + for(int i=1;i<=size;i++){ + arr[i-1] = rand() % 117; + } + sort(arr.begin(),arr.end()); + return arr; +} + + +// Convert array to linkedlist +Node * convertArrayToLinkedList(vector & v) { + + Node* head =new Node(); + Node* temp = head; + for(auto it:v){ + Node * temp = new Node(it); + head->next = temp; + head = head->next; + } + return temp->next; +} + + + +// Insert into a linked list at given position. Position = 1 means insert before head. +Node * insertNode (Node * head, int val, int position) { + if(position == 1 ){ + Node* temp = new Node(val); + temp->next = head; + head = temp; + return head; + } + int cnt = 1; + Node * temp = head; + while(cnt!=position-1){ + temp = temp->next; + cnt++; + + } + Node* temp2 = new Node(val); + temp2->next = temp->next; + temp->next = temp2; + + + + return head; +} + + +// Delete from a linked list. +Node *deleteNode (Node * head, int position) { + if(position == 1){ + return head->next; + } + if(head->next == NULL){ + Node *temp = new Node(); + return temp; + } + int cnt = 1; + Node * temp = head; + while(cnt != position-1){ + temp = temp->next; + cnt++; + } + temp->next = temp->next->next; + + + return head; +} + + +// Compare linkedlists. +bool areEqual(Node *head1, Node * head2) { + while(head1 != NULL and head2 !=NULL){ + if(head1->val != head2->val) + return false; + head1 = head1->next; + head2 = head2->next; + } + if(head1 != NULL or head2 !=NULL) + return false; + return true; +} + +// K = 1 means delete last node. +Node *deleteKthNodeFromEnd(Node * head, int k) { + Node * temp1 = head; + Node* temp2 = head; + + while(k-- and temp1!=NULL){ + temp1 = temp1->next; + } + if(temp1 == NULL) + return head->next; + while(temp1->next != NULL){ + temp2 = temp2->next; + temp1 = temp1->next; + } + temp2->next = temp2->next->next; + + return head; +} + + + +// Reverse a linkedlist. +Node * reverseList(Node *head) { + Node * next = head; + Node* curr = head; + Node * prev = NULL; + while(curr != NULL){ + next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + } + return prev; +} + +// Merge sorted lists. +Node * mergeSortedLists(Node * head1, Node * head2) { + Node *h1 = head1; + if(head1->val < head2->val){ + head1 = head1->next; + } + else{ + h1 = head2; + head2 = head2->next; + } + Node* temp = h1; + while(head1 != NULL and head2 != NULL){ + if(head1->val < head2->val){ + + h1->next = head1; + h1 = h1->next; + head1 = head1->next; + } + else{ + h1->next = head2; + h1 = h1->next; + head2 = head2->next; + } + } + while(head1 != NULL){ + h1->next = head1; + head1 = head1->next; + h1 = h1->next; + } + while(head2 != NULL){ + h1->next = head1; + head2 = head2->next; + h1 = h1->next; + } + +return temp; +} + +int main() { + // 1. Get a random array. + // 2. Convert array to LinkedList List1. + // 3. Insert a number at position P into the list. + // 4. Delete this number from position P. + // 5. Create another linkedlist (List2) from the same random array. + // 6. Compare that List1 is the same as List2. + + int size = 4; + vector arr = generateRandomArray(size); + vector arr2 = generateRandomArray(size+2); + Node *head = convertArrayToLinkedList(arr); + Node *head2 = convertArrayToLinkedList(arr2); +// printList(head); +// head = insertNode(head,312,11); +// printList(head); +// head = deleteNode(head,10); + printList(head); + printList(head2); +// printList(head2); +// cout<< areEqual(head,head2)< 3 -> 2 -> 1 -> 0 +Topological Sorting +3 0 2 4 5 1 +Shortest Path from 0 to 0 +0 +Shortest Path from 0 to 1 +1 -> 0 +Shortest Path from 0 to 2 +2 -> 1 -> 0 +Shortest Path from 0 to 3 +3 -> 1 -> 0 +Shortest Path from 0 to 4 +4 -> 2 -> 1 -> 0 +Distance from 0 to 0 = 0 +Distance from 0 to 1 = 2 +Distance from 0 to 2 = 1 +Distance from 0 to 3 = -1 +Distance from 0 to 4 = 4