├── Graph ├── README.md ├── adjacency-matrix │ ├── README.md │ ├── adjacency-matrix-undirected.c │ ├── adjacency-matrix-undirected-weighted.c │ ├── adjacency-matrix-directed.c │ └── adjacency-matrix-directed-weighted.c ├── breadth-first-search │ ├── README.md │ ├── BFS-tree-edges.c │ ├── BFS.c │ ├── BFS-undirected.c │ ├── BFS-connected.c │ └── BFS-shortest-path.c ├── depth-first-search │ ├── README.md │ ├── DFS-rec-undirected-cyclic.c │ ├── DFS-rec-classify-edges-undirected.c │ ├── DFS-rec-undirected.c │ ├── DFS-rec-time.c │ ├── DFS-rec.c │ ├── DFS-rec-cyclic.c │ ├── DFS-rec-classify-edges-directed.c │ ├── DFS-tree-edges.c │ └── DFS.c ├── warshalls-algorithm.c ├── path-matrix-by-powers-adj.c ├── prims-algorithm.c ├── dijkstras-algorithm.C ├── kruskals-algorithm.c └── adjacency-list.c ├── Trees ├── README.md ├── binary-tree.C ├── expression-tree.C ├── binary-serach-tree-non-recursive.c ├── in-threaded-binary-search-tree.C ├── binary-search-tree-recursive.C ├── avl-tree.C └── b-tree.c └── README.md /Graph/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C 2 | 3 | This [“Advanced Data Structures and Algorithms in C”](https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-c-2](https://user-images.githubusercontent.com/98641125/153195975-d359113b-ff13-4aaa-9f06-6f600c0ddc69.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 18 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 19 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /Trees/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C 2 | 3 | This [“Advanced Data Structures and Algorithms in C”](https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-c-2](https://user-images.githubusercontent.com/98641125/153195975-d359113b-ff13-4aaa-9f06-6f600c0ddc69.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 18 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 19 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /Graph/adjacency-matrix/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C 2 | 3 | This [“Advanced Data Structures and Algorithms in C”](https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-c-2](https://user-images.githubusercontent.com/98641125/153195975-d359113b-ff13-4aaa-9f06-6f600c0ddc69.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 18 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 19 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /Graph/breadth-first-search/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C 2 | 3 | This [“Advanced Data Structures and Algorithms in C”](https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-c-2](https://user-images.githubusercontent.com/98641125/153195975-d359113b-ff13-4aaa-9f06-6f600c0ddc69.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 18 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 19 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /Graph/depth-first-search/README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C 2 | 3 | This [“Advanced Data Structures and Algorithms in C”](https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Thoroughly detailed course with complete working programs 7 | * Contains lots of animations to help you visualize the concepts 8 | * Includes AVL tree, BTree, Graph Algorithms 9 | * Builds a solid foundation in Data Structures and Algorithms 10 | * Prepares you for coding interviews 11 | * Lifetime Access 12 | 13 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 14 | 15 | [![data-structures-and-algorithms-in-c](https://user-images.githubusercontent.com/98641125/153195841-209d2615-ed5f-4007-ae54-539ac3c1538a.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c/?couponCode=GITHUBSTUDENT) 16 | [![data-structures- and-algorithms-in-c-2](https://user-images.githubusercontent.com/98641125/153195975-d359113b-ff13-4aaa-9f06-6f600c0ddc69.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-2/?couponCode=GITHUBSTUDENT) 17 | [![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT) 18 | [![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT) 19 | [![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT) 20 | [![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT) 21 | [![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT) 22 | [![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT) 23 | 24 | ## Copyright 25 | © Copyright Deepali Srivastava : All rights reserved. 26 | Not to be used for commercial purposes. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advanced Data Structures and Algorithms in C ( DSA ) 2 | 3 | This [Data Structures and Algorithms in C (DSA)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course. 4 | 5 | ### About the Course 6 | * Instructor - Deepali Srivastava, Author of [Ultimate Python Programming](https://www.amazon.in/Ultimate-Python-Programming-programs-questions/dp/935551655X) 7 | * Thoroughly detailed course with complete working programs 8 | * Contains lots of animations to help you visualize the concepts 9 | * Includes AVL tree, BTree, Graph Algorithms 10 | * Builds a solid foundation in Data Structures and Algorithms 11 | * Prepares you for coding interviews 12 | * Lifetime Access 13 | 14 | ### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/) 15 | 16 | [![data-structures-algorithms-c](https://user-images.githubusercontent.com/96913690/200234592-25d33957-0e9e-4cc0-b324-2a73325aca85.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-c-masterclass?coupon=GITHUB50) 17 | [![data-structures-algorithms-python](https://user-images.githubusercontent.com/96913690/200234827-86aec10a-bfab-4371-91fc-e2be855ff1ff.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-python-masterclass?coupon=GITHUB50) 18 | [![data-structures-algorithms-java](https://user-images.githubusercontent.com/96913690/200234744-14a5ed97-085f-44f3-9298-979c2053c580.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-java-masterclass?coupon=GITHUB50) 19 | [![data-structures-algorithms-csharp](https://user-images.githubusercontent.com/96913690/200234905-67b85dfd-20c4-4f4b-afd2-e10d3568fff8.jpg)](https://coursegalaxy.newzenler.com/courses/data-structures-algorithms-csharp-masterclass?coupon=GITHUB50) 20 | 21 | 29 | 30 | ## Copyright 31 | © Copyright Deepali Srivastava : All rights reserved. 32 | Not to be used for commercial purposes. 33 | -------------------------------------------------------------------------------- /Graph/warshalls-algorithm.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | 5 | */ 6 | 7 | /* Path matrix by Warshall's algorithm */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #define MAX 30 14 | 15 | struct Vertex 16 | { 17 | char name[50]; 18 | }; 19 | 20 | struct Vertex vertexList[MAX]; 21 | 22 | int adj[MAX][MAX]; /* Adjacency Matrix */ 23 | int n=0; /* Number of vertices in the graph */ 24 | int e=0; /* Number of edges in the graph */ 25 | 26 | int getIndex(char s[]); 27 | void insertVertex(char s[]); 28 | void deleteVertex(char s[]); 29 | void insertEdge(char s1[], char s2[]); 30 | void deleteEdge(char s1[], char s2[] ); 31 | void warshalls(); 32 | 33 | main() 34 | { 35 | insertVertex("Zero"); 36 | insertVertex("One"); 37 | insertVertex("Two"); 38 | insertVertex("Three"); 39 | 40 | insertEdge("Zero", "One"); 41 | insertEdge("Zero", "Three"); 42 | insertEdge("One", "Two"); 43 | insertEdge("Two", "One"); 44 | insertEdge("Three", "Zero"); 45 | insertEdge("Three", "Two"); 46 | 47 | warshalls(); 48 | } 49 | 50 | void warshalls() 51 | { 52 | int i,j,k,p[MAX][MAX]; 53 | 54 | for(i=0; i 8 | #include 9 | 10 | struct node 11 | { 12 | struct node *lchild; 13 | char info; 14 | struct node *rchild; 15 | }; 16 | 17 | struct node *getnode(char x); 18 | void display(struct node *p,int level); 19 | void preorder(struct node *p); 20 | void inorder(struct node *p); 21 | void postorder(struct node *p); 22 | void levelOrder(struct node *root); 23 | int height(struct node *p); 24 | 25 | #define MAX 100 26 | struct node *queue[MAX]; 27 | int front=-1,rear=-1; 28 | void insertQueue(struct node *item); 29 | struct node *deleteQueue(); 30 | int queueEmpty(); 31 | 32 | main() 33 | { 34 | struct node *root=NULL; 35 | 36 | root = getnode('P'); 37 | 38 | root->lchild = getnode('Q'); 39 | root->rchild = getnode('R'); 40 | root->lchild->lchild = getnode('A'); 41 | root->lchild->rchild = getnode('B'); 42 | root->rchild->lchild = getnode('X'); 43 | 44 | display(root,0); 45 | printf("\n\n"); 46 | 47 | printf("Preorder : "); 48 | preorder(root); 49 | printf("\n\n"); 50 | 51 | printf("Inorder : "); 52 | inorder(root); 53 | printf("\n\n"); 54 | 55 | printf("Postorder : "); 56 | postorder(root); 57 | printf("\n\n"); 58 | 59 | printf("Level order : "); 60 | levelOrder(root); 61 | printf("\n\n"); 62 | 63 | printf("Height of tree is %d\n", height(root)); 64 | 65 | getchar(); 66 | 67 | } 68 | 69 | struct node *getnode(char x) 70 | { 71 | struct node *p = (struct node *) malloc(sizeof(struct node)); 72 | p->info=x; 73 | p->lchild=p->rchild=NULL; 74 | return p; 75 | } 76 | 77 | void display(struct node *p,int level) 78 | { 79 | int i; 80 | if(p==NULL) 81 | return; 82 | 83 | display(p->rchild, level+1); 84 | printf("\n"); 85 | 86 | for(i=0; iinfo); 89 | 90 | display(p->lchild, level+1); 91 | }/*End of display()*/ 92 | 93 | void preorder(struct node *p) 94 | { 95 | if(p==NULL) 96 | return; 97 | printf("%c ",p->info); 98 | preorder(p->lchild); 99 | preorder(p->rchild); 100 | }/*End of preorder()*/ 101 | 102 | void inorder(struct node *p) 103 | { 104 | if(p==NULL ) 105 | return; 106 | inorder(p->lchild); 107 | printf("%c ",p->info); 108 | inorder(p->rchild); 109 | }/*End of inorder()*/ 110 | 111 | void postorder(struct node *p) 112 | { 113 | if(p==NULL ) 114 | return; 115 | postorder(p->lchild); 116 | postorder(p->rchild); 117 | printf("%c ",p->info); 118 | 119 | }/*End of postorder()*/ 120 | 121 | int height(struct node *p) 122 | { 123 | int hL,hR; 124 | 125 | if(p==NULL) 126 | return 0; 127 | 128 | hL=height(p->lchild); 129 | hR=height(p->rchild); 130 | 131 | if(hL > hR) 132 | return 1+hL; 133 | else 134 | return 1+hR; 135 | }/*End of height()*/ 136 | 137 | void levelOrder(struct node *p) 138 | { 139 | if(p==NULL) 140 | { 141 | printf("Tree is empty\n"); 142 | return; 143 | } 144 | 145 | insertQueue(p); 146 | while(!queueEmpty()) 147 | { 148 | p=deleteQueue(); 149 | printf("%c ",p->info); 150 | if(p->lchild!=NULL) 151 | insertQueue(p->lchild); 152 | if(p->rchild!=NULL) 153 | insertQueue(p->rchild); 154 | } 155 | } 156 | 157 | /*Functions for implementation of queue*/ 158 | void insertQueue(struct node *item) 159 | { 160 | if(rear==MAX-1) 161 | { 162 | printf("Queue Overflow\n"); 163 | return; 164 | } 165 | if(front==-1) /*If queue is initially empty*/ 166 | front=0; 167 | rear=rear+1; 168 | queue[rear]=item ; 169 | } 170 | 171 | struct node *deleteQueue() 172 | { 173 | struct node *item; 174 | if(front==-1 || front==rear+1) 175 | { 176 | printf("Queue Underflow\n"); 177 | return 0; 178 | } 179 | item=queue[front]; 180 | front=front+1; 181 | return item; 182 | }/*End of deleteQueue()*/ 183 | 184 | int queueEmpty() 185 | { 186 | if(front==-1 || front==rear+1) 187 | return 1; 188 | else 189 | return 0; 190 | } 191 | -------------------------------------------------------------------------------- /Graph/path-matrix-by-powers-adj.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | /* Path matrix */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define MAX 30 13 | 14 | struct Vertex 15 | { 16 | char name[50]; 17 | }; 18 | 19 | struct Vertex vertexList[MAX]; 20 | 21 | int adj[MAX][MAX]; /* Adjacency Matrix */ 22 | int n=0; /* Number of vertices in the graph */ 23 | int e=0; /* Number of edges in the graph */ 24 | 25 | int getIndex(char s[]); 26 | void insertVertex(char s[]); 27 | void deleteVertex(char s[]); 28 | void insertEdge(char s1[], char s2[]); 29 | void deleteEdge(char s1[], char s2[] ); 30 | void findPathMatrix(); 31 | 32 | main() 33 | { 34 | insertVertex("Zero"); 35 | insertVertex("One"); 36 | insertVertex("Two"); 37 | insertVertex("Three"); 38 | 39 | insertEdge("Zero","One"); 40 | insertEdge("Zero","Three"); 41 | insertEdge("One","Two"); 42 | insertEdge("One","Three"); 43 | insertEdge("Three","Two"); 44 | 45 | findPathMatrix(); 46 | } 47 | 48 | void findPathMatrix() 49 | { 50 | int x[MAX][MAX]; 51 | int adjp[MAX][MAX]; 52 | int temp[MAX][MAX]; 53 | 54 | int i,j,p,k; 55 | for(i = 0; i 7 | #include 8 | #include 9 | 10 | struct node 11 | { 12 | struct node *lchild; 13 | char info; 14 | struct node *rchild; 15 | }; 16 | 17 | int isOperator(char c); 18 | struct node *getNode(int x); 19 | struct node *buildTree(char postfix[],struct node *root); 20 | void prefix(struct node *root); 21 | void preorder(struct node *p); 22 | void postfix(struct node *root); 23 | void postorder(struct node *p); 24 | void parenthesizedInfix(struct node *root); 25 | void inorder(struct node *p); 26 | void display(struct node *root, int level); 27 | int value(struct node *root); 28 | int evaluate(struct node *p); 29 | 30 | #define MAX 50 31 | struct node* stack[MAX]; 32 | int top; 33 | 34 | void initializeStack(); 35 | int isEmpty(); 36 | int isFull(); 37 | void push(struct node* x); 38 | struct node* pop(); 39 | 40 | main() 41 | { 42 | struct node *root=NULL; 43 | 44 | char postfixExpr[] = "54+12*3*-"; 45 | 46 | root = buildTree(postfixExpr, root); 47 | display(root,0); 48 | printf("\n"); 49 | 50 | printf("Prefix : "); 51 | prefix(root); 52 | 53 | printf("Postfix : "); 54 | postfix(root); 55 | 56 | printf("Infix : "); 57 | parenthesizedInfix(root); 58 | 59 | printf("Value : %d\n",value(root) ); 60 | } 61 | 62 | int isOperator(char c) 63 | { 64 | if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') 65 | return 1; 66 | return 0; 67 | } 68 | 69 | struct node *getNode(int x) 70 | { 71 | struct node *p=(struct node *) malloc(sizeof(struct node)); 72 | p->info=x; 73 | p->lchild=NULL; 74 | p->rchild=NULL; 75 | return p; 76 | } 77 | 78 | struct node *buildTree(char postfix[], struct node *root) 79 | { 80 | struct node *t; 81 | int i; 82 | 83 | initializeStack(); 84 | 85 | for(i = 0; i < strlen(postfix); i++) 86 | { 87 | t = getNode(postfix[i]); 88 | if(isOperator(postfix[i])) 89 | { 90 | t->rchild = pop(); 91 | t->lchild = pop(); 92 | } 93 | push(t); 94 | } 95 | root = pop(); 96 | return root; 97 | } 98 | 99 | void prefix(struct node *root) 100 | { 101 | preorder(root); 102 | printf("\n"); 103 | } 104 | 105 | void preorder(struct node *p) 106 | { 107 | if (p == NULL) 108 | return; 109 | printf("%c",p->info); 110 | preorder(p->lchild); 111 | preorder(p->rchild); 112 | } 113 | 114 | void postfix(struct node *root) 115 | { 116 | postorder(root); 117 | printf("\n"); 118 | } 119 | 120 | void postorder(struct node *p) 121 | { 122 | if (p == NULL) 123 | return; 124 | postorder(p->lchild); 125 | postorder(p->rchild); 126 | printf("%c",p->info); 127 | } 128 | 129 | void parenthesizedInfix(struct node *root) 130 | { 131 | inorder(root); 132 | printf("\n"); 133 | } 134 | 135 | void inorder(struct node *p) 136 | { 137 | if (p == NULL) 138 | return; 139 | 140 | if (isOperator(p->info)) 141 | printf("("); 142 | 143 | inorder(p->lchild); 144 | printf("%c",p->info); 145 | inorder(p->rchild); 146 | 147 | if (isOperator(p->info)) 148 | printf(")"); 149 | } 150 | 151 | void display(struct node *p, int level) 152 | { 153 | int i; 154 | if (p == NULL) 155 | return; 156 | 157 | display(p->rchild, level + 1); 158 | printf("\n"); 159 | 160 | for (i = 0; i < level; i++) 161 | printf(" "); 162 | printf("%c",p->info); 163 | 164 | display(p->lchild, level + 1); 165 | } 166 | 167 | int value(struct node *root) 168 | { 169 | if( root == NULL ) 170 | return 0; 171 | return evaluate(root); 172 | } 173 | 174 | int evaluate(struct node *p) 175 | { 176 | int leftValue, rightValue; 177 | 178 | if( !isOperator(p->info) ) 179 | return p->info - 48; 180 | 181 | leftValue = evaluate(p->lchild); 182 | rightValue = evaluate(p->rchild); 183 | 184 | if(p->info=='+') 185 | return leftValue + rightValue; 186 | else if (p->info=='-') 187 | return leftValue - rightValue; 188 | else if (p->info=='*') 189 | return leftValue * rightValue; 190 | else 191 | return leftValue / rightValue; 192 | } 193 | 194 | /* Function definitions for implementation of stack */ 195 | 196 | void initializeStack() 197 | { 198 | top=-1; 199 | } 200 | 201 | int isEmpty() 202 | { 203 | if(top==-1) 204 | return 1; 205 | else 206 | return 0; 207 | } 208 | 209 | int isFull() 210 | { 211 | if(top == MAX-1) 212 | return 1; 213 | else 214 | return 0; 215 | } 216 | 217 | void push(struct node* x) 218 | { 219 | if(isFull()) 220 | { 221 | printf("Stack Overflow\n"); 222 | return; 223 | } 224 | top=top+1; 225 | stack[top]=x; 226 | } 227 | 228 | struct node* pop() 229 | { 230 | struct node* x; 231 | if(isEmpty()) 232 | { 233 | printf("Stack Underflow\n"); 234 | exit(1); 235 | } 236 | x=stack[top]; 237 | top=top-1; 238 | return x; 239 | } 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | -------------------------------------------------------------------------------- /Trees/binary-serach-tree-non-recursive.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | struct node 10 | { 11 | struct node *lchild; 12 | int info; 13 | struct node *rchild; 14 | }; 15 | 16 | struct node *getnode(int x); 17 | void display(struct node *p,int level); 18 | struct node *search(struct node *p, int x); 19 | struct node *insertNode(struct node *root, int x); 20 | struct node *deleteNode(struct node *root, int x); 21 | struct node *Min(struct node *p); 22 | struct node *Max(struct node *p); 23 | 24 | main() 25 | { 26 | struct node *root=NULL, *p; 27 | int choice,x; 28 | 29 | while(1) 30 | { 31 | printf("\n"); 32 | printf("1.Display Tree\n"); 33 | printf("2.Search\n"); 34 | printf("3.Insert a new node\n"); 35 | printf("4.Delete a node\n"); 36 | printf("5.Find minimum\n"); 37 | printf("6.Find maximum\n"); 38 | printf("7.Quit\n"); 39 | printf("Enter your choice : "); 40 | scanf("%d",&choice); 41 | 42 | if(choice==7) 43 | break; 44 | 45 | switch(choice) 46 | { 47 | case 1: 48 | display(root,0); 49 | break; 50 | case 2: 51 | printf("Enter the key to be searched : "); 52 | scanf("%d",&x); 53 | p=search(root,x); 54 | if(p==NULL) 55 | printf("Key not found\n"); 56 | else 57 | printf("Key found\n"); 58 | break; 59 | case 3: 60 | printf("Enter the key to be inserted : "); 61 | scanf("%d",&x); 62 | root=insertNode(root,x); 63 | break; 64 | case 4: 65 | printf("Enter the key to be deleted : "); 66 | scanf("%d",&x); 67 | root=deleteNode(root,x); 68 | break; 69 | case 5: 70 | p=Min(root); 71 | if(p==NULL) 72 | printf("Tree is empty\n"); 73 | else 74 | printf("Minimum key is %d\n", p->info); 75 | break; 76 | case 6: 77 | p=Max(root); 78 | if(p==NULL) 79 | printf("Tree is empty\n"); 80 | else 81 | printf("Maximum key is %d\n", p->info); 82 | break; 83 | }/*End of switch*/ 84 | }/*End of while */ 85 | }/*End of main()*/ 86 | 87 | struct node *getnode(int x) 88 | { 89 | struct node *p=(struct node *) malloc(sizeof(struct node)); 90 | p->info=x; 91 | p->lchild=NULL; 92 | p->rchild=NULL; 93 | return p; 94 | } 95 | 96 | struct node *insertNode(struct node *root, int x) 97 | { 98 | struct node *temp,*par,*p; 99 | 100 | p=root; 101 | par=NULL; 102 | 103 | while(p!=NULL) 104 | { 105 | par=p; 106 | if(x < p->info) 107 | p=p->lchild; 108 | else if(x > p->info) 109 | p=p->rchild; 110 | else 111 | { 112 | printf("%d already present in the tree\n",x); 113 | return root; 114 | } 115 | } 116 | 117 | temp=getnode(x); 118 | 119 | if(par==NULL) 120 | root=temp; 121 | else if(x < par->info) 122 | par->lchild=temp; 123 | else 124 | par->rchild=temp; 125 | 126 | return root; 127 | }/*End of insertNode()*/ 128 | 129 | struct node *search(struct node *p, int x) 130 | { 131 | while(p!=NULL) 132 | { 133 | if(x < p->info) 134 | p=p->lchild; /*Move to left child*/ 135 | else if(x > p->info) 136 | p=p->rchild; /*Move to right child */ 137 | else /*x found*/ 138 | return p; 139 | } 140 | return NULL; 141 | }/*End of search()*/ 142 | 143 | struct node *deleteNode(struct node *root, int x) 144 | { 145 | struct node *par,*p,*ch,*s,*ps; 146 | 147 | p=root; 148 | par=NULL; 149 | while(p!=NULL) 150 | { 151 | if(x==p->info) 152 | break; 153 | par=p; 154 | if(x < p->info) 155 | p=p->lchild; 156 | else 157 | p=p->rchild; 158 | } 159 | 160 | if(p==NULL) 161 | { 162 | printf("%d not found\n",x); 163 | return root; 164 | } 165 | 166 | /*Case C: 2 children*/ 167 | /*Find inorder successor and its parent*/ 168 | if(p->lchild!=NULL && p->rchild!=NULL) 169 | { 170 | ps=p; 171 | s=p->rchild; 172 | while(s->lchild!=NULL) 173 | { 174 | ps=s; 175 | s=s->lchild; 176 | } 177 | p->info=s->info; 178 | p=s; 179 | par=ps; 180 | } 181 | 182 | /*Case B and Case A : 1 or no child*/ 183 | if(p->lchild!=NULL) /*node to be deleted has left child */ 184 | ch=p->lchild; 185 | else /*node to be deleted has right child or no child*/ 186 | ch=p->rchild; 187 | 188 | if(par==NULL) /*node to be deleted is root node*/ 189 | root=ch; 190 | else if(p==par->lchild)/*node is left child of its parent*/ 191 | par->lchild=ch; 192 | else /*node is right child of its parent*/ 193 | par->rchild=ch; 194 | free(p); 195 | return root; 196 | } 197 | 198 | struct node *Min(struct node *p) 199 | { 200 | if(p==NULL) 201 | return NULL; 202 | while(p->lchild!=NULL) 203 | p=p->lchild; 204 | return p; 205 | }/*End of Min()*/ 206 | 207 | struct node *Max(struct node *p) 208 | { 209 | if(p==NULL) 210 | return NULL; 211 | while(p->rchild!=NULL) 212 | p=p->rchild; 213 | return p; 214 | }/*End of Max()*/ 215 | 216 | void display(struct node *p,int level) 217 | { 218 | int i; 219 | if(p==NULL) 220 | return; 221 | 222 | display(p->rchild, level+1); 223 | printf("\n"); 224 | 225 | for(i=0; iinfo); 228 | 229 | display(p->lchild, level+1); 230 | }/*End of display()*/ -------------------------------------------------------------------------------- /Graph/depth-first-search/DFS-rec-undirected-cyclic.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #define INITIAL 0 11 | #define VISITED 1 12 | #define FINISHED 2 13 | 14 | #define MAX 30 15 | 16 | struct Vertex 17 | { 18 | char name[50]; 19 | int state; 20 | int predecessor; 21 | }; 22 | 23 | struct Vertex vertexList[MAX]; 24 | 25 | int adj[MAX][MAX]; /* Adjacency Matrix */ 26 | int n=0; /* Number of vertices in the graph */ 27 | int e=0; /* Number of edges in the graph */ 28 | 29 | void display(); 30 | int getIndex(char s[]); 31 | void insertVertex(char s[]); 32 | void deleteVertex(char s[]); 33 | void insertEdge(char s1[], char s2[]); 34 | void deleteEdge(char s1[], char s2[] ); 35 | 36 | int hasCycle; 37 | 38 | void dfs(int v); 39 | int isCyclic(); 40 | 41 | 42 | main() 43 | { 44 | insertVertex("Zero"); 45 | insertVertex("One"); 46 | insertVertex("Two"); 47 | insertVertex("Three"); 48 | insertVertex("Four"); 49 | insertVertex("Five"); 50 | insertVertex("Six"); 51 | insertVertex("Seven"); 52 | insertVertex("Eight"); 53 | insertVertex("Nine"); 54 | insertVertex("Ten"); 55 | insertVertex("Eleven"); 56 | insertVertex("Twelve"); 57 | insertVertex("Thirteen"); 58 | insertVertex("Fourteen"); 59 | 60 | insertEdge("Zero","One"); 61 | insertEdge("Zero","Three"); 62 | insertEdge("One","Two"); 63 | insertEdge("One","Three"); 64 | insertEdge("One","Four"); 65 | insertEdge("Three","Four"); 66 | insertEdge("Five","Six"); 67 | insertEdge("Five","Seven"); 68 | insertEdge("Five","Eight"); 69 | insertEdge("Seven","Eight"); 70 | insertEdge("Nine","Ten"); 71 | insertEdge("Nine","Eleven"); 72 | insertEdge("Nine","Twelve"); 73 | insertEdge("Nine","Thirteen"); 74 | insertEdge("Ten","Twelve"); 75 | insertEdge("Eleven","Thirteen"); 76 | insertEdge("Eleven","Fourteen"); 77 | 78 | if( isCyclic() ) 79 | printf("Graph is Cyclic\n"); 80 | else 81 | printf("Graph is Acylic\n"); 82 | } 83 | 84 | int isCyclic() 85 | { 86 | int v; 87 | for ( v = 0; v < n; v++ ) 88 | vertexList[v].state = INITIAL; 89 | 90 | hasCycle = 0; 91 | 92 | for ( v = 0; v < n; v++) 93 | if ( vertexList[v].state == INITIAL ) 94 | dfs(v); 95 | return hasCycle; 96 | } 97 | 98 | void dfs(int v) 99 | { 100 | int i; 101 | vertexList[v].state = VISITED; 102 | 103 | for(i = 0; i < n; i++) 104 | { 105 | if(adj[v][i] != 0 && vertexList[v].predecessor != i ) 106 | { 107 | if(vertexList[i].state == INITIAL) 108 | { 109 | vertexList[i].predecessor = v; 110 | dfs(i); 111 | } 112 | else if(vertexList[i].state == VISITED) 113 | hasCycle = 1; 114 | } 115 | } 116 | vertexList[v].state = FINISHED; 117 | } 118 | 119 | 120 | void display() 121 | { 122 | int i,j; 123 | 124 | printf("Adjacency Matrix - \n"); 125 | for(i=0; i 8 | #include 9 | #include 10 | 11 | #define INITIAL 0 12 | #define VISITED 1 13 | #define FINISHED 2 14 | 15 | #define MAX 30 16 | 17 | struct Vertex 18 | { 19 | char name[50]; 20 | int state; 21 | int predecessor; 22 | }; 23 | 24 | struct Vertex vertexList[MAX]; 25 | 26 | int adj[MAX][MAX]; /* Adjacency Matrix */ 27 | int n=0; /* Number of vertices in the graph */ 28 | int e=0; /* Number of edges in the graph */ 29 | 30 | void display(); 31 | int getIndex(char s[]); 32 | void insertVertex(char s[]); 33 | void deleteVertex(char s[]); 34 | void insertEdge(char s1[], char s2[]); 35 | void deleteEdge(char s1[], char s2[] ); 36 | 37 | void dfs(int v); 38 | void dfsTraversalAll(); 39 | 40 | main() 41 | { 42 | insertVertex("Zero"); 43 | insertVertex("One"); 44 | insertVertex("Two"); 45 | insertVertex("Three"); 46 | insertVertex("Four"); 47 | insertVertex("Five"); 48 | insertVertex("Six"); 49 | insertVertex("Seven"); 50 | insertVertex("Eight"); 51 | insertVertex("Nine"); 52 | insertVertex("Ten"); 53 | insertVertex("Eleven"); 54 | insertVertex("Twelve"); 55 | insertVertex("Thirteen"); 56 | insertVertex("Fourteen"); 57 | 58 | insertEdge("Zero","One"); 59 | insertEdge("Zero","Three"); 60 | insertEdge("One","Two"); 61 | insertEdge("One","Three"); 62 | insertEdge("One","Four"); 63 | insertEdge("Three","Four"); 64 | insertEdge("Five","Six"); 65 | insertEdge("Five","Seven"); 66 | insertEdge("Five","Eight"); 67 | insertEdge("Seven","Eight"); 68 | insertEdge("Nine","Ten"); 69 | insertEdge("Nine","Eleven"); 70 | insertEdge("Nine","Twelve"); 71 | insertEdge("Nine","Thirteen"); 72 | insertEdge("Ten","Twelve"); 73 | insertEdge("Eleven","Thirteen"); 74 | insertEdge("Eleven","Fourteen"); 75 | 76 | dfsTraversalAll(); 77 | } 78 | 79 | 80 | void dfs(int v) 81 | { 82 | int i; 83 | vertexList[v].state = VISITED; 84 | 85 | for(i = 0; i < n; i++) 86 | { 87 | if(adj[v][i] != 0 && vertexList[v].predecessor != i ) 88 | { 89 | if(vertexList[i].state == INITIAL) 90 | { 91 | vertexList[i].predecessor = v; 92 | printf("Tree edge -(%s,%s)\n",vertexList[v].name, vertexList[i].name); 93 | dfs(i); 94 | } 95 | else if(vertexList[i].state == VISITED) 96 | printf("Back edge - (%s,%s)\n", vertexList[v].name, vertexList[i].name); 97 | } 98 | } 99 | vertexList[v].state = FINISHED; 100 | } 101 | 102 | void dfsTraversalAll() 103 | { 104 | int u,v; 105 | char s[50]; 106 | 107 | printf("Enter starting vertex for Depth First Search : "); 108 | scanf("%s", &s); 109 | u = getIndex(s); 110 | if(u == -1) 111 | return; 112 | 113 | for (v = 0; v < n; v++) 114 | vertexList[v].state = INITIAL; 115 | 116 | dfs(u); 117 | 118 | for(v=0; v 7 | #include 8 | #include 9 | 10 | #define INITIAL 0 11 | #define VISITED 1 12 | #define FINISHED 2 13 | 14 | #define MAX 30 15 | 16 | struct Vertex 17 | { 18 | char name[50]; 19 | int state; 20 | }; 21 | 22 | struct Vertex vertexList[MAX]; 23 | 24 | int adj[MAX][MAX]; /* Adjacency Matrix */ 25 | int n=0; /* Number of vertices in the graph */ 26 | int e=0; /* Number of edges in the graph */ 27 | 28 | void display(); 29 | int getIndex(char s[]); 30 | void insertVertex(char s[]); 31 | void deleteVertex(char s[]); 32 | void insertEdge(char s1[], char s2[]); 33 | void deleteEdge(char s1[], char s2[] ); 34 | 35 | void dfs(int v); 36 | void dfsTraversal(); 37 | void dfsTraversalAll(); 38 | 39 | main() 40 | { 41 | insertVertex("Zero"); 42 | insertVertex("One"); 43 | insertVertex("Two"); 44 | insertVertex("Three"); 45 | insertVertex("Four"); 46 | insertVertex("Five"); 47 | insertVertex("Six"); 48 | insertVertex("Seven"); 49 | insertVertex("Eight"); 50 | insertVertex("Nine"); 51 | insertVertex("Ten"); 52 | insertVertex("Eleven"); 53 | insertVertex("Twelve"); 54 | insertVertex("Thirteen"); 55 | insertVertex("Fourteen"); 56 | 57 | insertEdge("Zero","One"); 58 | insertEdge("Zero","Three"); 59 | insertEdge("One","Two"); 60 | insertEdge("One","Three"); 61 | insertEdge("One","Four"); 62 | insertEdge("Three","Four"); 63 | insertEdge("Five","Six"); 64 | insertEdge("Five","Seven"); 65 | insertEdge("Five","Eight"); 66 | insertEdge("Seven","Eight"); 67 | insertEdge("Nine","Ten"); 68 | insertEdge("Nine","Eleven"); 69 | insertEdge("Nine","Twelve"); 70 | insertEdge("Nine","Thirteen"); 71 | insertEdge("Ten","Twelve"); 72 | insertEdge("Eleven","Thirteen"); 73 | insertEdge("Eleven","Fourteen"); 74 | 75 | dfsTraversal(); 76 | dfsTraversalAll(); 77 | } 78 | 79 | void dfsTraversal() 80 | { 81 | int u,v; 82 | char s[50]; 83 | 84 | printf("Enter starting vertex for Depth First Search : "); 85 | scanf("%s", &s); 86 | u = getIndex(s); 87 | if(u == -1) 88 | return; 89 | for (v = 0; v < n; v++) 90 | vertexList[v].state = INITIAL; 91 | 92 | dfs(u); 93 | 94 | printf("\n"); 95 | } 96 | 97 | void dfs(int v) 98 | { 99 | int i; 100 | printf("%s ", vertexList[v].name); 101 | vertexList[v].state = VISITED; 102 | 103 | for(i=0; i 7 | #include 8 | #include 9 | 10 | #define INITIAL 0 11 | #define VISITED 1 12 | #define FINISHED 2 13 | 14 | #define MAX 30 15 | 16 | struct Vertex 17 | { 18 | char name[50]; 19 | int state; 20 | int discoveryTime; 21 | int finishingTime; 22 | }; 23 | 24 | int time; 25 | 26 | struct Vertex vertexList[MAX]; 27 | 28 | int adj[MAX][MAX]; /* Adjacency Matrix */ 29 | int n=0; /* Number of vertices in the graph */ 30 | int e=0; /* Number of edges in the graph */ 31 | 32 | void display(); 33 | int getIndex(char s[]); 34 | int isAdjacent(char s1[], char s2[]); 35 | void insertVertex(char s[]); 36 | void deleteVertex(char s[]); 37 | void insertEdge(char s1[], char s2[]); 38 | void deleteEdge(char s1[], char s2[] ); 39 | 40 | void dfsTraversal(); 41 | void dfsTraversalAll(); 42 | void dfs(int v); 43 | 44 | main() 45 | { 46 | insertVertex("Zero"); 47 | insertVertex("One"); 48 | insertVertex("Two"); 49 | insertVertex("Three"); 50 | insertVertex("Four"); 51 | insertVertex("Five"); 52 | insertVertex("Six"); 53 | insertVertex("Seven"); 54 | insertVertex("Eight"); 55 | insertVertex("Nine"); 56 | insertVertex("Ten"); 57 | insertVertex("Eleven"); 58 | 59 | insertEdge("Zero", "One"); 60 | insertEdge("Zero", "Three"); 61 | insertEdge("One", "Two"); 62 | insertEdge("One", "Four"); 63 | insertEdge("One", "Five"); 64 | insertEdge("Two", "Five"); 65 | insertEdge("Two", "Seven"); 66 | insertEdge("Three", "Six"); 67 | insertEdge("Four", "Three"); 68 | insertEdge("Five", "Three"); 69 | insertEdge("Five", "Six"); 70 | insertEdge("Five", "Eight"); 71 | insertEdge("Seven", "Eight"); 72 | insertEdge("Seven", "Ten"); 73 | insertEdge("Eight", "Eleven"); 74 | insertEdge("Nine", "Six"); 75 | insertEdge("Eleven", "Nine"); 76 | 77 | dfsTraversalAll(); 78 | } 79 | 80 | void dfs(int v) 81 | { 82 | int i; 83 | vertexList[v].state = VISITED; 84 | vertexList[v].discoveryTime = ++time; 85 | 86 | for(i=0; i 9 | #include 10 | #include 11 | 12 | #define INITIAL 0 13 | #define VISITED 1 14 | #define FINISHED 2 15 | 16 | #define MAX 30 17 | 18 | struct Vertex 19 | { 20 | char name[50]; 21 | int state; 22 | }; 23 | 24 | struct Vertex vertexList[MAX]; 25 | 26 | int adj[MAX][MAX]; /* Adjacency Matrix */ 27 | int n=0; /* Number of vertices in the graph */ 28 | int e=0; /* Number of edges in the graph */ 29 | 30 | void display(); 31 | int getIndex(char s[]); 32 | int isAdjacent(char s1[], char s2[]); 33 | void insertVertex(char s[]); 34 | void deleteVertex(char s[]); 35 | void insertEdge(char s1[], char s2[]); 36 | void deleteEdge(char s1[], char s2[] ); 37 | 38 | void dfsTraversal(); 39 | void dfsTraversalAll(); 40 | void dfs(int v); 41 | 42 | main() 43 | { 44 | insertVertex("Zero"); 45 | insertVertex("One"); 46 | insertVertex("Two"); 47 | insertVertex("Three"); 48 | insertVertex("Four"); 49 | insertVertex("Five"); 50 | insertVertex("Six"); 51 | insertVertex("Seven"); 52 | insertVertex("Eight"); 53 | insertVertex("Nine"); 54 | insertVertex("Ten"); 55 | insertVertex("Eleven"); 56 | 57 | insertEdge("Zero", "One"); 58 | insertEdge("Zero", "Three"); 59 | insertEdge("One", "Two"); 60 | insertEdge("One", "Four"); 61 | insertEdge("One", "Five"); 62 | insertEdge("Two", "Five"); 63 | insertEdge("Two", "Seven"); 64 | insertEdge("Three", "Six"); 65 | insertEdge("Four", "Three"); 66 | insertEdge("Five", "Three"); 67 | insertEdge("Five", "Six"); 68 | insertEdge("Five", "Eight"); 69 | insertEdge("Seven", "Eight"); 70 | insertEdge("Seven", "Ten"); 71 | insertEdge("Eight", "Eleven"); 72 | insertEdge("Nine", "Six"); 73 | insertEdge("Eleven", "Nine"); 74 | 75 | dfsTraversal(); 76 | 77 | dfsTraversalAll(); 78 | } 79 | 80 | void dfsTraversal() 81 | { 82 | int u,v; 83 | char s[50]; 84 | 85 | printf("Enter starting vertex for Depth First Search : "); 86 | scanf("%s", &s); 87 | u = getIndex(s); 88 | if(u == -1) 89 | return; 90 | for (v = 0; v < n; v++) 91 | vertexList[v].state = INITIAL; 92 | 93 | dfs(u); 94 | printf("\n"); 95 | } 96 | 97 | void dfs(int v) 98 | { 99 | int i; 100 | printf("%s ", vertexList[v].name); 101 | vertexList[v].state = VISITED; 102 | 103 | for(i=0; i 9 | #include 10 | #include 11 | 12 | #define INITIAL 0 13 | #define VISITED 1 14 | #define FINISHED 2 15 | 16 | #define MAX 30 17 | 18 | struct Vertex 19 | { 20 | char name[50]; 21 | int state; 22 | int discoveryTime; 23 | int finishingTime; 24 | }; 25 | 26 | int hasCycle; 27 | 28 | struct Vertex vertexList[MAX]; 29 | 30 | int adj[MAX][MAX]; /* Adjacency Matrix */ 31 | int n=0; /* Number of vertices in the graph */ 32 | int e=0; /* Number of edges in the graph */ 33 | 34 | void display(); 35 | int getIndex(char s[]); 36 | int isAdjacent(char s1[], char s2[]); 37 | void insertVertex(char s[]); 38 | void deleteVertex(char s[]); 39 | void insertEdge(char s1[], char s2[]); 40 | void deleteEdge(char s1[], char s2[] ); 41 | 42 | int isCyclic(); 43 | void dfs(int v); 44 | 45 | main() 46 | { 47 | insertVertex("Zero"); 48 | insertVertex("One"); 49 | insertVertex("Two"); 50 | insertVertex("Three"); 51 | insertVertex("Four"); 52 | insertVertex("Five"); 53 | insertVertex("Six"); 54 | insertVertex("Seven"); 55 | insertVertex("Eight"); 56 | insertVertex("Nine"); 57 | insertVertex("Ten"); 58 | insertVertex("Eleven"); 59 | insertVertex("Twelve"); 60 | insertVertex("Thirteen"); 61 | insertVertex("Fourteen"); 62 | insertVertex("Fifteen"); 63 | insertVertex("Sixteen"); 64 | 65 | insertEdge("Zero","One"); 66 | insertEdge("Zero","Two"); 67 | insertEdge("Zero","Four"); 68 | insertEdge("One","Three"); 69 | insertEdge("Two","Three"); 70 | insertEdge("Two","Four"); 71 | insertEdge("Two","Five"); 72 | /*insertEdge("Three","Zero");*/ 73 | insertEdge("Four","Five"); 74 | insertEdge("Five","Three"); 75 | insertEdge("Six","One"); 76 | insertEdge("Six","Seven"); 77 | insertEdge("Six","Eight"); 78 | insertEdge("Six","Nine"); 79 | insertEdge("Seven","Nine"); 80 | insertEdge("Eight","Ten"); 81 | insertEdge("Nine","Five"); 82 | /*insertEdge("Ten","Six");*/ 83 | insertEdge("Ten","Nine"); 84 | insertEdge("Eleven","Eight"); 85 | insertEdge("Eleven","Thirteen"); 86 | insertEdge("Eleven","Fifteen"); 87 | /*insertEdge("Twelve","Eleven");*/ 88 | insertEdge("Thirteen","Eight"); 89 | insertEdge("Thirteen","Fourteen"); 90 | insertEdge("Thirteen","Fifteen"); 91 | insertEdge("Thirteen","Sixteen"); 92 | insertEdge("Fourteen","Sixteen"); 93 | insertEdge("Fifteen","Twelve"); 94 | insertEdge("Fifteen","Sixteen"); 95 | 96 | if( isCyclic() ) 97 | printf("Graph is Cyclic\n"); 98 | else 99 | printf("Graph is Acylic\n"); 100 | } 101 | 102 | int isCyclic() 103 | { 104 | int v; 105 | for (v = 0; v < n; v++) 106 | vertexList[v].state = INITIAL; 107 | 108 | hasCycle = 0; 109 | 110 | for (v = 0; v < n; v++) 111 | if (vertexList[v].state == INITIAL) 112 | dfs(v); 113 | return hasCycle; 114 | } 115 | 116 | void dfs(int v) 117 | { 118 | int i; 119 | vertexList[v].state = VISITED; 120 | 121 | for(i=0; i 9 | #include 10 | #include 11 | 12 | #define MAX 30 13 | 14 | struct Vertex 15 | { 16 | char name[50]; 17 | }; 18 | 19 | struct Vertex vertexList[MAX]; 20 | 21 | int adj[MAX][MAX]; /* Adjacency Matrix */ 22 | int n=0; /* Number of vertices in the graph */ 23 | int e=0; /* Number of edges in the graph */ 24 | 25 | void display(); 26 | int getIndex(char s[]); 27 | int isAdjacent(char s1[], char s2[]); 28 | void insertVertex(char s[]); 29 | void deleteVertex(char s[]); 30 | void insertEdge(char s1[], char s2[]); 31 | void deleteEdge(char s1[], char s2[] ); 32 | int degree(char s[]); 33 | 34 | main() 35 | { 36 | int choice,x; 37 | char s1[50],s2[50]; 38 | 39 | insertVertex("AA"); 40 | insertVertex("BB"); 41 | insertVertex("CC"); 42 | insertVertex("DD"); 43 | insertVertex("EE"); 44 | insertEdge("AA","BB"); 45 | insertEdge("AA","CC"); 46 | insertEdge("CC","DD"); 47 | insertEdge("DD","AA"); 48 | insertEdge("BB","EE"); 49 | 50 | 51 | while(1) 52 | { 53 | printf("1.Display Adjacency Matrix\n"); 54 | printf("2.Insert a vertex\n"); 55 | printf("3.Remove a vertex\n"); 56 | printf("4.Insert an edge\n"); 57 | printf("5.Delete an edge\n"); 58 | printf("6.Display degree of a vertex\n"); 59 | printf("7.Check if there is an edge between two vertices\n"); 60 | printf("8.Exit\n"); 61 | printf("Enter your choice : "); 62 | scanf("%d",&choice); 63 | 64 | switch(choice) 65 | { 66 | case 1: 67 | display(); 68 | break; 69 | case 2: 70 | printf("Enter vertex name : "); 71 | scanf("%s",s1); 72 | insertVertex(s1); 73 | break; 74 | case 3: 75 | printf("Enter vertex name : "); 76 | scanf("%s",s1); 77 | deleteVertex(s1); 78 | break; 79 | case 4: 80 | printf("Enter first and second vertices : "); 81 | scanf("%s",s1); 82 | scanf("%s",s2); 83 | insertEdge(s1,s2); 84 | break; 85 | case 5: 86 | printf("Enter first and second vertices : "); 87 | scanf("%s",s1); 88 | scanf("%s",s2); 89 | deleteEdge(s1,s2); 90 | break; 91 | case 6: 92 | printf("Enter vertex name : "); 93 | scanf("%s",s1); 94 | x = degree(s1); 95 | if ( x == -1 ) 96 | printf("Vertex name not present in the graph\n\n"); 97 | else 98 | printf("Degree is : %d\n\n",x ); 99 | break; 100 | case 7: 101 | printf("Enter the name of first vertex : "); 102 | scanf("%s",s1); 103 | printf("Enter the name of second vertex : "); 104 | scanf("%s",s2); 105 | x = isAdjacent(s1,s2); 106 | if( x == -1 ) 107 | printf("Vertex name not present in the graph\n\n"); 108 | else if ( x == 0 ) 109 | printf("There is no edge from %s to %s\n\n",s1,s2); 110 | else 111 | printf("There is an edge from %s to %s\n\n",s1,s2); 112 | break; 113 | case 8: 114 | exit(1); 115 | default: 116 | printf("Wrong choice\n\n"); 117 | break; 118 | } 119 | } 120 | } 121 | 122 | void display() 123 | { 124 | int i,j; 125 | 126 | printf("Adjacency Matrix - \n"); 127 | for(i=0; i 9 | #include 10 | #include 11 | 12 | #define MAX 30 13 | 14 | struct Vertex 15 | { 16 | char name[50]; 17 | }; 18 | struct Vertex vertexList[MAX]; 19 | 20 | int adj[MAX][MAX]; /* Adjacency Matrix */ 21 | int n=0; /* Number of vertices in the graph */ 22 | int e=0; /* Number of edges in the graph */ 23 | 24 | void display(); 25 | int getIndex(char s[]); 26 | int isAdjacent(char s1[], char s2[]); 27 | void insertVertex(char s[]); 28 | void deleteVertex(char s[]); 29 | void insertEdge(char s1[], char s2[], int wt); 30 | void deleteEdge(char s1[], char s2[] ); 31 | int degree(char s[]); 32 | 33 | main() 34 | { 35 | int choice,wt,x; 36 | char s1[50],s2[50]; 37 | 38 | insertVertex("AA"); 39 | insertVertex("BB"); 40 | insertVertex("CC"); 41 | insertVertex("DD"); 42 | insertVertex("EE"); 43 | insertEdge("AA","BB",3); 44 | insertEdge("AA","CC",5); 45 | insertEdge("CC","DD",4); 46 | insertEdge("DD","AA",2); 47 | insertEdge("BB","EE",9); 48 | 49 | while(1) 50 | { 51 | printf("1.Display Adjacency Matrix\n"); 52 | printf("2.Insert a vertex\n"); 53 | printf("3.Remove a vertex\n"); 54 | printf("4.Insert an edge\n"); 55 | printf("5.Delete an edge\n"); 56 | printf("6.Display degree of a vertex\n"); 57 | printf("7.Check if there is an edge between two vertices\n"); 58 | printf("8.Exit\n"); 59 | printf("Enter your choice : "); 60 | scanf("%d",&choice); 61 | 62 | switch(choice) 63 | { 64 | case 1: 65 | display(); 66 | break; 67 | case 2: 68 | printf("Enter vertex name : "); 69 | scanf("%s",s1); 70 | insertVertex(s1); 71 | break; 72 | case 3: 73 | printf("Enter vertex name : "); 74 | scanf("%s",s1); 75 | deleteVertex(s1); 76 | break; 77 | case 4: 78 | printf("Enter first and second vertices : "); 79 | scanf("%s",s1); 80 | scanf("%s",s2); 81 | printf("Enter weight : "); 82 | scanf("%d",&wt); 83 | insertEdge(s1,s2,wt); 84 | break; 85 | case 5: 86 | printf("Enter first and second vertices : "); 87 | scanf("%s",s1); 88 | scanf("%s",s2); 89 | deleteEdge(s1,s2); 90 | break; 91 | case 6: 92 | printf("Enter vertex name : "); 93 | scanf("%s",s1); 94 | x = degree(s1); 95 | if ( x == -1 ) 96 | printf("Vertex name not present in the graph\n\n"); 97 | else 98 | printf("Degree is : %d\n\n",x ); 99 | break; 100 | case 7: 101 | printf("Enter the name of first vertex : "); 102 | scanf("%s",s1); 103 | printf("Enter the name of second vertex : "); 104 | scanf("%s",s2); 105 | x = isAdjacent(s1,s2); 106 | if( x == -1 ) 107 | printf("Vertex name not present in the graph\n\n"); 108 | else if ( x == 0 ) 109 | printf("There is no edge from %s to %s\n\n",s1,s2); 110 | else 111 | printf("There is an edge from %s to %s\n\n",s1,s2); 112 | break; 113 | case 8: 114 | exit(1); 115 | default: 116 | printf("Wrong choice\n\n"); 117 | break; 118 | } 119 | } 120 | } 121 | 122 | void display() 123 | { 124 | int i,j; 125 | 126 | printf("Adjacency Matrix - \n"); 127 | for(i=0; i 10 | #include 11 | 12 | typedef enum {False,True} boolean; 13 | 14 | struct node 15 | { 16 | struct node *left; 17 | boolean leftThread; 18 | int info; 19 | boolean rightThread; 20 | struct node *right; 21 | }; 22 | 23 | struct node *inorderPredecessor(struct node *p); 24 | struct node *inorderSuccessor(struct node *p); 25 | struct node *getNode(int x); 26 | struct node *insertNode(struct node *root, int x); 27 | struct node *deleteNode(struct node *root, int x); 28 | void inorder(struct node *root); 29 | 30 | 31 | main( ) 32 | { 33 | int choice,x; 34 | struct node *root=NULL; 35 | 36 | while(1) 37 | { 38 | printf("1.Insert a new node\n"); 39 | printf("2.Delete a node\n"); 40 | printf("3.Inorder Traversal\n"); 41 | printf("4.Exit\n"); 42 | 43 | printf("Enter your choice : "); 44 | scanf("%d",&choice); 45 | 46 | switch(choice) 47 | { 48 | case 1: 49 | printf("Enter the key to be inserted : "); 50 | scanf("%d",&x); 51 | root = insertNode(root,x); 52 | break; 53 | case 2: 54 | printf("Enter the key to be deleted : "); 55 | scanf("%d",&x); 56 | root = deleteNode(root,x); 57 | break; 58 | case 3: 59 | inorder(root); 60 | break; 61 | case 4: 62 | exit(1); 63 | default: 64 | printf("Wrong choice\n"); 65 | } 66 | } 67 | } 68 | 69 | struct node *inorderPredecessor(struct node *p) 70 | { 71 | if( p->leftThread == True ) 72 | return p->left; 73 | 74 | p = p->left; 75 | while( p->rightThread == False ) 76 | p = p->right; 77 | return p; 78 | } 79 | 80 | struct node *inorderSuccessor(struct node *p) 81 | { 82 | if( p->rightThread == True ) 83 | return p->right; 84 | 85 | p = p->right; 86 | while( p->leftThread == False ) 87 | p = p->left; 88 | return p; 89 | } 90 | 91 | void inorder( struct node *root) 92 | { 93 | struct node *p; 94 | if(root == NULL ) 95 | { 96 | printf("Tree is empty\n"); 97 | return; 98 | } 99 | 100 | /* Find the leftmost node of the tree */ 101 | p = root; 102 | while( p->leftThread == False ) 103 | p = p->left; 104 | 105 | while( p!=NULL ) 106 | { 107 | printf("%d ",p->info); 108 | if( p->rightThread == True) 109 | p = p->right; 110 | else 111 | { 112 | p = p->right; 113 | while( p->leftThread == False ) 114 | p = p->left; 115 | } 116 | } 117 | printf("\n\n"); 118 | } 119 | 120 | struct node *getNode(int x) 121 | { 122 | struct node *p = (struct node *) malloc(sizeof(struct node)); 123 | p->info = x; 124 | p->left = NULL; 125 | p->right = NULL; 126 | p->leftThread = True; 127 | p->rightThread = True; 128 | return p; 129 | } 130 | 131 | struct node *insertNode(struct node *root, int x) 132 | { 133 | struct node *temp,*par,*p; 134 | 135 | p = root; 136 | par = NULL; 137 | 138 | while( p!=NULL ) 139 | { 140 | par = p; 141 | if(x < p->info) 142 | { 143 | if(p->leftThread == False) 144 | p = p->left; 145 | else 146 | break; 147 | } 148 | else if (x > p->info) 149 | { 150 | if(p->rightThread == False) 151 | p = p->right; 152 | else 153 | break; 154 | } 155 | else 156 | { 157 | printf("%d already present in the tree\n",x); 158 | return; 159 | } 160 | } 161 | 162 | temp = getNode(x); 163 | 164 | if( par == NULL ) 165 | root = temp; 166 | else if( x < par->info ) /* inserted as left child */ 167 | { 168 | temp->left = par->left; 169 | temp->right = par; 170 | par->leftThread = False; 171 | par->left = temp; 172 | } 173 | else /* inserted as right child */ 174 | { 175 | temp->left = par; 176 | temp->right = par->right; 177 | par->rightThread = False; 178 | par->right = temp; 179 | } 180 | return root; 181 | } 182 | 183 | struct node *deleteNode(struct node *root, int x) 184 | { 185 | struct node *par,*p, *ps, *s, *ch, *pred, *succ; 186 | 187 | p = root; 188 | par = NULL; 189 | 190 | while( p!=NULL) 191 | { 192 | if(x == p->info) 193 | break; 194 | 195 | par = p; 196 | if(x < p->info) 197 | { 198 | if(p->leftThread == False) 199 | p = p->left; 200 | else 201 | break; 202 | } 203 | else 204 | { 205 | if(p->rightThread == False) 206 | p = p->right; 207 | else 208 | break; 209 | } 210 | } 211 | 212 | if(p == NULL || p->info != x) 213 | { 214 | printf("%d not found\n",x); 215 | return root; 216 | } 217 | 218 | if(p->leftThread==False && p->rightThread==False )/*Case C : 2 children*/ 219 | { 220 | /*Find inorder successor and its parent*/ 221 | ps = p; 222 | s = p->right; 223 | 224 | while(s->leftThread == False) 225 | { 226 | ps = s; 227 | s = s->left; 228 | } 229 | p->info = s->info; 230 | p = s; 231 | par = ps; 232 | } 233 | 234 | /*Case A : No child*/ 235 | if(p->leftThread == True && p->rightThread == True) 236 | { 237 | if(par == NULL) 238 | root = NULL; 239 | else if(p == par->left) 240 | { 241 | par->leftThread = True; 242 | par->left = p->left; 243 | } 244 | else 245 | { 246 | par->rightThread = True; 247 | par->right = p->right; 248 | } 249 | free(p); 250 | return root; 251 | } 252 | /*Case B : 1 child*/ 253 | if(p->leftThread == False) /*node to be deleted has left child */ 254 | ch = p->left; 255 | else /*node to be deleted has right child */ 256 | ch = p->right; 257 | 258 | if (par == NULL) /*node to be deleted is root node*/ 259 | root = ch; 260 | else if (p == par->left) /* node is left child of its parent */ 261 | par->left = ch; 262 | else /*node is right child of its parent*/ 263 | par->right = ch; 264 | 265 | pred = inorderPredecessor(p); 266 | succ = inorderSuccessor(p); 267 | 268 | if(p->leftThread == False) /*if p has left child, right is a thread */ 269 | pred->right = succ; 270 | else /*p has right child,left is a thread*/ 271 | succ->left = pred; 272 | 273 | free(p); 274 | return root; 275 | } 276 | 277 | -------------------------------------------------------------------------------- /Trees/binary-search-tree-recursive.C: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | 7 | #include 8 | #include 9 | 10 | struct node 11 | { 12 | struct node *lchild; 13 | int info; 14 | struct node *rchild; 15 | }; 16 | 17 | struct node *getnode(int x); 18 | void display(struct node *p,int level); 19 | struct node *search(struct node *p, int x); 20 | struct node *insertNode(struct node *p, int x); 21 | struct node *deleteNode(struct node *p, int x); 22 | struct node *Min(struct node *p); 23 | struct node *Max(struct node *p); 24 | int height(struct node *p); 25 | void preorder(struct node *p); 26 | void inorder(struct node *p); 27 | void postorder(struct node *p); 28 | 29 | main() 30 | { 31 | struct node *root=NULL,*p; 32 | int choice,x; 33 | 34 | root=insertNode(root,70); 35 | root=insertNode(root,80); 36 | root=insertNode(root,75); 37 | root=insertNode(root,89); 38 | root=insertNode(root,82); 39 | root=insertNode(root,93); 40 | root=insertNode(root,83); 41 | display(root,0); 42 | root=deleteNode(root,80); 43 | display(root,0); 44 | 45 | while(1) 46 | { 47 | printf("\n"); 48 | printf("1.Display Tree\n"); 49 | printf("2.Search\n"); 50 | printf("3.Insert a new node\n"); 51 | printf("4.Delete a node\n"); 52 | printf("5.Preorder Traversal\n"); 53 | printf("6.Inorder Traversal\n"); 54 | printf("7.Postorder Traversal\n"); 55 | printf("8.Height of tree\n"); 56 | printf("9.Find Minimum key\n"); 57 | printf("10.Find Maximum key\n"); 58 | printf("11.Quit\n"); 59 | printf("Enter your choice : "); 60 | scanf("%d",&choice); 61 | 62 | if(choice==11) 63 | break; 64 | 65 | switch(choice) 66 | { 67 | case 1: 68 | display(root,0); 69 | break; 70 | case 2: 71 | printf("Enter the key to be searched : "); 72 | scanf("%d",&x); 73 | p=search(root,x); 74 | if(p==NULL) 75 | printf("Key not found\n"); 76 | else 77 | printf("Key found\n"); 78 | break; 79 | case 3: 80 | printf("Enter the key to be inserted : "); 81 | scanf("%d",&x); 82 | root=insertNode(root,x); 83 | break; 84 | case 4: 85 | printf("Enter the key to be deleted : "); 86 | scanf("%d",&x); 87 | root=deleteNode(root,x); 88 | break; 89 | case 5: 90 | preorder(root); 91 | break; 92 | case 6: 93 | inorder(root); 94 | break; 95 | case 7: 96 | postorder(root); 97 | break; 98 | case 8: 99 | printf("Height of tree is %d\n", height(root)); 100 | break; 101 | case 9: 102 | p=Min(root); 103 | if(p==NULL) 104 | printf("Tree is empty\n"); 105 | else 106 | printf("Minimum key is %d\n", p->info); 107 | break; 108 | case 10: 109 | p=Max(root); 110 | if(p==NULL) 111 | printf("Tree is empty\n"); 112 | else 113 | printf("Maximum key is %d\n", p->info); 114 | break; 115 | }/*End of switch */ 116 | }/*End of while */ 117 | }/*End of main( )*/ 118 | 119 | struct node *getnode(int x) 120 | { 121 | struct node *p=(struct node *) malloc(sizeof(struct node)); 122 | p->info=x; 123 | p->lchild=NULL; 124 | p->rchild=NULL; 125 | return p; 126 | } 127 | 128 | struct node *insertNode(struct node *p, int x) 129 | { 130 | if(p==NULL) 131 | p=getnode(x); 132 | else if(x < p->info) 133 | p->lchild = insertNode(p->lchild,x); 134 | else if(x > p->info) 135 | p->rchild = insertNode(p->rchild,x); 136 | else 137 | printf("%d already present in tree\n",x); 138 | return p; 139 | }/*End of insertNode()*/ 140 | 141 | struct node *search(struct node *p, int x) 142 | { 143 | if(p==NULL) 144 | return NULL; /*key not found*/ 145 | if(x < p->info)/*search in left subtree*/ 146 | return search(p->lchild, x); 147 | if(x > p->info)/*search in right subtree*/ 148 | return search(p->rchild, x); 149 | return p; /*key found*/ 150 | }/*End of search()*/ 151 | 152 | struct node *deleteNode(struct node *p, int x) 153 | { 154 | struct node *ch,*s; 155 | 156 | if(p==NULL) 157 | { 158 | printf("%d not found\n",x); 159 | return p; 160 | } 161 | if(x < p->info) /*delete from left subtree*/ 162 | p->lchild = deleteNode(p->lchild, x); 163 | else if(x > p->info) /*delete from right subtree*/ 164 | p->rchild = deleteNode(p->rchild, x); 165 | else 166 | { 167 | /*key to be deleted is found*/ 168 | if( p->lchild!=NULL && p->rchild!=NULL ) /*2 children*/ 169 | { 170 | s=p->rchild; 171 | while(s->lchild) 172 | s=s->lchild; 173 | p->info=s->info; 174 | p->rchild = deleteNode(p->rchild,s->info); 175 | } 176 | else /*1 child or no child*/ 177 | { 178 | if(p->lchild != NULL) /*only left child*/ 179 | ch=p->lchild; 180 | else /*only right child or no child*/ 181 | ch=p->rchild; 182 | free(p); 183 | p=ch; 184 | } 185 | } 186 | return p; 187 | }/*End of deleteNode()*/ 188 | 189 | 190 | struct node *Min(struct node *p) 191 | { 192 | if(p==NULL) 193 | return NULL; 194 | if(p->lchild==NULL) 195 | return p; 196 | return Min(p->lchild); 197 | }/*End of Min()*/ 198 | 199 | struct node *Max(struct node *p) 200 | { 201 | if(p==NULL) 202 | return NULL; 203 | if(p->rchild==NULL) 204 | return p; 205 | return Max(p->rchild); 206 | }/*End of Max()*/ 207 | 208 | void preorder(struct node *p) 209 | { 210 | if(p == NULL ) /*Base Case*/ 211 | return; 212 | printf("%d ",p->info); 213 | preorder(p->lchild); 214 | preorder(p->rchild); 215 | }/*End of preorder( )*/ 216 | 217 | void inorder(struct node *p) 218 | { 219 | if(p == NULL )/*Base Case*/ 220 | return; 221 | inorder(p->lchild); 222 | printf("%d ",p->info); 223 | inorder(p->rchild); 224 | }/*End of inorder( )*/ 225 | 226 | void postorder(struct node *p) 227 | { 228 | if(p == NULL)/*Base Case*/ 229 | return; 230 | postorder(p->lchild); 231 | postorder(p->rchild); 232 | printf("%d ",p->info); 233 | 234 | }/*End of postorder()*/ 235 | 236 | int height(struct node *p) 237 | { 238 | int hL,hR; 239 | 240 | if(p==NULL) /*Base Case*/ 241 | return 0; 242 | 243 | hL=height(p->lchild); 244 | hR=height(p->rchild); 245 | 246 | if(hL>hR) 247 | return 1+hL; 248 | else 249 | return 1+hR; 250 | }/*End of height()*/ 251 | 252 | void display(struct node *p,int level) 253 | { 254 | int i; 255 | if(p==NULL) 256 | return; 257 | 258 | display(p->rchild, level+1); 259 | printf("\n"); 260 | 261 | for(i=0; iinfo); 264 | 265 | display(p->lchild, level+1); 266 | }/*End of display()*/ -------------------------------------------------------------------------------- /Graph/breadth-first-search/BFS-tree-edges.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | 7 | /* Tree edges */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #define INITIAL 0 14 | #define WAITING 1 15 | #define VISITED 2 16 | 17 | #define MAX 30 18 | 19 | struct Vertex 20 | { 21 | char name[50]; 22 | int state; 23 | }; 24 | 25 | struct Vertex vertexList[MAX]; 26 | 27 | int adj[MAX][MAX]; /* Adjacency Matrix */ 28 | int n=0; /* Number of vertices in the graph */ 29 | int e=0; /* Number of edges in the graph */ 30 | 31 | void display(); 32 | int getIndex(char s[]); 33 | int isAdjacent(char s1[], char s2[]); 34 | void insertVertex(char s[]); 35 | void deleteVertex(char s[]); 36 | void insertEdge(char s1[], char s2[]); 37 | void deleteEdge(char s1[], char s2[] ); 38 | 39 | void bfs(int v); 40 | void bfsTreeEdges(); 41 | 42 | int queue[MAX]; 43 | int front,rear; 44 | void initializeQueue(); 45 | int isFull(); 46 | int isEmpty(); 47 | void enqueue(int x); 48 | int dequeue(); 49 | 50 | main() 51 | { 52 | insertVertex("Zero"); 53 | insertVertex("One"); 54 | insertVertex("Two"); 55 | insertVertex("Three"); 56 | insertVertex("Four"); 57 | insertVertex("Five"); 58 | insertVertex("Six"); 59 | insertVertex("Seven"); 60 | insertVertex("Eight"); 61 | insertVertex("Nine"); 62 | 63 | insertEdge("Zero", "One"); 64 | insertEdge("Zero", "Three"); 65 | insertEdge("One", "Two"); 66 | insertEdge("One", "Four"); 67 | insertEdge("One", "Five"); 68 | insertEdge("Two", "Three"); 69 | insertEdge("Two", "Five"); 70 | insertEdge("Three", "Six"); 71 | insertEdge("Four", "Five"); 72 | insertEdge("Four", "Seven"); 73 | insertEdge("Five", "Six"); 74 | insertEdge("Five", "Eight"); 75 | insertEdge("Six", "Eight"); 76 | insertEdge("Six", "Nine"); 77 | insertEdge("Seven", "Eight"); 78 | insertEdge("Eight", "Nine"); 79 | 80 | bfsTreeEdges(); 81 | } 82 | 83 | void bfsTreeEdges() 84 | { 85 | char s[50]; 86 | int u,v; 87 | 88 | printf("Enter starting vertex for Breadth First Search : "); 89 | scanf("%s",s); 90 | 91 | u = getIndex(s); 92 | if(u == -1) 93 | return; 94 | 95 | for (v = 0; v < n; v++) 96 | vertexList[v].state = INITIAL; 97 | 98 | bfs(u); 99 | 100 | for (v = 0; v < n; v++) 101 | if(vertexList[v].state == INITIAL) 102 | bfs(v); 103 | } 104 | 105 | void bfs(int v) 106 | { 107 | int i; 108 | initializeQueue(); 109 | enqueue(v); 110 | vertexList[v].state = WAITING; 111 | 112 | while( !isEmpty() ) 113 | { 114 | v = dequeue(); 115 | vertexList[v].state = VISITED; 116 | 117 | for (i = 0; i 7 | #include 8 | #include 9 | 10 | #define MAX 30 11 | #define TEMPORARY 1 12 | #define PERMANENT 2 13 | #define NIL -1 14 | #define INFINITY 99999 15 | 16 | struct Vertex 17 | { 18 | char name[50]; 19 | int status; 20 | int predecessor; 21 | int length; 22 | }; 23 | struct Vertex vertexList[MAX]; 24 | 25 | int adj[MAX][MAX]; /* Adjacency Matrix */ 26 | int n=0; /* Number of vertices in the graph */ 27 | int e=0; /* Number of edges in the graph */ 28 | 29 | void display(); 30 | int getIndex(char s[]); 31 | void insertVertex(char s[]); 32 | void deleteVertex(char s[]); 33 | void insertEdge(char s1[], char s2[], int wt); 34 | void deleteEdge(char s1[], char s2[]); 35 | 36 | void prims(); 37 | int tempVertexMinL(); 38 | 39 | main() 40 | { 41 | insertVertex("Zero"); 42 | insertVertex("One"); 43 | insertVertex("Two"); 44 | insertVertex("Three"); 45 | insertVertex("Four"); 46 | insertVertex("Five"); 47 | insertVertex("Six"); 48 | insertVertex("Seven"); 49 | insertVertex("Eight"); 50 | insertVertex("Nine"); 51 | 52 | insertEdge("Zero", "One", 19); 53 | insertEdge("Zero", "Three", 14); 54 | insertEdge("Zero", "Four", 12); 55 | insertEdge("One", "Two", 20); 56 | insertEdge("One", "Four", 18); 57 | insertEdge("Two", "Four", 17); 58 | insertEdge("Two", "Five", 15); 59 | insertEdge("Two", "Nine", 29); 60 | insertEdge("Three", "Four", 13); 61 | insertEdge("Three", "Six", 28); 62 | insertEdge("Four", "Five", 16); 63 | insertEdge("Four", "Six", 21); 64 | insertEdge("Four", "Seven", 22); 65 | insertEdge("Four", "Eight", 24); 66 | insertEdge("Five", "Eight", 26); 67 | insertEdge("Five", "Nine", 27); 68 | insertEdge("Six", "Seven", 23); 69 | insertEdge("Seven", "Eight", 30); 70 | insertEdge("Eight", "Nine", 35); 71 | 72 | prims(); 73 | } 74 | 75 | void prims() 76 | { 77 | int c,v; 78 | 79 | int edgesInTree = 0; 80 | int wtTree = 0; 81 | int root; 82 | 83 | for(v=0; v 9 | #include 10 | #include 11 | 12 | #define INITIAL 0 13 | #define VISITED 1 14 | #define FINISHED 2 15 | 16 | #define MAX 30 17 | 18 | struct Vertex 19 | { 20 | char name[50]; 21 | int state; 22 | int discoveryTime; 23 | int finishingTime; 24 | }; 25 | 26 | int time; 27 | 28 | struct Vertex vertexList[MAX]; 29 | 30 | int adj[MAX][MAX]; /* Adjacency Matrix */ 31 | int n=0; /* Number of vertices in the graph */ 32 | int e=0; /* Number of edges in the graph */ 33 | 34 | void display(); 35 | int getIndex(char s[]); 36 | int isAdjacent(char s1[], char s2[]); 37 | void insertVertex(char s[]); 38 | void deleteVertex(char s[]); 39 | void insertEdge(char s1[], char s2[]); 40 | void deleteEdge(char s1[], char s2[] ); 41 | 42 | void dfsTraversalAll(); 43 | void dfs(int v); 44 | 45 | main() 46 | { 47 | insertVertex("Zero"); 48 | insertVertex("One"); 49 | insertVertex("Two"); 50 | insertVertex("Three"); 51 | insertVertex("Four"); 52 | insertVertex("Five"); 53 | insertVertex("Six"); 54 | insertVertex("Seven"); 55 | insertVertex("Eight"); 56 | insertVertex("Nine"); 57 | insertVertex("Ten"); 58 | insertVertex("Eleven"); 59 | insertVertex("Twelve"); 60 | insertVertex("Thirteen"); 61 | insertVertex("Fourteen"); 62 | insertVertex("Fifteen"); 63 | insertVertex("Sixteen"); 64 | 65 | insertEdge("Zero","One"); 66 | insertEdge("Zero","Two"); 67 | insertEdge("Zero","Four"); 68 | insertEdge("One","Three"); 69 | insertEdge("Two","Three"); 70 | insertEdge("Two","Four"); 71 | insertEdge("Two","Five"); 72 | insertEdge("Three","Zero"); 73 | insertEdge("Four","Five"); 74 | insertEdge("Five","Three"); 75 | insertEdge("Six","One"); 76 | insertEdge("Six","Seven"); 77 | insertEdge("Six","Eight"); 78 | insertEdge("Six","Nine"); 79 | insertEdge("Seven","Nine"); 80 | insertEdge("Eight","Ten"); 81 | insertEdge("Nine","Five"); 82 | insertEdge("Ten","Six"); 83 | insertEdge("Ten","Nine"); 84 | insertEdge("Eleven","Eight"); 85 | insertEdge("Eleven","Thirteen"); 86 | insertEdge("Eleven","Fifteen"); 87 | insertEdge("Twelve","Eleven"); 88 | insertEdge("Thirteen","Eight"); 89 | insertEdge("Thirteen","Fourteen"); 90 | insertEdge("Thirteen","Fifteen"); 91 | insertEdge("Thirteen","Sixteen"); 92 | insertEdge("Fourteen","Sixteen"); 93 | insertEdge("Fifteen","Twelve"); 94 | insertEdge("Fifteen","Sixteen"); 95 | 96 | dfsTraversalAll(); 97 | } 98 | 99 | void dfs(int v) 100 | { 101 | int i; 102 | vertexList[v].state = VISITED; 103 | vertexList[v].discoveryTime = ++time; 104 | 105 | for(i=0; i 9 | #include 10 | #include 11 | 12 | #define MAX 30 13 | 14 | struct Vertex 15 | { 16 | char name[50]; 17 | }; 18 | 19 | struct Vertex vertexList[MAX]; 20 | 21 | int adj[MAX][MAX]; /* Adjacency Matrix */ 22 | int n=0; /* Number of vertices in the graph */ 23 | int e=0; /* Number of edges in the graph */ 24 | 25 | void display(); 26 | int getIndex(char s[]); 27 | int isAdjacent(char s1[], char s2[]); 28 | void insertVertex(char s[]); 29 | void deleteVertex(char s[]); 30 | void insertEdge(char s1[], char s2[]); 31 | void deleteEdge(char s1[], char s2[] ); 32 | int outdegree(char s[]); 33 | int indegree(char s[]); 34 | 35 | main() 36 | { 37 | int choice,x; 38 | char s1[50],s2[50]; 39 | 40 | insertVertex("AA"); 41 | insertVertex("BB"); 42 | insertVertex("CC"); 43 | insertVertex("DD"); 44 | insertVertex("EE"); 45 | 46 | insertEdge("AA","BB"); 47 | insertEdge("AA","CC"); 48 | insertEdge("CC","DD"); 49 | insertEdge("DD","AA"); 50 | insertEdge("CC","AA"); 51 | insertEdge("BB","EE"); 52 | 53 | while(1) 54 | { 55 | printf("1.Display Adjacency Matrix\n"); 56 | printf("2.Insert a vertex\n"); 57 | printf("3.Remove a vertex\n"); 58 | printf("4.Insert an edge\n"); 59 | printf("5.Delete an edge\n"); 60 | printf("6.Display Indegree and outdegree of a vertex\n"); 61 | printf("7.Check if there is an edge between two vertices\n"); 62 | printf("8.Exit\n"); 63 | 64 | printf("Enter your choice : "); 65 | scanf("%d",&choice); 66 | 67 | switch(choice) 68 | { 69 | case 1: 70 | display(); 71 | break; 72 | case 2: 73 | printf("Enter vertex name : "); 74 | scanf("%s",s1); 75 | insertVertex(s1); 76 | break; 77 | case 3: 78 | printf("Enter vertex name : "); 79 | scanf("%s",s1); 80 | deleteVertex(s1); 81 | break; 82 | case 4: 83 | printf("Enter start and end vertices : "); 84 | scanf("%s",s1); 85 | scanf("%s",s2); 86 | insertEdge(s1,s2); 87 | break; 88 | case 5: 89 | printf("Enter start and end vertices : "); 90 | scanf("%s",s1); 91 | scanf("%s",s2); 92 | deleteEdge(s1,s2); 93 | break; 94 | case 6: 95 | printf("Enter vertex name : "); 96 | scanf("%s",s1); 97 | x = indegree(s1); 98 | if ( x == -1 ) 99 | printf("Vertex name not present in the graph\n\n"); 100 | else 101 | { 102 | printf("Indegree is : %d\n\n",x ); 103 | printf("Outdegree is : %d\n\n", outdegree(s1)); 104 | } 105 | break; 106 | case 7: 107 | printf("Enter the name of first vertex : "); 108 | scanf("%s",s1); 109 | printf("Enter the name of second vertex : "); 110 | scanf("%s",s2); 111 | x = isAdjacent(s1,s2); 112 | if( x == -1 ) 113 | printf("Vertex name not present in the graph\n\n"); 114 | else if ( x == 0 ) 115 | printf("There is no edge from %s to %s\n\n",s1,s2); 116 | else 117 | printf("There is an edge from %s to %s\n\n",s1,s2); 118 | break; 119 | case 8: 120 | exit(1); 121 | default: 122 | printf("Wrong choice\n\n"); 123 | break; 124 | } 125 | } 126 | } 127 | 128 | void display() 129 | { 130 | int i,j; 131 | 132 | printf("Adjacency Matrix - \n"); 133 | for(i=0; i 8 | #include 9 | #include 10 | 11 | #define INITIAL 0 12 | #define VISITED 1 13 | 14 | #define MAX 30 15 | 16 | struct Vertex 17 | { 18 | char name[50]; 19 | int state; 20 | int predecessor; 21 | }; 22 | 23 | struct Vertex vertexList[MAX]; 24 | 25 | int adj[MAX][MAX]; /* Adjacency Matrix */ 26 | int n=0; /* Number of vertices in the graph */ 27 | int e=0; /* Number of edges in the graph */ 28 | 29 | void display(); 30 | int getIndex(char s[]); 31 | int isAdjacent(char s1[], char s2[]); 32 | void insertVertex(char s[]); 33 | void deleteVertex(char s[]); 34 | void insertEdge(char s1[], char s2[]); 35 | void deleteEdge(char s1[], char s2[] ); 36 | 37 | void dfsTreeEdges(); 38 | void dfs(int v); 39 | 40 | int stack[MAX]; 41 | int top; 42 | 43 | void initializeStack(); 44 | int isEmpty(); 45 | int isFull(); 46 | void push(int x); 47 | int pop(); 48 | 49 | 50 | main() 51 | { 52 | insertVertex("Zero"); 53 | insertVertex("One"); 54 | insertVertex("Two"); 55 | insertVertex("Three"); 56 | insertVertex("Four"); 57 | insertVertex("Five"); 58 | insertVertex("Six"); 59 | insertVertex("Seven"); 60 | insertVertex("Eight"); 61 | insertVertex("Nine"); 62 | insertVertex("Ten"); 63 | insertVertex("Eleven"); 64 | 65 | insertEdge("Zero", "One"); 66 | insertEdge("Zero", "Three"); 67 | insertEdge("One", "Two"); 68 | insertEdge("One", "Four"); 69 | insertEdge("One", "Five"); 70 | insertEdge("Two", "Five"); 71 | insertEdge("Two", "Seven"); 72 | insertEdge("Three", "Six"); 73 | insertEdge("Four", "Three"); 74 | insertEdge("Five", "Three"); 75 | insertEdge("Five", "Six"); 76 | insertEdge("Five", "Eight"); 77 | insertEdge("Seven", "Eight"); 78 | insertEdge("Seven", "Ten"); 79 | insertEdge("Eight", "Eleven"); 80 | insertEdge("Nine", "Six"); 81 | insertEdge("Eleven", "Nine"); 82 | 83 | dfsTreeEdges(); 84 | 85 | } 86 | 87 | void dfsTreeEdges() 88 | { 89 | int v,u; 90 | char s[50]; 91 | 92 | printf("Enter starting vertex for Depth First Search : "); 93 | scanf("%s", &s); 94 | u = getIndex(s); 95 | if(u == -1) 96 | return; 97 | 98 | for(v=0; v= 0; i--) 135 | if ( adj[v][i]!=0 && vertexList[i].state == INITIAL ) 136 | { 137 | push(i); 138 | vertexList[i].predecessor = v; 139 | } 140 | 141 | } 142 | printf("\n"); 143 | } 144 | 145 | 146 | void display() 147 | { 148 | int i,j; 149 | 150 | printf("Adjacency Matrix - \n"); 151 | for(i=0; i 9 | #include 10 | #include 11 | 12 | #define INITIAL 0 13 | #define VISITED 1 14 | 15 | #define MAX 30 16 | 17 | struct Vertex 18 | { 19 | char name[50]; 20 | int state; 21 | }; 22 | 23 | struct Vertex vertexList[MAX]; 24 | 25 | int adj[MAX][MAX]; /* Adjacency Matrix */ 26 | int n=0; /* Number of vertices in the graph */ 27 | int e=0; /* Number of edges in the graph */ 28 | 29 | void display(); 30 | int getIndex(char s[]); 31 | int isAdjacent(char s1[], char s2[]); 32 | void insertVertex(char s[]); 33 | void deleteVertex(char s[]); 34 | void insertEdge(char s1[], char s2[]); 35 | void deleteEdge(char s1[], char s2[] ); 36 | 37 | void dfsTraversal(); 38 | void dfsTraversalAll(); 39 | void dfs(int v); 40 | 41 | int stack[MAX]; 42 | int top; 43 | 44 | void initializeStack(); 45 | int isEmpty(); 46 | int isFull(); 47 | void push(int x); 48 | int pop(); 49 | 50 | 51 | main() 52 | { 53 | insertVertex("Zero"); 54 | insertVertex("One"); 55 | insertVertex("Two"); 56 | insertVertex("Three"); 57 | insertVertex("Four"); 58 | insertVertex("Five"); 59 | insertVertex("Six"); 60 | insertVertex("Seven"); 61 | insertVertex("Eight"); 62 | insertVertex("Nine"); 63 | insertVertex("Ten"); 64 | insertVertex("Eleven"); 65 | 66 | insertEdge("Zero", "One"); 67 | insertEdge("Zero", "Three"); 68 | insertEdge("One", "Two"); 69 | insertEdge("One", "Four"); 70 | insertEdge("One", "Five"); 71 | insertEdge("Two", "Five"); 72 | insertEdge("Two", "Seven"); 73 | insertEdge("Three", "Six"); 74 | insertEdge("Four", "Three"); 75 | insertEdge("Five", "Three"); 76 | insertEdge("Five", "Six"); 77 | insertEdge("Five", "Eight"); 78 | insertEdge("Seven", "Eight"); 79 | insertEdge("Seven", "Ten"); 80 | insertEdge("Eight", "Eleven"); 81 | insertEdge("Nine", "Six"); 82 | insertEdge("Eleven", "Nine"); 83 | 84 | dfsTraversal(); 85 | 86 | dfsTraversalAll(); 87 | } 88 | 89 | void dfsTraversal() 90 | { 91 | int u,v; 92 | char s[50]; 93 | 94 | printf("Enter starting vertex for Depth First Search : "); 95 | scanf("%s", &s); 96 | u = getIndex(s); 97 | if(u == -1) 98 | return; 99 | for (v = 0; v < n; v++) 100 | vertexList[v].state = INITIAL; 101 | 102 | dfs(u); 103 | } 104 | 105 | void dfs(int v) 106 | { 107 | int i; 108 | initializeStack(); 109 | push(v); 110 | 111 | while ( !isEmpty() ) 112 | { 113 | v = pop(); 114 | if( vertexList[v].state == INITIAL ) 115 | { 116 | printf("%s ", vertexList[v].name); 117 | vertexList[v].state=VISITED; 118 | } 119 | 120 | for(i = n-1; i >= 0; i--) 121 | if ( adj[v][i]!=0 && vertexList[i].state == INITIAL ) 122 | push(i); 123 | } 124 | printf("\n"); 125 | } 126 | 127 | void dfsTraversalAll() 128 | { 129 | int u,v; 130 | char s[50]; 131 | 132 | printf("Enter starting vertex for Depth First Search : "); 133 | scanf("%s", &s); 134 | u = getIndex(s); 135 | if(u == -1) 136 | return; 137 | 138 | for (v = 0; v < n; v++) 139 | vertexList[v].state = INITIAL; 140 | 141 | dfs(u); 142 | 143 | for(v=0; v 9 | #include 10 | #include 11 | 12 | #define INITIAL 0 13 | #define WAITING 1 14 | #define VISITED 2 15 | 16 | #define MAX 30 17 | 18 | struct Vertex 19 | { 20 | char name[50]; 21 | int state; 22 | }; 23 | 24 | struct Vertex vertexList[MAX]; 25 | 26 | int adj[MAX][MAX]; /* Adjacency Matrix */ 27 | int n=0; /* Number of vertices in the graph */ 28 | int e=0; /* Number of edges in the graph */ 29 | 30 | void display(); 31 | int getIndex(char s[]); 32 | int isAdjacent(char s1[], char s2[]); 33 | void insertVertex(char s[]); 34 | void deleteVertex(char s[]); 35 | void insertEdge(char s1[], char s2[]); 36 | void deleteEdge(char s1[], char s2[] ); 37 | 38 | void bfsTraversal(); 39 | void bfsTraversalAll(); 40 | void bfs(int v); 41 | 42 | int queue[MAX]; 43 | int front,rear; 44 | void initializeQueue(); 45 | int isFull(); 46 | int isEmpty(); 47 | void enqueue(int x); 48 | int dequeue(); 49 | 50 | main() 51 | { 52 | insertVertex("Zero"); 53 | insertVertex("One"); 54 | insertVertex("Two"); 55 | insertVertex("Three"); 56 | insertVertex("Four"); 57 | insertVertex("Five"); 58 | insertVertex("Six"); 59 | insertVertex("Seven"); 60 | insertVertex("Eight"); 61 | insertVertex("Nine"); 62 | 63 | insertEdge("Zero", "One"); 64 | insertEdge("Zero", "Three"); 65 | insertEdge("One", "Two"); 66 | insertEdge("One", "Four"); 67 | insertEdge("One", "Five"); 68 | insertEdge("Two", "Three"); 69 | insertEdge("Two", "Five"); 70 | insertEdge("Three", "Six"); 71 | insertEdge("Four", "Five"); 72 | insertEdge("Four", "Seven"); 73 | insertEdge("Five", "Six"); 74 | insertEdge("Five", "Eight"); 75 | insertEdge("Six", "Eight"); 76 | insertEdge("Six", "Nine"); 77 | insertEdge("Seven", "Eight"); 78 | insertEdge("Eight", "Nine"); 79 | 80 | bfsTraversal(); 81 | 82 | bfsTraversalAll(); 83 | } 84 | 85 | void bfsTraversal() 86 | { 87 | char s[50]; 88 | int u,v; 89 | 90 | printf("Enter starting vertex for Breadth First Search : "); 91 | scanf("%s",s); 92 | 93 | u = getIndex(s); 94 | if(u == -1) 95 | return; 96 | 97 | for(v=0; v 9 | #include 10 | #include 11 | 12 | #define MAX 30 13 | 14 | struct Vertex 15 | { 16 | char name[50]; 17 | }; 18 | 19 | struct Vertex vertexList[MAX]; 20 | 21 | int adj[MAX][MAX]; /* Adjacency Matrix */ 22 | int n=0; /* Number of vertices in the graph */ 23 | int e=0; /* Number of edges in the graph */ 24 | 25 | void display(); 26 | int getIndex(char s[]); 27 | int isAdjacent(char s1[], char s2[]); 28 | void insertVertex(char s[]); 29 | void deleteVertex(char s[]); 30 | void insertEdge(char s1[], char s2[], int wt); 31 | void deleteEdge(char s1[], char s2[] ); 32 | int outdegree(char s[]); 33 | int indegree(char s[]); 34 | 35 | main() 36 | { 37 | int choice,wt,x; 38 | char s1[50],s2[50]; 39 | 40 | insertVertex("AA"); 41 | insertVertex("BB"); 42 | insertVertex("CC"); 43 | insertVertex("DD"); 44 | insertVertex("EE"); 45 | insertEdge("AA","BB",3); 46 | insertEdge("AA","CC",5); 47 | insertEdge("CC","DD",4); 48 | insertEdge("DD","AA",2); 49 | insertEdge("CC","AA",7); 50 | insertEdge("BB","EE",9); 51 | 52 | while(1) 53 | { 54 | printf("1.Display Adjacency Matrix\n"); 55 | printf("2.Insert a vertex\n"); 56 | printf("3.Remove a vertex\n"); 57 | printf("4.Insert an edge\n"); 58 | printf("5.Delete an edge\n"); 59 | printf("6.Display Indegree and outdegree of a vertex\n"); 60 | printf("7.Check if there is an edge between two vertices\n"); 61 | printf("8.Exit\n"); 62 | printf("Enter your choice : "); 63 | scanf("%d",&choice); 64 | 65 | switch(choice) 66 | { 67 | case 1: 68 | display(); 69 | break; 70 | case 2: 71 | printf("Enter vertex name : "); 72 | scanf("%s",s1); 73 | insertVertex(s1); 74 | break; 75 | case 3: 76 | printf("Enter vertex name : "); 77 | scanf("%s",s1); 78 | deleteVertex(s1); 79 | break; 80 | case 4: 81 | printf("Enter start and end vertices : "); 82 | scanf("%s",s1); 83 | scanf("%s",s2); 84 | printf("Enter weight : "); 85 | scanf("%d",&wt); 86 | insertEdge(s1,s2,wt); 87 | break; 88 | case 5: 89 | printf("Enter start and end vertices : "); 90 | scanf("%s",s1); 91 | scanf("%s",s2); 92 | deleteEdge(s1,s2); 93 | break; 94 | case 6: 95 | printf("Enter vertex name : "); 96 | scanf("%s",s1); 97 | x = indegree(s1); 98 | if ( x == -1 ) 99 | printf("Vertex name not present in the graph\n\n"); 100 | else 101 | { 102 | printf("Indegree is : %d\n\n",x ); 103 | printf("Outdegree is : %d\n\n", outdegree(s1)); 104 | } 105 | break; 106 | case 7: 107 | printf("Enter the name of first vertex : "); 108 | scanf("%s",s1); 109 | printf("Enter the name of second vertex : "); 110 | scanf("%s",s2); 111 | x = isAdjacent(s1,s2); 112 | if( x == -1 ) 113 | printf("Vertex name not present in the graph\n\n"); 114 | else if ( x == 0 ) 115 | printf("There is no edge from %s to %s\n\n",s1,s2); 116 | else 117 | printf("There is an edge from %s to %s\n\n",s1,s2); 118 | break; 119 | case 8: 120 | exit(1); 121 | default: 122 | printf("Wrong choice\n\n"); 123 | break; 124 | } 125 | } 126 | } 127 | 128 | void display() 129 | { 130 | int i,j; 131 | 132 | printf("Adjacency Matrix - \n"); 133 | for(i=0; i 7 | #include 8 | #include 9 | 10 | #define MAX 30 11 | #define TEMPORARY 1 12 | #define PERMANENT 2 13 | #define NIL -1 14 | #define INFINITY 99999 15 | 16 | struct Vertex 17 | { 18 | char name[50]; 19 | int status; 20 | int predecessor; 21 | int pathLength; 22 | }; 23 | 24 | struct Vertex vertexList[MAX]; 25 | 26 | int adj[MAX][MAX]; /* Adjacency Matrix */ 27 | int n=0; /* Number of vertices in the graph */ 28 | int e=0; /* Number of edges in the graph */ 29 | 30 | void display(); 31 | int getIndex(char s[]); 32 | void insertVertex(char s[]); 33 | void deleteVertex(char s[]); 34 | void insertEdge(char s1[], char s2[], int wt); 35 | void deleteEdge(char s1[], char s2[] ); 36 | 37 | void findPaths(char s[]); 38 | void findPath(int s, int v); 39 | void dijkstra(int s); 40 | int tempVertexMinPL(); 41 | 42 | main() 43 | { 44 | insertVertex("Zero"); 45 | insertVertex("One"); 46 | insertVertex("Two"); 47 | insertVertex("Three"); 48 | insertVertex("Four"); 49 | insertVertex("Five"); 50 | insertVertex("Six"); 51 | insertVertex("Seven"); 52 | insertVertex("Eight"); 53 | 54 | insertEdge("Zero", "Three", 2); 55 | insertEdge("Zero", "One", 5); 56 | insertEdge("Zero", "Four", 8); 57 | insertEdge("One", "Four", 2); 58 | insertEdge("Two", "One", 3); 59 | insertEdge("Two", "Five", 4); 60 | insertEdge("Three", "Four", 7); 61 | insertEdge("Three", "Six", 8); 62 | insertEdge("Four", "Five", 9); 63 | insertEdge("Four", "Seven", 4); 64 | insertEdge("Five", "One", 6); 65 | insertEdge("Six", "Seven", 9); 66 | insertEdge("Seven", "Three", 5); 67 | insertEdge("Seven", "Five", 3); 68 | insertEdge("Seven", "Eight", 5); 69 | insertEdge("Eight", "Five", 3); 70 | 71 | findPaths("Zero"); 72 | } 73 | 74 | void findPaths(char source[]) 75 | { 76 | int s,v; 77 | s = getIndex(source); 78 | 79 | if(s == -1) 80 | return; 81 | 82 | dijkstra(s); 83 | 84 | printf("Source Vertex : %s\n\n",source); 85 | 86 | for(v = 0; v < n; v++) 87 | { 88 | printf("Destination Vertex : %s \n", vertexList[v].name); 89 | if ( vertexList[v].pathLength == INFINITY ) 90 | printf("There is no path from %s to %s\n\n", source, vertexList[v].name ); 91 | else 92 | findPath(s,v); 93 | } 94 | } 95 | 96 | void dijkstra(int s) 97 | { 98 | int v, c; 99 | 100 | for(v = 0; v < n; v++) 101 | { 102 | vertexList[v].status = TEMPORARY; 103 | vertexList[v].pathLength = INFINITY; 104 | vertexList[v].predecessor = NIL; 105 | } 106 | 107 | vertexList[s].pathLength = 0; 108 | 109 | while(1) 110 | { 111 | c = tempVertexMinPL(); 112 | 113 | if(c == NIL) 114 | return; 115 | 116 | vertexList[c].status = PERMANENT; 117 | 118 | for(v=0; v=1; i--) 166 | printf("%s ", vertexList[path[i]].name ); 167 | printf("\n Shortest distance is : %d\n\n",sd ); 168 | } 169 | 170 | void display() 171 | { 172 | int i,j; 173 | 174 | printf("Adjacency Matrix - \n"); 175 | for(i=0; i 9 | #include 10 | #include 11 | 12 | #define INITIAL 0 13 | #define WAITING 1 14 | #define VISITED 2 15 | 16 | #define MAX 30 17 | 18 | struct Vertex 19 | { 20 | char name[50]; 21 | int state; 22 | }; 23 | 24 | struct Vertex vertexList[MAX]; 25 | 26 | int adj[MAX][MAX]; /* Adjacency Matrix */ 27 | int n=0; /* Number of vertices in the graph */ 28 | int e=0; /* Number of edges in the graph */ 29 | 30 | void display(); 31 | int getIndex(char s[]); 32 | int isAdjacent(char s1[], char s2[]); 33 | void insertVertex(char s[]); 34 | void deleteVertex(char s[]); 35 | void insertEdge(char s1[], char s2[]); 36 | void deleteEdge(char s1[], char s2[] ); 37 | 38 | void bfsTraversal(); 39 | void bfsTraversalAll(); 40 | void bfs(int v); 41 | 42 | int queue[MAX]; 43 | int front,rear; 44 | void initializeQueue(); 45 | int isFull(); 46 | int isEmpty(); 47 | void enqueue(int x); 48 | int dequeue(); 49 | 50 | main() 51 | { 52 | insertVertex("Zero"); 53 | insertVertex("One"); 54 | insertVertex("Two"); 55 | insertVertex("Three"); 56 | insertVertex("Four"); 57 | insertVertex("Five"); 58 | insertVertex("Six"); 59 | insertVertex("Seven"); 60 | insertVertex("Eight"); 61 | insertVertex("Nine"); 62 | 63 | insertEdge("Zero", "One"); 64 | insertEdge("Zero", "Seven"); 65 | insertEdge("One", "Two"); 66 | insertEdge("One", "Four"); 67 | insertEdge("One", "Five"); 68 | insertEdge("Two", "Three"); 69 | insertEdge("Two", "Five"); 70 | insertEdge("Three", "Six"); 71 | insertEdge("Four", "Seven"); 72 | insertEdge("Five", "Six"); 73 | insertEdge("Five", "Seven"); 74 | insertEdge("Five", "Eight"); 75 | insertEdge("Six", "Nine"); 76 | insertEdge("Seven", "Eight"); 77 | insertEdge("Eight", "Nine"); 78 | 79 | bfsTraversal(); 80 | 81 | bfsTraversalAll(); 82 | } 83 | 84 | void bfsTraversal() 85 | { 86 | char s[50]; 87 | int u,v; 88 | 89 | printf("Enter starting vertex for Breadth First Search : "); 90 | scanf("%s",s); 91 | 92 | u = getIndex(s); 93 | if(u == -1) 94 | return; 95 | 96 | for(v=0; v 7 | #include 8 | #include 9 | 10 | #define INITIAL 0 11 | #define WAITING 1 12 | #define VISITED 2 13 | 14 | #define MAX 30 15 | 16 | struct Vertex 17 | { 18 | char name[50]; 19 | int state; 20 | int componentNumber; 21 | }; 22 | 23 | struct Vertex vertexList[MAX]; 24 | 25 | int adj[MAX][MAX]; /* Adjacency Matrix */ 26 | int n=0; /* Number of vertices in the graph */ 27 | int e=0; /* Number of edges in the graph */ 28 | 29 | void display(); 30 | int getIndex(char s[]); 31 | int isAdjacent(char s1[], char s2[]); 32 | void insertVertex(char s[]); 33 | void deleteVertex(char s[]); 34 | void insertEdge(char s1[], char s2[]); 35 | void deleteEdge(char s1[], char s2[] ); 36 | 37 | int isConnected(); 38 | void bfs(int v); 39 | 40 | int queue[MAX]; 41 | int front,rear; 42 | void initializeQueue(); 43 | int isFull(); 44 | int isEmpty(); 45 | void enqueue(int x); 46 | int dequeue(); 47 | 48 | main() 49 | { 50 | insertVertex("Zero"); 51 | insertVertex("One"); 52 | insertVertex("Two"); 53 | insertVertex("Three"); 54 | insertVertex("Four"); 55 | insertVertex("Five"); 56 | insertVertex("Six"); 57 | insertVertex("Seven"); 58 | insertVertex("Eight"); 59 | insertVertex("Nine"); 60 | insertVertex("Ten"); 61 | insertVertex("Eleven"); 62 | insertVertex("Twelve"); 63 | insertVertex("Thirteen"); 64 | insertVertex("Fourteen"); 65 | insertVertex("Fifteen"); 66 | insertVertex("Sixteen"); 67 | 68 | insertEdge("Zero", "One"); 69 | insertEdge("Zero", "Two"); 70 | insertEdge("Zero", "Three"); 71 | insertEdge("One", "Three"); 72 | insertEdge("Two", "Five"); 73 | insertEdge("Three", "Four"); 74 | insertEdge("Four", "Five"); 75 | insertEdge("Six", "Seven"); 76 | insertEdge("Six", "Eight"); 77 | insertEdge("Seven", "Ten"); 78 | insertEdge("Eight", "Nine"); 79 | insertEdge("Nine", "Ten"); 80 | insertEdge("Eleven", "Twelve"); 81 | insertEdge("Eleven", "Fourteen"); 82 | insertEdge("Eleven", "Fifteen"); 83 | insertEdge("Twelve", "Thirteen"); 84 | insertEdge("Thirteen", "Fourteen"); 85 | insertEdge("Fourteen", "Sixteen"); 86 | 87 | isConnected(); 88 | } 89 | 90 | int isConnected() 91 | { 92 | int cN,v; 93 | 94 | for (v = 0; v < n; v++) 95 | vertexList[v].state = INITIAL; 96 | 97 | cN = 1; 98 | bfs(0,cN); 99 | 100 | for(v = 0; v < n; v++) 101 | if (vertexList[v].state == INITIAL) 102 | { 103 | cN++; 104 | bfs(v,cN); 105 | } 106 | 107 | if (cN == 1) 108 | { 109 | printf("Graph is connected\n"); 110 | return 1; 111 | } 112 | else 113 | { 114 | printf("Graph is not connected, it has %d connected components\n", cN); 115 | for (v = 0; v < n; v++) 116 | printf("%s %d\n", vertexList[v].name, vertexList[v].componentNumber); 117 | return 0; 118 | } 119 | } 120 | 121 | void bfs(int v, int cN) 122 | { 123 | int i; 124 | initializeQueue(); 125 | enqueue(v); 126 | vertexList[v].state = WAITING; 127 | 128 | while( !isEmpty() ) 129 | { 130 | v = dequeue(); 131 | vertexList[v].state = VISITED; 132 | vertexList[v].componentNumber = cN; 133 | 134 | for (i = 0; i 7 | #include 8 | #include 9 | 10 | #define MAX 30 11 | #define NIL -1 12 | 13 | struct Vertex 14 | { 15 | char name[50]; 16 | int father; 17 | }; 18 | struct Vertex vertexList[MAX]; 19 | 20 | struct Edge 21 | { 22 | int u; 23 | int v; 24 | int weight; 25 | }; 26 | 27 | struct QueueNode 28 | { 29 | struct Edge edge; 30 | struct QueueNode *link; 31 | }; 32 | 33 | struct QueueNode *front = NULL; 34 | 35 | int adj[MAX][MAX]; /* Adjacency Matrix */ 36 | int n=0; /* Number of vertices in the graph */ 37 | int e=0; /* Number of edges in the graph */ 38 | 39 | void display(); 40 | int getIndex(char s[]); 41 | void insertVertex(char s[]); 42 | void deleteVertex(char s[]); 43 | void insertEdge(char s1[], char s2[], int wt); 44 | void deleteEdge(char s1[], char s2[] ); 45 | 46 | void kruskals(); 47 | 48 | void insert(struct Edge e); 49 | struct Edge Delete(); 50 | int isEmpty(); 51 | 52 | main() 53 | { 54 | insertVertex("Zero"); 55 | insertVertex("One"); 56 | insertVertex("Two"); 57 | insertVertex("Three"); 58 | insertVertex("Four"); 59 | insertVertex("Five"); 60 | insertVertex("Six"); 61 | insertVertex("Seven"); 62 | insertVertex("Eight"); 63 | insertVertex("Nine"); 64 | 65 | insertEdge("Zero", "One", 19); 66 | insertEdge("Zero", "Three", 14); 67 | insertEdge("Zero", "Four", 12); 68 | insertEdge("One", "Two", 20); 69 | insertEdge("One", "Four", 18); 70 | insertEdge("Two", "Four", 17); 71 | insertEdge("Two", "Five", 15); 72 | insertEdge("Two", "Nine", 29); 73 | insertEdge("Three", "Four", 13); 74 | insertEdge("Three", "Six", 28); 75 | insertEdge("Four", "Five", 16); 76 | insertEdge("Four", "Six", 21); 77 | insertEdge("Four", "Seven", 22); 78 | insertEdge("Four", "Eight", 24); 79 | insertEdge("Five", "Eight", 26); 80 | insertEdge("Five", "Nine", 27); 81 | insertEdge("Six", "Seven", 23); 82 | insertEdge("Seven", "Eight", 30); 83 | insertEdge("Eight", "Nine", 35); 84 | 85 | kruskals(); 86 | } 87 | 88 | void kruskals() 89 | { 90 | struct QueueNode *front = NULL; 91 | struct Edge e; 92 | 93 | int u, v, v1, v2, r1 ,r2, edgesInTree,wtTree; 94 | 95 | for(u = 0; u < n; u++) 96 | for (v = 0; v < u; v++) 97 | if( adj[u][v] != 0 ) 98 | { 99 | e.u = u; 100 | e.v = v; 101 | e.weight = adj[u][v]; 102 | insert(e); 103 | } 104 | 105 | for (v = 0; v < n; v++) 106 | vertexList[v].father = NIL; 107 | 108 | r1 = NIL; /* root of the tree to which vertex v1 belongs */ 109 | r2 = NIL; /* root of the tree to which vertex v2 belongs */ 110 | edgesInTree = 0; 111 | wtTree = 0; 112 | 113 | while( !isEmpty() && edgesInTree < n-1 ) 114 | { 115 | e = Delete(); 116 | v1 = e.u; 117 | v2 = e.v; 118 | 119 | v = v1; 120 | while(vertexList[v].father!=NIL) 121 | v = vertexList[v].father; 122 | r1 = v; 123 | 124 | v = v2; 125 | while(vertexList[v].father!=NIL) 126 | v = vertexList[v].father; 127 | r2 = v; 128 | 129 | if(r1!=r2) /*Edge (v1,v2) is included*/ 130 | { 131 | edgesInTree++; 132 | printf("(%s, %s) \n",vertexList[v1].name,vertexList[v2].name); 133 | wtTree += e.weight; 134 | vertexList[r2].father = r1; 135 | } 136 | } 137 | 138 | if(edgesInTree < n-1) 139 | printf("Graph is not connected, no spanning tree possible\n"); 140 | else 141 | printf("Weight of Minimum Spanning Tree is %d\n",wtTree); 142 | } 143 | 144 | void display() 145 | { 146 | int i,j; 147 | 148 | printf("Adjacency Matrix - \n"); 149 | for(i=0; iedge = e; 261 | 262 | /*Queue is empty or edge to be added has weight less than first edge*/ 263 | if( front == NULL || temp->edge.weight < front->edge.weight ) 264 | { 265 | temp->link = front; 266 | front = temp; 267 | } 268 | else 269 | { 270 | p = front; 271 | while( p->link != NULL && p->link->edge.weight <= temp->edge.weight ) 272 | p = p->link; 273 | temp->link = p->link; 274 | p->link = temp; 275 | } 276 | } 277 | 278 | /*Delete an edge from the linked priority queue*/ 279 | struct Edge Delete() 280 | { 281 | struct QueueNode *temp; 282 | struct Edge e; 283 | if(isEmpty()) 284 | { 285 | printf("Queue Underflow\n"); 286 | exit(1); 287 | } 288 | temp = front; 289 | front = front->link; 290 | e = temp->edge; 291 | free(temp); 292 | return e; 293 | } 294 | 295 | int isEmpty() 296 | { 297 | if(front==NULL) 298 | return 1; 299 | else 300 | return 0; 301 | } 302 | 303 | 304 | -------------------------------------------------------------------------------- /Graph/breadth-first-search/BFS-shortest-path.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | /* Shortest distance and shortest path of a vertex from start vertex using BFS*/ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define INITIAL 0 13 | #define WAITING 1 14 | #define VISITED 2 15 | 16 | #define INFINITY 99999 17 | 18 | #define MAX 30 19 | 20 | struct Vertex 21 | { 22 | char name[50]; 23 | int state; 24 | int distance; 25 | int predecessor; 26 | }; 27 | 28 | struct Vertex vertexList[MAX]; 29 | 30 | int adj[MAX][MAX]; /* Adjacency Matrix */ 31 | int n=0; /* Number of vertices in the graph */ 32 | int e=0; /* Number of edges in the graph */ 33 | 34 | void display(); 35 | int getIndex(char s[]); 36 | int isAdjacent(char s1[], char s2[]); 37 | void insertVertex(char s[]); 38 | void deleteVertex(char s[]); 39 | void insertEdge(char s1[], char s2[]); 40 | void deleteEdge(char s1[], char s2[] ); 41 | 42 | void findShortestPath(char s[]); 43 | void bfs(int v); 44 | 45 | int queue[MAX]; 46 | int front,rear; 47 | void initializeQueue(); 48 | int isFull(); 49 | int isEmpty(); 50 | void enqueue(int x); 51 | int dequeue(); 52 | 53 | main() 54 | { 55 | insertVertex("Zero"); 56 | insertVertex("One"); 57 | insertVertex("Two"); 58 | insertVertex("Three"); 59 | insertVertex("Four"); 60 | insertVertex("Five"); 61 | insertVertex("Six"); 62 | insertVertex("Seven"); 63 | insertVertex("Eight"); 64 | insertVertex("Nine"); 65 | 66 | insertEdge("Zero", "One"); 67 | insertEdge("Zero", "Three"); 68 | insertEdge("One", "Two"); 69 | insertEdge("One", "Four"); 70 | insertEdge("One", "Five"); 71 | insertEdge("Two", "Three"); 72 | insertEdge("Two", "Five"); 73 | insertEdge("Three", "Six"); 74 | insertEdge("Four", "Five"); 75 | insertEdge("Four", "Seven"); 76 | insertEdge("Five", "Six"); 77 | insertEdge("Five", "Eight"); 78 | insertEdge("Six", "Eight"); 79 | insertEdge("Six", "Nine"); 80 | insertEdge("Seven", "Eight"); 81 | insertEdge("Eight", "Nine"); 82 | 83 | findShortestPath("Two"); 84 | } 85 | 86 | void findShortestPath(char s[]) 87 | { 88 | int i,u,v,x,y,count,path[MAX]; 89 | 90 | u = getIndex(s); 91 | if(u == -1) 92 | return; 93 | 94 | for(v = 0; v < n; v++) 95 | { 96 | vertexList[v].state = INITIAL; 97 | vertexList[v].predecessor = -1; 98 | vertexList[v].distance = INFINITY; 99 | } 100 | 101 | bfs(u); 102 | 103 | for(v = 0; v < n; v++) 104 | { 105 | if(vertexList[v].distance == INFINITY ) 106 | printf("No path from vertex %s to vertex %s\n",s,vertexList[v].name); 107 | else 108 | { 109 | printf("Shortest distance from vertex %s to vertex %s is %d\n", s,vertexList[v].name,vertexList[v].distance); 110 | 111 | /*Store the full path in array path*/ 112 | count = 0; 113 | y=v; 114 | while ( y != -1 ) 115 | { 116 | count++; 117 | path[count] = y; 118 | x = vertexList[y].predecessor; 119 | y = x; 120 | } 121 | printf("Shortest Path is : "); 122 | for(i = count; i > 1; i-- ) 123 | printf("%s ->", vertexList[path[i]].name); 124 | printf(vertexList[path[i]].name ); 125 | printf("\n"); 126 | } 127 | } 128 | } 129 | 130 | void bfs(int v) 131 | { 132 | int i; 133 | initializeQueue(); 134 | enqueue(v); 135 | vertexList[v].state = WAITING; 136 | vertexList[v].distance = 0; 137 | vertexList[v].predecessor = -1; 138 | 139 | while( !isEmpty() ) 140 | { 141 | v = dequeue(); 142 | vertexList[v].state = VISITED; 143 | 144 | for (i = 0; i 9 | #include 10 | #include 11 | 12 | struct EdgeNode; 13 | 14 | struct VertexNode 15 | { 16 | char name[50]; 17 | struct VertexNode *nextVertex; 18 | struct EdgeNode *firstEdge; 19 | }; 20 | 21 | struct VertexNode *start = NULL; 22 | 23 | struct EdgeNode 24 | { 25 | struct VertexNode *endVertex; 26 | struct EdgeNode *nextEdge; 27 | }; 28 | 29 | struct VertexNode *findVertex(char s[]); 30 | void insertVertex(char s[]); 31 | void insertEdge(char s1[],char s2[]); 32 | void deleteEdge(char s1[],char s2[]); 33 | void deletefromEdgeLists(char s[]); 34 | void deletefromVertexList(char s[]); 35 | void deleteVertex(char s[]); 36 | void display(); 37 | int isAdjacent(char s1[], char s2[]); 38 | int outdegree(char s[]); 39 | int indegree(char s[]); 40 | 41 | main() 42 | { 43 | int choice,x; 44 | char s1[50],s2[50]; 45 | 46 | insertVertex("AA"); 47 | insertVertex("BB"); 48 | insertVertex("CC"); 49 | insertVertex("DD"); 50 | insertVertex("EE"); 51 | 52 | insertEdge("AA","BB"); 53 | insertEdge("AA","CC"); 54 | insertEdge("CC","DD"); 55 | insertEdge("DD","AA"); 56 | insertEdge("CC","AA"); 57 | insertEdge("BB","EE"); 58 | 59 | while(1) 60 | { 61 | printf("1.Display Adjacency List\n"); 62 | printf("2.Insert a vertex\n"); 63 | printf("3.Remove a vertex\n"); 64 | printf("4.Insert an edge\n"); 65 | printf("5.Delete an edge\n"); 66 | printf("6.Display Indegree and outdegree of a vertex\n"); 67 | printf("7.Check if there is an edge between two vertices\n"); 68 | printf("8.Exit\n"); 69 | printf("Enter your choice : "); 70 | scanf("%d",&choice); 71 | 72 | switch(choice) 73 | { 74 | case 1: 75 | display(); 76 | break; 77 | case 2: 78 | printf("Enter vertex name : "); 79 | scanf("%s",s1); 80 | insertVertex(s1); 81 | break; 82 | case 3: 83 | printf("Enter vertex name : "); 84 | scanf("%s",s1); 85 | deleteVertex(s1); 86 | break; 87 | case 4: 88 | printf("Enter start and end vertices : "); 89 | scanf("%s",s1); 90 | scanf("%s",s2); 91 | insertEdge(s1,s2); 92 | break; 93 | case 5: 94 | printf("Enter start and end vertices : "); 95 | scanf("%s",s1); 96 | scanf("%s",s2); 97 | deleteEdge(s1,s2); 98 | break; 99 | case 6: 100 | printf("Enter vertex name : "); 101 | scanf("%s",s1); 102 | x = indegree(s1); 103 | if ( x == -1 ) 104 | printf("Vertex name not present in the graph\n\n"); 105 | else 106 | { 107 | printf("Indegree is : %d\n\n",x ); 108 | printf("Outdegree is : %d\n\n", outdegree(s1)); 109 | } 110 | break; 111 | case 7: 112 | printf("Enter the name of first vertex : "); 113 | scanf("%s",s1); 114 | printf("Enter the name of second vertex : "); 115 | scanf("%s",s2); 116 | x = isAdjacent(s1,s2); 117 | if( x == -1 ) 118 | printf("Vertex name not present in the graph\n\n"); 119 | else if ( x == 0 ) 120 | printf("There is no edge from %s to %s\n\n",s1,s2); 121 | else 122 | printf("There is an edge from %s to %s\n\n",s1,s2); 123 | break; 124 | case 8: 125 | exit(1); 126 | default: 127 | printf("Wrong choice\n\n"); 128 | break; 129 | } 130 | } 131 | } 132 | 133 | void insertVertex(char s[]) 134 | { 135 | struct VertexNode *temp,*p; 136 | 137 | if(start == NULL) 138 | { 139 | temp = (struct VertexNode *)malloc(sizeof(struct VertexNode)); 140 | strcpy(temp->name,s); 141 | temp->nextVertex = NULL; 142 | temp->firstEdge = NULL; 143 | start = temp; 144 | return; 145 | } 146 | 147 | p = start; 148 | while( p->nextVertex!=NULL ) 149 | { 150 | if( strcmp(p->name,s)==0) 151 | { 152 | printf("Vertex already present\n\n"); 153 | return; 154 | } 155 | p = p->nextVertex; 156 | } 157 | if( strcmp(p->name,s)==0) 158 | { 159 | printf("Vertex already present\n\n"); 160 | return; 161 | } 162 | temp = (struct VertexNode *)malloc(sizeof(struct VertexNode)); 163 | strcpy(temp->name,s); 164 | temp->nextVertex = NULL; 165 | temp->firstEdge = NULL; 166 | p->nextVertex = temp; 167 | } 168 | 169 | struct VertexNode *findVertex(char s[]) 170 | { 171 | struct VertexNode *p = start; 172 | while(p!=NULL) 173 | { 174 | if( strcmp(p->name,s)==0 ) 175 | return p; 176 | p = p->nextVertex; 177 | } 178 | return NULL; 179 | } 180 | 181 | void deleteVertex(char s[]) 182 | { 183 | deletefromEdgeLists(s); 184 | deletefromVertexList(s); 185 | } 186 | 187 | void deletefromVertexList(char s[]) 188 | { 189 | struct VertexNode *temp,*p; 190 | struct EdgeNode *ptr,*temporary; 191 | 192 | if(start == NULL) 193 | { 194 | printf("No vertices to be deleted\n\n"); 195 | return; 196 | } 197 | if( strcmp(start->name,s) == 0)/* Vertex to be deleted is first vertex of list*/ 198 | { 199 | temp = start; 200 | start = start->nextVertex; 201 | } 202 | else /* Vertex to be deleted is in between or at last */ 203 | { 204 | p = start; 205 | while(p->nextVertex != NULL) 206 | { 207 | if( strcmp( p->nextVertex->name,s)==0 ) 208 | break; 209 | p = p->nextVertex; 210 | } 211 | 212 | if(p->nextVertex==NULL) 213 | { 214 | printf("Vertex not found\n\n"); 215 | return; 216 | } 217 | else 218 | { 219 | temp = p->nextVertex; 220 | p->nextVertex = temp->nextVertex; 221 | } 222 | } 223 | 224 | /*Before freeing the node temp, free all edges going from this vertex */ 225 | ptr = temp->firstEdge; 226 | while(ptr!=NULL) 227 | { 228 | temporary = ptr; 229 | ptr = ptr->nextEdge; 230 | free(temporary); 231 | } 232 | free(temp); 233 | } 234 | 235 | void deletefromEdgeLists(char s[]) 236 | { 237 | struct VertexNode *p; 238 | struct EdgeNode *q,*temp; 239 | 240 | for( p = start; p!=NULL; p = p->nextVertex ) 241 | { 242 | if(p->firstEdge == NULL) 243 | continue; 244 | 245 | if( strcmp(p->firstEdge->endVertex->name,s) == 0) 246 | { 247 | temp = p->firstEdge; 248 | p->firstEdge = p->firstEdge->nextEdge; 249 | free(temp); 250 | } 251 | else 252 | { 253 | q = p->firstEdge; 254 | while(q->nextEdge!= NULL) 255 | { 256 | if( strcmp(q->nextEdge->endVertex->name,s) == 0 ) 257 | { 258 | temp = q->nextEdge; 259 | q->nextEdge = temp->nextEdge; 260 | free(temp); 261 | break; 262 | } 263 | q = q->nextEdge; 264 | } 265 | } 266 | } 267 | } 268 | 269 | void insertEdge(char s1[], char s2[]) 270 | { 271 | struct VertexNode *u,*v; 272 | struct EdgeNode *p,*temp; 273 | 274 | if( strcmp(s1,s2)==0 ) 275 | { 276 | printf("Inavlid Edge : Start and end vertices are same\n\n"); 277 | return; 278 | } 279 | 280 | u = findVertex(s1); 281 | v = findVertex(s2); 282 | 283 | if( u == NULL ) 284 | { 285 | printf("Start vertex not present, first insert vertex %s\n\n",s1); 286 | return; 287 | } 288 | if(v == NULL ) 289 | { 290 | printf("End vertex not present, first insert vertex %s\n",s2); 291 | return; 292 | } 293 | 294 | 295 | temp = (struct EdgeNode *)malloc(sizeof(struct EdgeNode)); 296 | temp->endVertex = v; 297 | temp->nextEdge = NULL; 298 | 299 | if(u->firstEdge == NULL) 300 | u->firstEdge = temp; 301 | else 302 | { 303 | p = u->firstEdge; 304 | while(p->nextEdge!=NULL) 305 | { 306 | if( strcmp(p->endVertex->name,s2)==0 ) 307 | { 308 | printf("Edge present\n\n"); 309 | return; 310 | } 311 | p = p->nextEdge; 312 | } 313 | if( strcmp(p->endVertex->name,s2)==0 ) 314 | { 315 | printf("Edge present\n\n"); 316 | return; 317 | } 318 | p->nextEdge = temp; 319 | } 320 | } 321 | 322 | void deleteEdge(char s1[], char s2[]) 323 | { 324 | struct VertexNode *u; 325 | struct EdgeNode *temp,*q; 326 | 327 | u = findVertex(s1); 328 | 329 | if(u == NULL ) 330 | { 331 | printf("Start vertex not present\n\n"); 332 | return; 333 | } 334 | if(u->firstEdge == NULL) 335 | { 336 | printf("Edge not present\n"); 337 | return; 338 | } 339 | 340 | if( strcmp( u->firstEdge->endVertex->name , s2 ) ==0 ) 341 | { 342 | temp = u->firstEdge; 343 | u->firstEdge = u->firstEdge->nextEdge; 344 | free(temp); 345 | return; 346 | } 347 | q = u->firstEdge; 348 | while(q->nextEdge != NULL) 349 | { 350 | if( strcmp( q->nextEdge->endVertex->name,s2)==0 ) 351 | { 352 | temp = q->nextEdge; 353 | q->nextEdge = temp->nextEdge; 354 | free(temp); 355 | return; 356 | } 357 | q = q->nextEdge; 358 | } 359 | printf("Edge not present in the graph\n\n"); 360 | } 361 | 362 | void display() 363 | { 364 | struct VertexNode *p; 365 | struct EdgeNode *q; 366 | 367 | for(p = start; p!=NULL; p = p->nextVertex) 368 | { 369 | printf("%s ->",p->name); 370 | for ( q = p->firstEdge; q!=NULL;q = q->nextEdge) 371 | printf(" %s",q->endVertex->name); 372 | printf("\n"); 373 | } 374 | } 375 | 376 | /* If edge (s1,s2) exists */ 377 | int isAdjacent(char s1[], char s2[]) 378 | { 379 | struct VertexNode *u = findVertex(s1); 380 | struct EdgeNode *q; 381 | 382 | if (u == NULL) 383 | { 384 | printf("Vertex not present\n\n"); 385 | return 0; 386 | } 387 | 388 | for( q=u->firstEdge; q != NULL; q = q->nextEdge ) 389 | if( strcmp(q->endVertex->name, s2)==0 ) 390 | return 1; 391 | return 0; 392 | } 393 | 394 | /*Returns number of edges going out from vertex s*/ 395 | int outdegree(char s[]) 396 | { 397 | int outd; 398 | struct VertexNode *u = findVertex(s); 399 | struct EdgeNode *q; 400 | 401 | if (u == NULL) 402 | { 403 | printf("Vertex not present\n\n"); 404 | return -1; 405 | } 406 | 407 | outd = 0; 408 | for( q = u->firstEdge; q != NULL; q = q->nextEdge ) 409 | outd++; 410 | return outd; 411 | } 412 | 413 | /*Returns number of edges coming to vertex s*/ 414 | int indegree(char s[]) 415 | { 416 | int ind; 417 | struct VertexNode *p; 418 | struct EdgeNode *q; 419 | struct VertexNode *u = findVertex(s); 420 | 421 | if( u == NULL ) 422 | { 423 | printf("Vertex not present\n\n"); 424 | return -1; 425 | } 426 | 427 | ind = 0; 428 | for(p=start; p != NULL; p = p->nextVertex) 429 | { 430 | for(q = p->firstEdge; q != NULL; q = q->nextEdge) 431 | if( q->endVertex == u ) 432 | ind++; 433 | } 434 | return ind; 435 | } -------------------------------------------------------------------------------- /Trees/avl-tree.C: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | #define FALSE 0 10 | #define TRUE 1 11 | 12 | struct node 13 | { 14 | struct node *lchild; 15 | int info; 16 | struct node *rchild; 17 | short int balance; 18 | }; 19 | 20 | void display(struct node *p, int level); 21 | void inorder(struct node *p); 22 | 23 | struct node *rotateLeft(struct node *p); 24 | struct node *rotateRight(struct node *p); 25 | 26 | struct node *getNode(int x); 27 | struct node *insertNode(struct node *p, int x); 28 | struct node *insertionLeftSubtreeCheck(struct node *p, int *taller); 29 | struct node *insertionRightSubtreeCheck(struct node *p, int *taller); 30 | struct node *insertionLeftBalance(struct node *p); 31 | struct node *insertionRightBalance(struct node *p); 32 | 33 | struct node *deleteNode(struct node *p, int x); 34 | struct node *deletionLeftSubtreeCheck(struct node *p, int *shorter); 35 | struct node *deletionRightSubtreeCheck(struct node *p, int *shorter); 36 | struct node *deletionLeftBalance(struct node *p,int *shorter); 37 | struct node *deletionRightBalance(struct node *p,int *shorter); 38 | 39 | 40 | main() 41 | { 42 | int choice,key; 43 | struct node *root = NULL; 44 | 45 | while(1) 46 | { 47 | printf("\n"); 48 | printf("1.Display Tree\n"); 49 | printf("2.Insert a new node\n"); 50 | printf("3.Delete a node\n"); 51 | printf("4.Inorder Traversal\n"); 52 | printf("5.Quit\n"); 53 | 54 | printf("Enter your choice : "); 55 | scanf("%d",&choice); 56 | 57 | switch(choice) 58 | { 59 | case 1: 60 | display(root,0); 61 | break; 62 | case 2: 63 | printf("Enter the key to be inserted : "); 64 | scanf("%d",&key); 65 | root = insertNode(root,key); 66 | break; 67 | case 3: 68 | printf("Enter the key to be deleted : "); 69 | scanf("%d",&key); 70 | root = deleteNode(root,key); 71 | break; 72 | case 4: 73 | inorder(root); 74 | break; 75 | case 5: 76 | exit(1); 77 | default: 78 | printf("Wrong choice\n"); 79 | } 80 | } 81 | } 82 | 83 | void display(struct node *p, int level) 84 | { 85 | int i; 86 | if( p == NULL ) 87 | return; 88 | 89 | display(p->rchild, level+1); 90 | printf("\n"); 91 | 92 | for(i=0; iinfo); 95 | display(p->lchild, level+1); 96 | } 97 | 98 | void inorder(struct node *p) 99 | { 100 | if(p == NULL ) 101 | return; 102 | inorder(p->lchild); 103 | printf("%d ",p->info); 104 | inorder(p->rchild); 105 | } 106 | 107 | struct node *rotateLeft(struct node *p) 108 | { 109 | struct node *a; 110 | a = p->rchild; 111 | p->rchild = a->lchild; 112 | a->lchild = p; 113 | return a; 114 | } 115 | 116 | struct node *rotateRight(struct node *p) 117 | { 118 | struct node *a; 119 | a = p->lchild; 120 | p->lchild = a->rchild; 121 | a->rchild = p; 122 | return a; 123 | } 124 | 125 | struct node *getNode(int x) 126 | { 127 | struct node *p = (struct node *) malloc(sizeof(struct node)); 128 | p->info = x; 129 | p->lchild = NULL; 130 | p->rchild = NULL; 131 | p->balance = 0; 132 | return p; 133 | } 134 | 135 | struct node *insertNode(struct node *p, int x) 136 | { 137 | static int taller; 138 | if( p == NULL ) 139 | { 140 | p = getNode(x); 141 | taller = TRUE; 142 | } 143 | else if(x < p->info) 144 | { 145 | p->lchild = insertNode(p->lchild, x); 146 | if( taller == TRUE ) 147 | p = insertionLeftSubtreeCheck( p, &taller ); 148 | } 149 | else if(x > p->info) 150 | { 151 | p->rchild = insertNode(p->rchild, x); 152 | if( taller == TRUE ) 153 | p = insertionRightSubtreeCheck( p, &taller ); 154 | } 155 | else 156 | { 157 | printf("%d already present in tree\n", x); 158 | taller = FALSE; 159 | } 160 | return p; 161 | } 162 | 163 | struct node *insertionLeftSubtreeCheck( struct node *p, int *taller ) 164 | { 165 | switch(p->balance) 166 | { 167 | case 0: /* Case L_1 : was balanced */ 168 | p->balance = 1; /* now left heavy */ 169 | break; 170 | case -1: /* Case L_2: was right heavy */ 171 | p->balance = 0; /* now balanced */ 172 | *taller = FALSE; 173 | break; 174 | case 1: /* Case L_3: was left heavy */ 175 | p = insertionLeftBalance(p); /* Left Balancing */ 176 | *taller = FALSE; 177 | } 178 | return p; 179 | } 180 | 181 | struct node *insertionRightSubtreeCheck(struct node *p, int *taller ) 182 | { 183 | switch(p->balance) 184 | { 185 | case 0: /* Case R_1 : was balanced */ 186 | p->balance = -1; /* now right heavy */ 187 | break; 188 | case 1: /* Case R_2 : was left heavy */ 189 | p->balance = 0; /* now balanced */ 190 | *taller = FALSE; 191 | break; 192 | case -1: /* Case R_3: Right heavy */ 193 | p = insertionRightBalance(p); /* Right Balancing */ 194 | *taller = FALSE; 195 | } 196 | return p; 197 | } 198 | 199 | struct node *insertionLeftBalance(struct node *p) 200 | { 201 | struct node *a, *b; 202 | 203 | a = p->lchild; 204 | if(a->balance == 1) /* Case L_3A : Insertion in AL */ 205 | { 206 | p->balance = 0; 207 | a->balance = 0; 208 | p = rotateRight(p); 209 | } 210 | else /* Case L_3B : Insertion in AR */ 211 | { 212 | b = a->rchild; 213 | switch(b->balance) 214 | { 215 | case 1: /* Case L_3B1 : Insertion in BL */ 216 | p->balance = -1; 217 | a->balance = 0; 218 | break; 219 | case -1: /* Case L_3B2 : Insertion in BR */ 220 | p->balance = 0; 221 | a->balance = 1; 222 | break; 223 | case 0: /* Case L_3B3 : B is the newly inserted node */ 224 | p->balance = 0; 225 | a->balance = 0; 226 | } 227 | b->balance = 0; 228 | p->lchild = rotateLeft(a); 229 | p = rotateRight(p); 230 | } 231 | return p; 232 | } 233 | 234 | struct node *insertionRightBalance(struct node *p) 235 | { 236 | struct node *a, *b; 237 | 238 | a = p->rchild; 239 | if(a->balance == -1) /* Case R_3A : Insertion in AR */ 240 | { 241 | p->balance = 0; 242 | a->balance = 0; 243 | p = rotateLeft(p); 244 | } 245 | else /* Case R_3B : Insertion in AL */ 246 | { 247 | b = a->lchild; 248 | switch(b->balance) 249 | { 250 | case -1: /* Case R_3B1 : Insertion in BR */ 251 | p->balance = 1; 252 | a->balance = 0; 253 | break; 254 | case 1: /* Case R_3B2 : Insertion in BL */ 255 | p->balance = 0; 256 | a->balance = -1; 257 | break; 258 | case 0: /* Case R_3B3 : B is the newly inserted node */ 259 | p->balance = 0; 260 | a->balance = 0; 261 | } 262 | b->balance = 0; 263 | p->rchild = rotateRight(a); 264 | p = rotateLeft(p); 265 | } 266 | return p; 267 | } 268 | 269 | struct node *deleteNode(struct node *p, int x) 270 | { 271 | struct node *ch, *s; 272 | static int shorter; 273 | 274 | if( p == NULL) 275 | { 276 | printf("%d not found\n",x); 277 | shorter = FALSE; 278 | return p; 279 | } 280 | if( x < p->info ) /* delete from left subtree */ 281 | { 282 | p->lchild = deleteNode(p->lchild, x); 283 | if(shorter == TRUE) 284 | p = deletionLeftSubtreeCheck(p, &shorter); 285 | } 286 | else if( x > p->info ) /* delete from right subtree */ 287 | { 288 | p->rchild = deleteNode(p->rchild, x); 289 | if(shorter==TRUE) 290 | p = deletionRightSubtreeCheck(p, &shorter); 291 | } 292 | else 293 | { 294 | /* key to be deleted is found */ 295 | if( p->lchild!=NULL && p->rchild!=NULL ) /*2 children*/ 296 | { 297 | s = p->rchild; 298 | while(s->lchild) 299 | s = s->lchild; 300 | p->info = s->info; 301 | p->rchild = deleteNode(p->rchild, s->info); 302 | if( shorter == TRUE ) 303 | p = deletionRightSubtreeCheck(p, &shorter); 304 | } 305 | else /* 1 child or no child */ 306 | { 307 | if( p->lchild != NULL ) /* only left child */ 308 | ch = p->lchild; 309 | else /* only right child or no child */ 310 | ch = p->rchild; 311 | free(p); 312 | p = ch; 313 | shorter = TRUE; 314 | } 315 | } 316 | return p; 317 | } 318 | 319 | struct node *deletionLeftSubtreeCheck(struct node *p, int *shorter) 320 | { 321 | switch(p->balance) 322 | { 323 | case 0: /* Case L_1 : was balanced */ 324 | p->balance = -1; /* now right heavy */ 325 | *shorter = FALSE; 326 | break; 327 | case 1: /* Case L_2 : was left heavy */ 328 | p->balance = 0; /* now balanced */ 329 | break; 330 | case -1: /* Case L_3 : was right heavy */ 331 | p = deletionRightBalance(p, shorter); /* Right Balancing */ 332 | } 333 | return p; 334 | } 335 | 336 | struct node *deletionRightSubtreeCheck(struct node *p, int *shorter) 337 | { 338 | switch(p->balance) 339 | { 340 | case 0: /* Case R_1 : was balanced */ 341 | p->balance = 1; /* now left heavy */ 342 | *shorter = FALSE; 343 | break; 344 | case -1: /* Case R_2 : was right heavy */ 345 | p->balance = 0; /* now balanced */ 346 | break; 347 | case 1: /* Case R_3 : was left heavy */ 348 | p = deletionLeftBalance(p, shorter ); /*Left Balancing*/ 349 | } 350 | return p; 351 | } 352 | 353 | struct node *deletionLeftBalance(struct node *p,int *shorter) 354 | { 355 | struct node *a, *b; 356 | a = p->lchild; 357 | if( a->balance == 0) /* Case R_3A */ 358 | { 359 | p->balance = 1; 360 | a->balance = -1; 361 | *shorter = FALSE; 362 | p = rotateRight(p); 363 | } 364 | else if(a->balance == 1 ) /* Case R_3B */ 365 | { 366 | p->balance = 0; 367 | a->balance = 0; 368 | p = rotateRight(p); 369 | } 370 | else /* Case R_3C */ 371 | { 372 | b = a->rchild; 373 | switch(b->balance) 374 | { 375 | case 0: /* Case R_3C1 */ 376 | p->balance = 0; 377 | a->balance = 0; 378 | break; 379 | case -1: /* Case R_3C2 */ 380 | p->balance = 0; 381 | a->balance = 1; 382 | case 1: /* Case R_3C3 */ 383 | p->balance = -1; 384 | a->balance = 0; 385 | break; 386 | 387 | } 388 | b->balance = 0; 389 | p->lchild = rotateLeft(a); 390 | p = rotateRight(p); 391 | } 392 | return p; 393 | } 394 | 395 | struct node *deletionRightBalance(struct node *p,int *shorter) 396 | { 397 | struct node *a, *b; 398 | 399 | a = p->rchild; 400 | if (a->balance == 0) /* Case L_3A */ 401 | { 402 | p->balance = -1; 403 | a->balance = 1; 404 | *shorter = FALSE; 405 | p = rotateLeft(p); 406 | } 407 | else if(a->balance == -1 ) /* Case L_3B */ 408 | { 409 | p->balance = 0; 410 | a->balance = 0; 411 | p = rotateLeft(p); 412 | } 413 | else /* Case L_3C */ 414 | { 415 | b = a->lchild; 416 | switch(b->balance) 417 | { 418 | case 0: /* Case L_3C1 */ 419 | p->balance = 0; 420 | a->balance = 0; 421 | break; 422 | case 1: /* Case L_3C2 */ 423 | p->balance = 0; 424 | a->balance = -1; 425 | break; 426 | case -1: /* Case L_3C3 */ 427 | p->balance = 1; 428 | a->balance = 0; 429 | } 430 | b->balance = 0; 431 | p->rchild = rotateRight(a); 432 | p = rotateLeft(p); 433 | } 434 | return p; 435 | } 436 | 437 | -------------------------------------------------------------------------------- /Trees/b-tree.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) Deepali Srivastava - All Rights Reserved 3 | This code is part of DSA course available on CourseGalaxy.com 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | #define FALSE 0 10 | #define TRUE 1 11 | 12 | #define M 5 /*order of B tree*/ 13 | #define MAX (M-1) /*Maximum number of keys allowed in a node*/ 14 | 15 | #if M%2==0 16 | #define CEIL_MDIV2 (M/2) 17 | #else 18 | #define CEIL_MDIV2 ((M+1)/2) 19 | #endif 20 | 21 | #define MIN (CEIL_MDIV2-1) /*Minimum number of keys allowed in a node except root*/ 22 | 23 | struct node 24 | { 25 | int numKeys; 26 | int key[M]; 27 | struct node *child[M]; 28 | }; 29 | 30 | void inorder(struct node *p); 31 | void display(struct node *p,int blanks); 32 | 33 | struct node *search(int x,struct node *p); 34 | int searchNode(int x,struct node *p,int *n); 35 | 36 | struct node *insert(int x,struct node *root); 37 | int recInsert(int x, struct node *p, int *iKey, struct node **iKeyRchild); 38 | 39 | void insertByShift( struct node *p, int n, int iKey, struct node *iKeyRchild ); 40 | void split(struct node *p, int n, int *iKey, struct node **iKeyRchild); 41 | 42 | struct node *Delete(int x,struct node *root); 43 | void recDelete(int x,struct node *p); 44 | void deleteByShift(struct node *p,int n); 45 | void restore(struct node *p,int n); 46 | void borrowLeft(struct node *p,int n); 47 | void borrowRight(struct node *p,int n); 48 | void combine(struct node *p,int m); 49 | 50 | int main() 51 | { 52 | struct node *root = NULL, *ptr; 53 | int x,choice,n; 54 | while(1) 55 | { 56 | printf("1.Search\n"); 57 | printf("2.Insert\n"); 58 | printf("3.Delete\n"); 59 | printf("4.Display tree\n"); 60 | printf("5.Inorder traversal\n"); 61 | printf("6.Quit\n"); 62 | printf("Enter your choice : "); 63 | scanf("%d",&choice); 64 | 65 | switch(choice) 66 | { 67 | case 1: 68 | printf("Enter the key to be searched : "); 69 | scanf("%d",&x); 70 | if( (ptr=search(x, root)) == NULL ) 71 | printf("Key not present in the tree\n"); 72 | else 73 | printf("Key present in node %p \n",ptr); 74 | break; 75 | case 2: 76 | printf("Enter the key to be inserted : "); 77 | scanf("%d",&x); 78 | root = insert(x, root); 79 | break; 80 | case 3: 81 | printf("Enter the key to be deleted : "); 82 | scanf("%d",&x); 83 | root = Delete(x, root); 84 | break; 85 | case 4: 86 | printf("\n\n"); 87 | display( root, 0 ); 88 | printf("\n\n"); 89 | break; 90 | case 5: 91 | inorder(root); 92 | printf("\n\n"); 93 | break; 94 | case 6: 95 | exit(1); 96 | default: 97 | printf("Wrong choice\n"); 98 | break; 99 | } 100 | } 101 | } 102 | 103 | void inorder(struct node *p) 104 | { 105 | int i; 106 | if( p == NULL ) 107 | return; 108 | 109 | for(i=0; inumKeys; i++) 110 | { 111 | inorder(p->child[i]); 112 | printf("%d\t",p->key[i+1] ); 113 | } 114 | inorder(p->child[i]); 115 | } 116 | 117 | void display(struct node *p, int blanks) 118 | { 119 | int i; 120 | if( p == NULL ) 121 | return; 122 | 123 | for(i=1; i<=blanks; i++) 124 | printf(" "); 125 | 126 | for(i=1; i<=p->numKeys; i++) 127 | printf("%d ",p->key[i]); 128 | 129 | printf("\n"); 130 | 131 | for(i=0; i<=p->numKeys; i++) 132 | display(p->child[i], blanks+10); 133 | } 134 | 135 | struct node *search(int x, struct node *p) 136 | { 137 | int n; 138 | 139 | if( p == NULL ) /* Key x not present in the tree */ 140 | return NULL; 141 | n=0; 142 | if( searchNode(x, p, &n ) ) /* Key x found in node p */ 143 | return p; 144 | 145 | return search( x, p->child[n]); /* Search in node p->child[n] */ 146 | } 147 | 148 | int searchNode(int x, struct node *p, int *n) 149 | { 150 | if( x < p->key[1] ) 151 | { 152 | *n = 0; 153 | return FALSE; 154 | } 155 | 156 | *n = p->numKeys; 157 | while( ( x < p->key[*n] ) && *n>1 ) 158 | (*n)--; 159 | 160 | if( x == p->key[*n] ) 161 | return TRUE; 162 | else 163 | return FALSE; 164 | } 165 | 166 | struct node *insert(int x, struct node *root) 167 | { 168 | int iKey=0, taller; 169 | struct node *iKeyRchild=NULL, *temp; 170 | 171 | taller = recInsert(x, root, &iKey, &iKeyRchild); 172 | 173 | if( taller ) /* Height increased by one, new root node has to be created */ 174 | { 175 | temp = (struct node *)malloc( sizeof(struct node) ); 176 | temp->numKeys = 1; 177 | temp->child[0] = root; 178 | temp->key[1] = iKey; 179 | temp->child[1] = iKeyRchild; 180 | root = temp; 181 | } 182 | return root; 183 | } 184 | 185 | int recInsert(int x, struct node *p, int *iKey, struct node **iKeyRchild) 186 | { 187 | int n,flag; 188 | 189 | if( p == NULL ) /* First Base case : key not found */ 190 | { 191 | *iKey = x; 192 | *iKeyRchild = NULL; 193 | return TRUE; 194 | } 195 | 196 | n = 0; 197 | if( searchNode( x, p, &n) ) /* Second Base Case : key found */ 198 | { 199 | printf("Duplicate keys are not allowed\n"); 200 | return FALSE; /* No need to insert the key */ 201 | } 202 | 203 | flag = recInsert( x, p->child[n], iKey, iKeyRchild ); 204 | 205 | if( flag ) 206 | { 207 | if( p->numKeys < MAX ) 208 | { 209 | insertByShift(p, n, *iKey, *iKeyRchild); 210 | return FALSE; /* Insertion over */ 211 | } 212 | else 213 | { 214 | split(p, n, iKey, iKeyRchild ); 215 | return TRUE; /* Insertion not over : Median key yet to be inserted */ 216 | } 217 | } 218 | return FALSE; 219 | } 220 | 221 | void insertByShift( struct node *p, int n, int iKey, struct node *iKeyRchild ) 222 | { 223 | int i; 224 | 225 | for( i = p->numKeys; i > n; i-- ) 226 | { 227 | p->key[i+1] = p->key[i]; 228 | p->child[i+1] = p->child[i]; 229 | } 230 | p->key[n+1] = iKey; 231 | p->child[n+1] = iKeyRchild; 232 | p->numKeys++; 233 | } 234 | 235 | void split(struct node *p, int n, int *iKey, struct node **iKeyRchild) 236 | { 237 | int i,j,medianKey, lastKey, d; 238 | struct node *lastChild, *newNode; 239 | 240 | if( n == MAX ) 241 | { 242 | lastKey = *iKey; 243 | lastChild = *iKeyRchild; 244 | } 245 | else 246 | { 247 | lastKey = p->key[MAX]; 248 | lastChild = p->child[MAX]; 249 | for( i = p->numKeys-1; i > n; i-- ) 250 | { 251 | p->key[i+1] = p->key[i]; 252 | p->child[i+1] = p->child[i]; 253 | } 254 | p->key[i+1] = *iKey; 255 | p->child[i+1] = *iKeyRchild; 256 | } 257 | 258 | 259 | d = CEIL_MDIV2; 260 | medianKey = p->key[d]; 261 | 262 | newNode = (struct node *)malloc(sizeof(struct node) ); 263 | newNode->numKeys = M - d; 264 | 265 | newNode->child[0] = p->child[d]; 266 | for( i=1, j=d+1; j<=MAX; i++,j++ ) 267 | { 268 | newNode->key[i] = p->key[j]; 269 | newNode->child[i] = p->child[j]; 270 | } 271 | 272 | newNode->key[i] = lastKey; 273 | newNode->child[i] = lastChild; 274 | 275 | p->numKeys = d-1; 276 | 277 | *iKey = medianKey; 278 | *iKeyRchild = newNode; 279 | } 280 | 281 | struct node *Delete(int x, struct node *root) 282 | { 283 | struct node *temp; 284 | 285 | if ( root == NULL ) 286 | { 287 | printf("Tree is empty"); 288 | return; 289 | } 290 | 291 | recDelete(x, root); 292 | 293 | if( root!=NULL && root->numKeys == 0 )/*Height of tree decreased by 1*/ 294 | { 295 | temp = root; 296 | root = root->child[0]; 297 | free(temp); 298 | } 299 | return root; 300 | } 301 | 302 | void recDelete(int x, struct node *p) 303 | { 304 | int n=0; 305 | struct node *s; 306 | 307 | if( searchNode(x, p, &n) ) /* Key x found in node p */ 308 | { 309 | if( p->child[n]==NULL )/*node p is a leaf node */ 310 | { 311 | deleteByShift( p, n); 312 | return; 313 | } 314 | else /*node p is a non leaf node */ 315 | { 316 | s = p->child[n]; /*point to the right subtree*/ 317 | while( s->child[0]!=NULL ) /*move down till leaf node arrives*/ 318 | s = s->child[0]; 319 | p->key[n] = s->key[1]; 320 | recDelete(s->key[1], p->child[n]); 321 | } 322 | } 323 | else /*key x not found in node p */ 324 | { 325 | if( p->child[n] == NULL )/*if p is a leaf node*/ 326 | { 327 | printf("Value %d not present in the tree\n", x); 328 | return; 329 | } 330 | /*p is a non-leaf node*/ 331 | recDelete(x, p->child[n]); 332 | } 333 | if( p->child[n]->numKeys < MIN ) /*check underflow in p->child[n]*/ 334 | restore( p, n); 335 | } 336 | 337 | 338 | void deleteByShift( struct node *p, int n) 339 | { 340 | int i; 341 | for( i = n+1; i <= p->numKeys; i++ ) 342 | { 343 | p->key[i-1] = p->key[i]; 344 | p->child[i-1] = p->child[i]; 345 | } 346 | p->numKeys--; 347 | } 348 | 349 | 350 | /* Called when p->child[n] becomes underflow */ 351 | void restore(struct node *p, int n) 352 | { 353 | if( n!=0 && p->child[n-1]->numKeys > MIN ) 354 | borrowLeft(p, n); 355 | else if( n!=p->numKeys && p->child[n+1]->numKeys > MIN ) 356 | borrowRight(p, n); 357 | else 358 | { 359 | if(n!=0) /*if there is a left sibling*/ 360 | combine(p, n); /*combine with left sibling*/ 361 | else 362 | combine(p, n+1); /*combine with right sibling*/ 363 | } 364 | } 365 | 366 | void borrowLeft(struct node *p, int n) 367 | { 368 | int i; 369 | struct node *underflowNode = p->child[n]; 370 | struct node *leftSibling = p->child[n-1]; /* left sibling of underflowNode */ 371 | 372 | underflowNode->numKeys++; 373 | 374 | /*Shift all the keys and children in underflowNode one position right*/ 375 | for(i = underflowNode->numKeys; i>0; i--) 376 | { 377 | underflowNode->key[i+1] = underflowNode->key[i]; 378 | underflowNode->child[i+1] = underflowNode->child[i]; 379 | } 380 | underflowNode->child[1] = underflowNode->child[0]; 381 | 382 | /* Move the separator key from parent node p to underflowNode */ 383 | underflowNode->key[1] = p->key[n]; 384 | 385 | /* Move the rightmost key of leftSibling to the parent node p */ 386 | p->key[n] = leftSibling->key[leftSibling->numKeys]; 387 | 388 | /*Rightmost child of leftSibling becomes leftmost child of underflowNode */ 389 | underflowNode->child[0] = leftSibling->child[leftSibling->numKeys]; 390 | 391 | leftSibling->numKeys--; 392 | } 393 | 394 | void borrowRight(struct node *p,int n) 395 | { 396 | int i; 397 | struct node *underflowNode = p->child[n]; 398 | struct node *rightSibling = p->child[n+1]; /* right sibling of underflowNode */ 399 | 400 | /* Move the separator key from parent node p to underflowNode */ 401 | underflowNode->numKeys++; 402 | underflowNode->key[underflowNode->numKeys] = p->key[n+1]; 403 | 404 | /* Leftmost child of node rightSibling becomes the rightmost child of underflowNode*/ 405 | underflowNode->child[underflowNode->numKeys] = rightSibling->child[0]; 406 | 407 | /*Move the leftmost key from node rightSibling to parent node p */ 408 | p->key[n+1] = rightSibling->key[1]; 409 | rightSibling->numKeys--; 410 | 411 | /*Shift all the keys and children of node rightSibling one position left*/ 412 | rightSibling->child[0] = rightSibling->child[1]; 413 | for(i=1; i<=rightSibling->numKeys; i++) 414 | { 415 | rightSibling->key[i] = rightSibling->key[i+1]; 416 | rightSibling->child[i] = rightSibling->child[i+1]; 417 | } 418 | } 419 | 420 | void combine(struct node *p, int m) 421 | { 422 | int i; 423 | struct node *nodeA = p->child[m-1]; 424 | struct node *nodeB = p->child[m]; 425 | 426 | nodeA->numKeys++; 427 | 428 | /* Move the key from the parent node p to nodeA */ 429 | nodeA->key[nodeA->numKeys] = p->key[m]; 430 | 431 | /* Shift the keys and children that are after separator key in node p one position left */ 432 | for(i=m; inumKeys; i++) 433 | { 434 | p->key[i] = p->key[i+1]; 435 | p->child[i] = p->child[i+1]; 436 | } 437 | p->numKeys--; 438 | 439 | /*Leftmost child of nodeB becomes rightmost child of nodeA */ 440 | nodeA->child[nodeA->numKeys] = nodeB->child[0]; 441 | 442 | /*Insert all the keys and children of nodeB at the end of nodeA */ 443 | for( i = 1; i<=nodeB->numKeys; i++) 444 | { 445 | nodeA->numKeys++; 446 | nodeA->key[nodeA->numKeys] = nodeB->key[i]; 447 | nodeA->child[nodeA->numKeys] = nodeB->child[i]; 448 | } 449 | free(nodeB); 450 | } 451 | --------------------------------------------------------------------------------