diff --git a/dsa1/assignment6/Matrix_1.txt b/dsa1/assignment6/Matrix_1.txt new file mode 100644 index 0000000..80d460c --- /dev/null +++ b/dsa1/assignment6/Matrix_1.txt @@ -0,0 +1,6 @@ +4 +3 +0 0 0 +1 0 2 +0 6 1 +7 0 0 \ No newline at end of file diff --git a/dsa1/assignment6/Matrix_2.txt b/dsa1/assignment6/Matrix_2.txt new file mode 100644 index 0000000..3eccf7a --- /dev/null +++ b/dsa1/assignment6/Matrix_2.txt @@ -0,0 +1,6 @@ +4 +3 +0 2 0 +-1 0 0 +0 -2 0 +0 0 4 \ No newline at end of file diff --git a/dsa1/assignment6/main.c b/dsa1/assignment6/main.c index 4ae579a..012bc39 100644 --- a/dsa1/assignment6/main.c +++ b/dsa1/assignment6/main.c @@ -22,10 +22,10 @@ int main(){ writeIntoFile("matrix4.txt", *s4); */ - readFromFile("Matrix1.txt", &s1); + readFromFile("Matrix_1.txt", &s1); display(s1); - readFromFile("Matrix2.txt", &s2); + readFromFile("Matrix_2.txt", &s2); display(s2); Sparse * s3 = add(s1, s2); diff --git a/dsa2/assignment5/logic.c b/dsa2/assignment5/logic.c index 760162a..cd12ff4 100644 --- a/dsa2/assignment5/logic.c +++ b/dsa2/assignment5/logic.c @@ -3,14 +3,19 @@ #include #include "header.h" +//---------------Utility function--------------- + void swap(int *a, int *b) { int temp = *a; + *a = *b; *b = temp; return; } +//---------------Max Heap Operations--------------- + int isEmptyMax(MaxHeap h) { return (h.rear == -1); @@ -19,16 +24,6 @@ int isFullMax(MaxHeap h) { return (h.rear == h.size - 1); } - -int isEmptyMin(MinHeap h) -{ - return (h.rear == -1); -} -int isFullMin(MinHeap h) -{ - return (h.rear == h.size - 1); -} - void initMax(MaxHeap *h, int size) { h->A = (int *)malloc(sizeof(int) * size); @@ -37,27 +32,8 @@ void initMax(MaxHeap *h, int size) return; } -void MaxHeapSort(int *arr, int size) -{ - MaxHeap h; - initMax(&h, size); - - for (int i = 0; i < size; i++) - { - insetMax(&h, arr[i]); - } - - int i = 0; - while (!isEmptyMax(h)) - { - arr[i] = removeMax(&h); - i++; - } - - return; -} - -void insetMax(MaxHeap *h, int value) +// Insert Element into Max Heap +void insertMax(MaxHeap *h, int value) { if (isFullMax(*h)) return; @@ -76,19 +52,7 @@ void insetMax(MaxHeap *h, int value) return; } -int removeMax(MaxHeap *h) -{ - if (isEmptyMax(*h)) - return INT_MIN; - - int removedElement = h->A[0]; - h->A[0] = h->A[h->rear]; - h->rear--; - maxHeapify(h, 0); - - return removedElement; -} - +// Creating Max Heap void maxHeapify(MaxHeap *h, int index) { int largest = index; @@ -110,35 +74,61 @@ void maxHeapify(MaxHeap *h, int index) return; } -void initMin(MinHeap *h, int size) +// Remove Max Element from Heap +int removeMax(MaxHeap *h) { - h->A = (int *)malloc(sizeof(int) * size); - h->size = size; - h->rear = -1; - return; + if (isEmptyMax(*h)) + return INT_MIN; + + int removedElement = h->A[0]; + h->A[0] = h->A[h->rear]; + h->rear--; + maxHeapify(h, 0); + + return removedElement; } -void MinHeapSort(int *arr, int size) +// Heap Sort using Max Heap +void MaxHeapSort(int *arr, int size) { - MinHeap h; - initMin(&h, size); + MaxHeap h; + initMax(&h, size); for (int i = 0; i < size; i++) { - insetMin(&h, arr[i]); + insertMax(&h, arr[i]); } int i = 0; - while (!isEmptyMin(h)) + while (!isEmptyMax(h)) { - arr[i] = removeMin(&h); + arr[i] = removeMax(&h); i++; } return; } -void insetMin(MinHeap *h, int value) +//---------------Min Heap Operations--------------- + +int isEmptyMin(MinHeap h) +{ + return (h.rear == -1); +} +int isFullMin(MinHeap h) +{ + return (h.rear == h.size - 1); +} +void initMin(MinHeap *h, int size) +{ + h->A = (int *)malloc(sizeof(int) * size); + h->size = size; + h->rear = -1; + return; +} + +// Insert element into Min Heap +void insertMin(MinHeap *h, int value) { if (isFullMin(*h)) return; @@ -157,6 +147,29 @@ void insetMin(MinHeap *h, int value) return; } +// Creating Min Heap +void minHeapify(MinHeap *h, int index) +{ + int smallest = index; + int left = 2 * index + 1; + int right = 2 * index + 2; + + if (left < h->rear && h->A[left] < h->A[smallest]) + smallest = left; + + if (right < h->rear && h->A[right] < h->A[smallest]) + smallest = right; + + if (smallest != index) + { + swap(&h->A[index], &h->A[smallest]); + minHeapify(h, smallest); + } + + return; +} + +// Remove Min Element from Heap int removeMin(MinHeap *h) { if (isEmptyMin(*h)) @@ -170,22 +183,23 @@ int removeMin(MinHeap *h) return removedElement; } -void minHeapify(MinHeap *h, int index) -{ - int smallest = index; - int left = 2 * index + 1; - int right = 2 * index + 2; - if (left < h->rear && h->A[left] < h->A[smallest]) - smallest = left; +// Heap Sort using Min Heap +void MinHeapSort(int *arr, int size) +{ + MinHeap h; + initMin(&h, size); - if (right < h->rear && h->A[right] < h->A[smallest]) - smallest = right; + for (int i = 0; i < size; i++) + { + insertMin(&h, arr[i]); + } - if (smallest != index) + int i = 0; + while (!isEmptyMin(h)) { - swap(&h->A[index], &h->A[smallest]); - minHeapify(h, smallest); + arr[i] = removeMin(&h); + i++; } return; diff --git a/graph/graphADT/graph.txt b/graph/graphADT/graph.txt new file mode 100644 index 0000000..4b2da41 --- /dev/null +++ b/graph/graphADT/graph.txt @@ -0,0 +1,5 @@ +4 +0 2 0 0 +2 0 7 9 +0 7 0 1 +0 9 1 0 \ No newline at end of file diff --git a/graph/graphADT/header.h b/graph/graphADT/header.h new file mode 100644 index 0000000..5f7ef8f --- /dev/null +++ b/graph/graphADT/header.h @@ -0,0 +1,17 @@ +#include + +typedef struct graph +{ + int ** G; + int vertices; + int edges; +} Graph; + +void init(Graph *g, int vertices); +void addEdge(Graph *g, int src, int des, int weight); +void createGraph(Graph *g, FILE *f); +void display(Graph g); +int isAdjacent(Graph g, int src, int des); +int getDegree(Graph g, int vertex); +int isConnected(Graph g); +void displayComponents(Graph g); diff --git a/graph/graphADT/logic.c b/graph/graphADT/logic.c new file mode 100644 index 0000000..c1509a2 --- /dev/null +++ b/graph/graphADT/logic.c @@ -0,0 +1,123 @@ +#include +#include "header.h" + +void init(Graph *g, int vertices) +{ + g->vertices = vertices; + g->edges = 0; + g->G = (int **)malloc(vertices * sizeof(int *)); + + for (int i = 0; i < vertices; i++) + { + g->G[i] = (int *)calloc(vertices, sizeof(int)); + } + + return; +} + +void addEdge(Graph *g, int src, int des, int weight) +{ + g->G[src][des] = weight; + g->G[des][src] = weight; + g->edges++; + return; +} + +void createGraph(Graph *g, FILE *f) +{ + int vertices, t; + fscanf(f, "%d", &vertices); + + init(g, vertices); + + for (int i = 0; i < vertices; i++) + { + for (int j = 0; j < vertices; j++) + { + fscanf(f, "%d", &t); + if (t && i <= j) + addEdge(g, i, j, t); + } + } + + return; +} + +void display(Graph g) +{ + int vertices = g.vertices; + + printf("The graph as a adjacency matrix:\n"); + + for (int i = 0; i < vertices; i++) + { + for (int j = 0; j < vertices; j++) + { + printf("%d ", g.G[i][j]); + } + + printf("\n"); + } + + return; +} + +int isAdjacent(Graph g, int src, int des){ + if (g.G[src][des] != 0) return 1; + + return 0; +} + +int getDegree(Graph g, int vertex){ + int degree = 0; + + for (int i = 0; i < g.vertices; i++) + { + if (g.G[vertex][i] != 0) degree++; + } + + return degree; +} + +int isConnected(Graph g){ // Not correct function... + int vertices = g.vertices; + + int i = 0, j = 0; + + while (i < vertices){ // Have to do proper DFS here, as this only tells us whether only one component is isolated... + j = 0; + while (j < vertices) + { + if (g.G[i][j] != 0){ + i++; + break; + } + else + j++; + } + + if (j == vertices) return 0; + + } + + return 1; +} + +void displayComponents(Graph g){ + int vertices = g.vertices; + + for (int i = 0; i < vertices; i++) + { + printf("%d: ", i); + for (int j = 0; j < vertices; j++) + { + if (g.G[i][j] != 0) printf("%d ", j); + } + + printf("\n"); + + } + + return; + +} \ No newline at end of file diff --git a/graph/graphADT/main.c b/graph/graphADT/main.c new file mode 100644 index 0000000..70afec3 --- /dev/null +++ b/graph/graphADT/main.c @@ -0,0 +1,50 @@ +#include "logic.c" + +int main(int argc, char *argv[]) +{ + if (argc == 1) + { + printf("Please pass the text file name containing the graph matrix as an argument.\n"); + return 1; + } + else if (argc != 2) + { + printf("Please pass on one argument!\n"); + return 1; + } + + Graph g; + + FILE *f = fopen(argv[1], "r"); + + createGraph(&g, f); + + printf("Displaying the graph:\n"); + display(g); + + int vertices = g.vertices; + + printf("Degree of each vertex is: \n"); + + for (int i = 0; i < vertices; i++) + { + printf("%d: %d\n", i, getDegree(g, i)); + } + + printf("Is the given graph connected? %s\n", isConnected(g) ? "Yes!" : "No!"); + + printf("Displaying the components of the graph: \n"); + + displayComponents(g); + + printf("Checking adjacency:\n"); + + int src = 1, des = 3; + printf("Is %d and %d adjacent? %s\n", src, des, isAdjacent(g, src, des) ? "Yes!" : "No!"); + + src = 0; + des = 2; + printf("Is %d and %d adjacent? %s\n", src, des, isAdjacent(g, src, des) ? "Yes!" : "No!"); + + return 0; +} \ No newline at end of file diff --git a/graph/graphUsingQueue/graph.txt b/graph/graphUsingQueue/graph.txt new file mode 100644 index 0000000..6f3bee0 --- /dev/null +++ b/graph/graphUsingQueue/graph.txt @@ -0,0 +1,10 @@ +9 +0 2 4 6 0 0 0 0 0 +2 0 5 0 0 0 0 0 0 +4 5 0 1 2 0 0 0 0 +6 0 1 0 0 3 0 4 0 +0 0 2 0 0 1 5 0 3 +0 0 0 3 1 0 4 0 0 +0 0 0 0 5 4 0 0 0 +0 0 0 4 0 0 0 0 0 +0 0 0 0 3 0 0 0 0 \ No newline at end of file diff --git a/graph/graphUsingQueue/header.h b/graph/graphUsingQueue/header.h new file mode 100644 index 0000000..2b8ec53 --- /dev/null +++ b/graph/graphUsingQueue/header.h @@ -0,0 +1,33 @@ +#include + +typedef struct node +{ + char vertex; + int weight; + struct node *next; +} Node; + +typedef struct vertex +{ + char vertex; + Node *edges; +} Vertex; + +typedef struct graph +{ + int V; + Vertex *array; +} Graph; + +void initGraph(Graph * g, int numberOfVertices); +Node *generateNode(char vertex, int weight); +void addVertex(Graph *g, char vertex); +int checkVertex(Graph g, char vertex); +void addEdge(Graph *g, char vertex1, char vertex2, int weight); +void createGraph(Graph *g, FILE *f); +void displayGraph(Graph g); + +void dfs(Graph g, char * visited, char vertex); +void bfs(Graph g, char * visited, char vertex); + +void primMST(Graph g); \ No newline at end of file diff --git a/graph/graphUsingQueue/logic.c b/graph/graphUsingQueue/logic.c new file mode 100644 index 0000000..44e3d22 --- /dev/null +++ b/graph/graphUsingQueue/logic.c @@ -0,0 +1,239 @@ +#include +#include +#include +#include "header.h" +#include "queue.c" + +void initGraph(Graph *g, int numberOfVertices) +{ + g->V = numberOfVertices; + + g->array = (Vertex *)malloc(sizeof(Vertex) * numberOfVertices); + + for (int i = 0; i < numberOfVertices; i++) + { + g->array[i].vertex = CHAR_MIN; + } + + return; +} + +Node *generateNode(char vertex, int weight) +{ + Node *nn = (Node *)malloc(sizeof(Node)); + + if (!nn) + return NULL; + + nn->vertex = vertex; + nn->weight = weight; + nn->next = NULL; + + return nn; +} + +void addVertex(Graph *g, char vertex) +{ + + if (checkVertex(*g, vertex) != -1) + return; + + for (int i = 0; i < g->V; i++) + { + if (g->array[i].vertex == CHAR_MIN) + { + g->array[i].vertex = vertex; + return; + } + } + + printf("Can add only %d nodes as mentioned earlier in Graph init function!\n", g->V); + + return; +} + +int checkVertex(Graph g, char vertex) +{ + + for (int i = 0; i < g.V; i++) + { + if (g.array[i].vertex == vertex) + { + return i; + } + } + + return -1; +} + +void addEdge(Graph *g, char vertex1, char vertex2, int weight) +{ + if (checkVertex(*g, vertex1) == -1) + addVertex(g, vertex1); + if (checkVertex(*g, vertex2) == -1) + addVertex(g, vertex2); + + for (int i = 0; i < g->V; i++) + { + if (g->array[i].vertex == vertex1) + { + Node *t = g->array[i].edges; + g->array[i].edges = generateNode(vertex2, weight); + g->array[i].edges->next = t; + break; + } + } + + for (int i = 0; i < g->V; i++) + { + if (g->array[i].vertex == vertex2) + { + Node *t = g->array[i].edges; + g->array[i].edges = generateNode(vertex1, weight); + g->array[i].edges->next = t; + break; + } + } + + return; +} + +void createGraph(Graph *g, FILE *f) +{ + int vertices, w; + char c = 'A'; + fscanf(f, "%d", &vertices); + + initGraph(g, vertices); + + for (int i = 0; i < vertices; i++) + { + addVertex(g, c); + for (int j = 0; j < vertices; j++) + { + fscanf(f, "%d", &w); + if (w && i <= j) + addEdge(g, c, 'A' + j, w); + } + c++; + } + + return; +} + +void displayGraph(Graph g) +{ + + for (int i = 0; i < g.V; i++) + { + printf("%c: ", g.array[i].vertex); + + Node *t = g.array[i].edges; + + while (t) + { + printf("%c[%d] ", t->vertex, t->weight); + t = t->next; + } + + printf("\n"); + } + + return; +} + +void dfs(Graph g, char *visited, char vertex) +{ + visited[vertex - 'A'] = 1; + + Node *e = g.array[checkVertex(g, vertex)].edges; + + while (e) + { + if (!visited[e->vertex - 'A']) + { + printf("%c ", e->vertex); + dfs(g, visited, e->vertex); + } + e = e->next; + } + + return; +} + +void bfs(Graph g, char * visited, char vertex){ + Queue q; + initQ(&q); + + enQueue(&q, vertex); + + visited[vertex - 'A'] = 1; + + while (!isEmpty(q)) + { + Node *e = g.array[checkVertex(g, vertex)].edges; + + while (e) + { + if (!visited[e->vertex - 'A']){ + enQueue(&q, e->vertex); + visited[e->vertex - 'A'] = 1; + } + e = e->next; + } + printf("%c ", deQueue(&q)); + if (q.front) vertex = q.front->data; + } + + return; +} + +void primMST(Graph g) { + int V = g.V; + char* parent = (char*)malloc(V * sizeof(char)); + int* key = (int*)malloc(V * sizeof(int)); + bool* mstSet = (bool*)malloc(V * sizeof(bool)); + + if (!parent || !key || !mstSet) { + return; + } + + for (int i = 0; i < V; ++i) { + parent[i] = ' '; + key[i] = INT_MAX; + mstSet[i] = false; + } + + key[0] = 0; + + for (int count = 0; count < V; ++count) { + int minKey = INT_MAX; + int minIndex; + for (int v = 0; v < V; ++v) { + if (mstSet[v] == false && key[v] < minKey) { + minKey = key[v]; + minIndex = v; + } + } + + mstSet[minIndex] = true; + + Node* temp = g.array[minIndex].edges; + while (temp != NULL) { + int v = checkVertex(g, temp->vertex); + if (temp->weight && mstSet[v] == false && temp->weight < key[v]) { + parent[v] = g.array[minIndex].vertex; + key[v] = temp->weight; + } + temp = temp->next; + } + } + + printf("Minimum Spanning Tree using Prim's algorithm:\n"); + for (int i = 1; i < V; ++i) + printf("(%c - %c) Weight: %d\n", parent[i], g.array[i].vertex, key[i]); + + free(parent); + free(key); + free(mstSet); +} diff --git a/graph/graphUsingQueue/main.c b/graph/graphUsingQueue/main.c new file mode 100644 index 0000000..c7d614b --- /dev/null +++ b/graph/graphUsingQueue/main.c @@ -0,0 +1,41 @@ +#include "logic.c" + +int main(int argc, char *argv[]) +{ + if (argc == 1) + { + printf("Please pass the text file name containing the graph matrix as an argument.\n"); + return 1; + } + else if (argc != 2) + { + printf("Please pass on one argument!\n"); + return 1; + } + + Graph g; + + FILE *f = fopen(argv[1], "r"); + + createGraph(&g, f); + + printf("Displaying the graph:\n"); + displayGraph(g); + + printf("DFS:\n"); + char *visited = (char *)calloc(g.V, sizeof(char)); + + printf("C "); + dfs(g, visited, 'C'); + printf("\n"); + + printf("BFS:\n"); + char *visited1 = (char *)calloc(g.V, sizeof(char)); + + bfs(g, visited1, 'C'); + printf("\n"); + + primMST(g); + + return 0; +} \ No newline at end of file diff --git a/graph/graphUsingQueue/queue.c b/graph/graphUsingQueue/queue.c new file mode 100644 index 0000000..a0e1b8f --- /dev/null +++ b/graph/graphUsingQueue/queue.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "queue.h" + +void initQ(Queue *q){ + q->front = NULL; + q->rear = NULL; +} + +void enQueue(Queue *q, char data){ + qNode * nn = (qNode *)malloc(sizeof(qNode)); + + if(!nn) return; + + nn->data = data; + nn->next = NULL; + + if (isEmpty(*q)){ + q->front = nn; + q->rear = nn; + return; + } + + q->rear->next = nn; + q->rear = nn; + return; +} + +char deQueue(Queue *q){ + if (isEmpty(*q)) return CHAR_MIN; + + if(q->front == q->rear) q->rear = NULL; + + qNode *p = q->front; + q->front = q->front->next; + + int d = p->data; + free(p); + + return d; +} + +int isEmpty(Queue q){ + if (!q.front) return 1; + return 0; +} + +char peek(Queue q){ + if (isEmpty(q)) return CHAR_MIN; + + return q.front->data; +} + +void display(Queue q){ + if (isEmpty(q)) return; + + printf("Displaying the queue: "); + + qNode *p = q.front; + + while (p){ + printf("%c | ", p->data); + p = p->next; + } + + printf("\b\b\b \n"); + return; +} \ No newline at end of file diff --git a/graph/graphUsingQueue/queue.h b/graph/graphUsingQueue/queue.h new file mode 100644 index 0000000..c430e47 --- /dev/null +++ b/graph/graphUsingQueue/queue.h @@ -0,0 +1,16 @@ +typedef struct qnode{ + char data; + struct qnode * next; +} qNode; + +typedef struct queue{ + qNode * front; + qNode * rear; +} Queue; + +void initQ(Queue *q); +void enQueue(Queue *q, char data); +char deQueue(Queue *q); +int isEmpty(Queue q); +char peek(Queue q); +void displayQueue(Queue q); diff --git a/heapADT/header.h b/heapADT/header.h new file mode 100644 index 0000000..739ae81 --- /dev/null +++ b/heapADT/header.h @@ -0,0 +1,33 @@ +typedef struct maxHeap +{ + int *A; + int size; + int rear; +} MaxHeap; + +typedef struct minHeap +{ + int *A; + int size; + int rear; +} MinHeap; + +void swap(int *a, int *b); + +int isEmptyMax(MaxHeap h); +int isFullMax(MaxHeap h); + +int isEmptyMin(MinHeap h); +int isFullMin(MinHeap h); + +void initMax(MaxHeap *h, int size); +void MaxHeapSort(int *arr, int size); +void insetMax(MaxHeap *h, int value); +int removeMax(MaxHeap *h); +void maxHeapify(MaxHeap *h, int index); + +void initMin(MinHeap *h, int size); +void MinHeapSort(int *arr, int size); +void insetMin(MinHeap *h, int value); +int removeMin(MinHeap *h); +void minHeapify(MinHeap *h, int index); diff --git a/heapADT/logic.c b/heapADT/logic.c new file mode 100644 index 0000000..cd12ff4 --- /dev/null +++ b/heapADT/logic.c @@ -0,0 +1,206 @@ +#include +#include +#include +#include "header.h" + +//---------------Utility function--------------- + +void swap(int *a, int *b) +{ + int temp = *a; + + *a = *b; + *b = temp; + return; +} + +//---------------Max Heap Operations--------------- + +int isEmptyMax(MaxHeap h) +{ + return (h.rear == -1); +} +int isFullMax(MaxHeap h) +{ + return (h.rear == h.size - 1); +} +void initMax(MaxHeap *h, int size) +{ + h->A = (int *)malloc(sizeof(int) * size); + h->size = size; + h->rear = -1; + return; +} + +// Insert Element into Max Heap +void insertMax(MaxHeap *h, int value) +{ + if (isFullMax(*h)) + return; + + h->rear++; + int i = h->rear; + + while (i > 0 && value > h->A[(i - 1) / 2]) + { + h->A[i] = h->A[(i - 1) / 2]; + i = (i - 1) / 2; + } + + h->A[i] = value; + + return; +} + +// Creating Max Heap +void maxHeapify(MaxHeap *h, int index) +{ + int largest = index; + int left = 2 * index + 1; + int right = 2 * index + 2; + + if (left < h->rear && h->A[left] > h->A[largest]) + largest = left; + + if (right < h->rear && h->A[right] > h->A[largest]) + largest = right; + + if (largest != index) + { + swap(&h->A[index], &h->A[largest]); + maxHeapify(h, largest); + } + + return; +} + +// Remove Max Element from Heap +int removeMax(MaxHeap *h) +{ + if (isEmptyMax(*h)) + return INT_MIN; + + int removedElement = h->A[0]; + h->A[0] = h->A[h->rear]; + h->rear--; + maxHeapify(h, 0); + + return removedElement; +} + +// Heap Sort using Max Heap +void MaxHeapSort(int *arr, int size) +{ + MaxHeap h; + initMax(&h, size); + + for (int i = 0; i < size; i++) + { + insertMax(&h, arr[i]); + } + + int i = 0; + while (!isEmptyMax(h)) + { + arr[i] = removeMax(&h); + i++; + } + + return; +} + +//---------------Min Heap Operations--------------- + +int isEmptyMin(MinHeap h) +{ + return (h.rear == -1); +} +int isFullMin(MinHeap h) +{ + return (h.rear == h.size - 1); +} +void initMin(MinHeap *h, int size) +{ + h->A = (int *)malloc(sizeof(int) * size); + h->size = size; + h->rear = -1; + return; +} + +// Insert element into Min Heap +void insertMin(MinHeap *h, int value) +{ + if (isFullMin(*h)) + return; + + h->rear++; + int i = h->rear; + + while (i > 0 && value < h->A[(i - 1) / 2]) + { + h->A[i] = h->A[(i - 1) / 2]; + i = (i - 1) / 2; + } + + h->A[i] = value; + + return; +} + +// Creating Min Heap +void minHeapify(MinHeap *h, int index) +{ + int smallest = index; + int left = 2 * index + 1; + int right = 2 * index + 2; + + if (left < h->rear && h->A[left] < h->A[smallest]) + smallest = left; + + if (right < h->rear && h->A[right] < h->A[smallest]) + smallest = right; + + if (smallest != index) + { + swap(&h->A[index], &h->A[smallest]); + minHeapify(h, smallest); + } + + return; +} + +// Remove Min Element from Heap +int removeMin(MinHeap *h) +{ + if (isEmptyMin(*h)) + return INT_MIN; + + int removedElement = h->A[0]; + h->A[0] = h->A[h->rear]; + h->rear--; + minHeapify(h, 0); + + return removedElement; +} + + +// Heap Sort using Min Heap +void MinHeapSort(int *arr, int size) +{ + MinHeap h; + initMin(&h, size); + + for (int i = 0; i < size; i++) + { + insertMin(&h, arr[i]); + } + + int i = 0; + while (!isEmptyMin(h)) + { + arr[i] = removeMin(&h); + i++; + } + + return; +} diff --git a/heapADT/main.c b/heapADT/main.c new file mode 100644 index 0000000..39656f2 --- /dev/null +++ b/heapADT/main.c @@ -0,0 +1,34 @@ +#include "logic.c" + +int main() { + + int n; + + printf("Enter the number of integers: "); + scanf("%d", &n); + + int * arr = (int *) malloc(sizeof(int) * n); + + printf("Enter the integers: "); + for (int i = 0; i < n; i++) { + scanf("%d", arr + i); + } + + MaxHeapSort(arr, n); + + printf("Sorted array using max heap: "); + for (int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + MinHeapSort(arr, n); + + printf("Sorted array using min heap: "); + for (int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + + return 0; +} diff --git a/tree/AVLtree/header.h b/tree/AVLtree/header.h new file mode 100644 index 0000000..58bfc46 --- /dev/null +++ b/tree/AVLtree/header.h @@ -0,0 +1,25 @@ +typedef struct node +{ + int balancingFactor; + char *name; + struct node *left; + struct node *right; + struct node *parent; +} Node; + +typedef Node *AVLtree; + +void init(AVLtree *t); +Node *generateNode(char *name); +void insertNode(AVLtree *t, char *name); +void balanceFactor(Node *node); +void removeNode(AVLtree *t, char *name); +void traverse(AVLtree t); // Display name, parent and the balancing factor of each node... +void destroyTree(AVLtree *t); +int height(AVLtree t); // This is just a comment I am writing form vim... + +void RL(AVLtree *t, Node * n); +void LR(AVLtree *t, Node * n); +void LL(AVLtree *t, Node * n); +void RR(AVLtree *t, Node * n); +void adjustImbalance(AVLtree *t, Node *n); diff --git a/tree/AVLtree/logic.c b/tree/AVLtree/logic.c new file mode 100644 index 0000000..41dc282 --- /dev/null +++ b/tree/AVLtree/logic.c @@ -0,0 +1,378 @@ +#include +#include +#include +#include "header.h" +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +void init(AVLtree *t) +{ + *t = NULL; + return; +} + +Node *generateNode(char *name) +{ + Node *nn = (Node *)malloc(sizeof(Node)); + if (!nn) + return NULL; + + nn->balancingFactor = 0; + nn->left = NULL; + nn->right = NULL; + nn->parent = NULL; + nn->name = (char *)malloc(strlen(name) + 1); + strcpy(nn->name, name); + + return nn; +} + +void balanceFactor(Node *node) +{ + if (!node) + return; + + node->balancingFactor = height(node->left) - height(node->right); + + while (node->parent) + { + node = node->parent; + + int t = height(node->left) - height(node->right); + node->balancingFactor = t; + } + + return; +} + +void insertNode(AVLtree *t, char *name) +{ + if (!*t) + { + *t = generateNode(name); + balanceFactor(*t); + return; + } + + Node *p = *t; + Node *q = NULL; + char direction; + + while (p) + { + q = p; + if (strcmp(name, p->name) > 0) + { + p = p->right; + direction = 'r'; + } + else if (strcmp(name, p->name) < 0) + { + p = p->left; + direction = 'l'; + } + else + { + printf("The element already exists in the tree!\n"); + return; + } + } + + p = generateNode(name); + + if (direction == 'r') + q->right = p; + else + q->left = p; + + p->parent = q; + + balanceFactor(p); + adjustImbalance(t, p->parent); + + return; +} + +void removeNode(AVLtree *t, char *name) +{ + if (!*t) + return; + + Node *p = *t, *q = *t; + char direction; + + while (strcmp(name, p->name) != 0) + { + q = p; + if (strcmp(name, p->name) < 0) + { + p = p->left; + direction = 'l'; // p is left child of q + } + else + { + p = p->right; + direction = 'r'; // p is right child of q + } + } + + // Handling the case when the node to be deleted has no child... + if (p->left == NULL && p->right == NULL) + { + if (p == q) + { + *t = NULL; + free(p); + printf("The tree is empty now...\n"); + return; + } + + if (direction == 'r') + q->right = NULL; + else + q->left = NULL; + free(p); + + balanceFactor(q); + adjustImbalance(t, q->parent); + + return; + } + + // Handling the case when the node has only one child... + if (p->left == NULL) + { + if (p == q) + { + *t = p->right; + p->right->parent = NULL; + printf("The root is replaced by %s\n", p->right->name); + } + else + { + if (direction == 'r') + q->right = p->right; + else + q->left = p->right; + + p->right->parent = q; + } + free(p); + + balanceFactor(q); + adjustImbalance(t, q->parent); + + return; + } + if (p->right == NULL) + { + if (p == q) + { + *t = p->left; + p->left->parent = NULL; + printf("The root is replaced by %s\n", p->left->name); + } + else + { + if (direction == 'r') + q->right = p->left; + else + q->left = p->left; + + p->left->parent = q; + } + free(p); + + balanceFactor(q); + adjustImbalance(t, q->parent); + + return; + } + + // Handling the case when the node has two childrens... + // Checking for the smallest number in the right sub tree... + q = p->right; + Node *r = p; + direction = 'r'; + while (q->left) + { + r = q; + direction = 'l'; + q = q->left; + } + + strcpy(p->name, q->name); + + if (p == *t) + { + printf("The root is replaced by %s\n", q->name); + } + if (direction == 'l') + r->left = q->right; + else + r->right = q->right; + + if (q->right) + q->right->parent = r; + + free(q); + + balanceFactor(r); + adjustImbalance(t, r->parent); + + return; +} + +void traverse(AVLtree t) // Display name, parent and the balancing factor of each node... +{ + if (!t) + return; + + traverse(t->left); + printf("Name: %s\nParent Name: %s\nBalancing Factor: %d\n\n", t->name, (t->parent) ? t->parent->name : "NULL", t->balancingFactor); + traverse(t->right); + + return; +} + +void destroyTree(AVLtree *t) +{ + if (!*t) + return; + + if (!(*t)->right && !(*t)->left) + { + free(*t); + *t = NULL; + return; + } + + if (!(*t)->right) + { + destroyTree(&(*t)->left); + free(*t); + *t = NULL; + return; + } + + if (!(*t)->left) + { + destroyTree(&(*t)->right); + free(*t); + *t = NULL; + return; + } + + destroyTree(&(*t)->left); + destroyTree(&(*t)->right); + free(*t); + *t = NULL; + return; +} + +int height(AVLtree t) +{ + if (!t) + return 0; + + return 1 + MAX(height(t->left), height(t->right)); +} + +void RL(AVLtree *t, Node *n) +{ + RR(t, n->left); + LL(t, n); + return; +} + +void LR(AVLtree *t, Node *n) +{ + LL(t, n->right); + RR(t, n); + return; +} + +void LL(AVLtree *t, Node *n) +{ + Node *temp = n->left; + n->left = temp->right; + if (n->left) + n->left->parent = n; + + temp->right = n; + temp->parent = n->parent; + n->parent = temp; + + if (temp->parent) + { + if (temp->parent->left == n) + temp->parent->left = temp; + else + temp->parent->right = temp; + } + else + *t = temp; + balanceFactor(n); + balanceFactor(temp); + + return; +} + +void RR(AVLtree *t, Node *n) +{ + Node *temp = n->right; + n->right = temp->left; + if (n->right) + n->right->parent = n; + + temp->left = n; + temp->parent = n->parent; + n->parent = temp; + + if (temp->parent) + { + if (temp->parent->right == n) + temp->parent->right = temp; + else + temp->parent->left = temp; + } + else + *t = temp; + balanceFactor(n); + balanceFactor(temp); + + return; +} + +void adjustImbalance(AVLtree *t, Node *n) +{ + + if (!n) + return; + + int bf = n->balancingFactor; + + while (bf == 0 | bf == 1 | bf == -1) + { + n = n->parent; + if (!n) + return; + bf = n->balancingFactor; + } + + if (bf == 2) + { + if (n->left->balancingFactor == -1) + RL(t, n); + else + LL(t, n); + } + else if (bf == -2) + { + if (n->right->balancingFactor == 1) + LR(t, n); + else + RR(t, n); + } + + return adjustImbalance(t, n->parent); +} diff --git a/tree/AVLtree/main.c b/tree/AVLtree/main.c new file mode 100644 index 0000000..fcb6755 --- /dev/null +++ b/tree/AVLtree/main.c @@ -0,0 +1,34 @@ +#include "logic.c" + +int main(){ + AVLtree T; + init(&T); + + insertNode(&T, "Pratham"); + insertNode(&T, "Bhavya"); + insertNode(&T, "Anjali"); + insertNode(&T, "Vaishanavi"); + insertNode(&T, "Vaishnavi"); + insertNode(&T, "Rohit"); + insertNode(&T, "Prajakta"); + + printf("Tree:\n"); + traverse(T); + + removeNode(&T, "Vaishnavi"); + removeNode(&T, "Pratham"); + + printf("Tree:\n"); + traverse(T); + + insertNode( &T,"Akash"); + printf("Tree:\n"); + traverse(T); + + destroyTree(&T); + + printf("Tree:\n"); + traverse(T); // This should print an empty tree since the tree has been destroyed... + + return 0; +} \ No newline at end of file diff --git a/tree/AVLtree/menu.c b/tree/AVLtree/menu.c new file mode 100644 index 0000000..657c239 --- /dev/null +++ b/tree/AVLtree/menu.c @@ -0,0 +1,54 @@ +#include "logic.c" + +void menu() { + printf("\n---------------------AVL Tree Menu----------------------\n"); + printf("1. Initialize AVL Tree\n"); + printf("2. Insert a Node\n"); + printf("3. Remove a Node\n"); + printf("4. Traverse AVL Tree\n"); + printf("5. Destroy AVL Tree\n"); + printf("6. Exit\n"); + printf("Enter your choice: "); +} + +int main() { + int choice; + AVLtree T; + char *name = (char *)malloc(sizeof(char) * 50); + do { + menu(); + scanf("%d", &choice); + switch(choice) { + case 1: + init(&T); + break; + case 2: + printf("Enter Name: "); + scanf("%s", name); + insertNode(&T, name); + printf("Node inserted.\n"); + break; + case 3: + printf("Enter Name: "); + scanf("%s", name); + removeNode(&T, name); + printf("Node Deleted.\n"); + break; + case 4: + printf("Inorder Traversal of the Tree: \n"); + traverse(T); + break; + case 5: + destroyTree(&T); + printf("AVL Tree destroyed.\n"); + break; + case 6: + printf("Exiting...\n"); + break; + default: + printf("Invalid choice! Please enter a valid option.\n"); + } + } while(choice != 6); + + return 0; +} \ No newline at end of file diff --git a/tree/BST/header.h b/tree/BST/header.h new file mode 100644 index 0000000..54094fe --- /dev/null +++ b/tree/BST/header.h @@ -0,0 +1,17 @@ +typedef struct tree +{ + int *T; // Using Implicit Indexing of arrays to represent a BST... + int size; // Max number of nodes the tree can bear... + int length; +} Tree; + +void init_bst(Tree *t, int n); +void insert_bst(Tree *t, int data); // non-recursive insert... +void non_recursive_traverse(Tree t); // non-recursive inorder, preorder, postorder... +void inorder(Tree t); +void preorder(Tree t); +void postorder(Tree t); +void levelwise(Tree t); // to show levelwise nodes... +int isComplete(Tree t); // Check whether BST is a complete tree... +int levels(Tree t, int i); +int height(Tree t); \ No newline at end of file diff --git a/tree/BST/logic.c b/tree/BST/logic.c new file mode 100644 index 0000000..91e8a4c --- /dev/null +++ b/tree/BST/logic.c @@ -0,0 +1,247 @@ +#include +#include +#include +#include +#include "header.h" +#include "stack.c" +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +void init_bst(Tree *t, int n) +{ + t->T = (int *)malloc(n * sizeof(int)); + t->size = n; + for (int i = 0; i < n; i++) + { + t->T[i] = INT_MIN; + } + t->length = 0; + + return; +} + +// non-recursive insert... +void insert_bst(Tree *t, int data) +{ + + int i = 0; + while (i < t->size && t->T[i] != INT_MIN) + { + if (t->T[i] > data) + { + i = 2 * i + 1; // left child + } + else if (t->T[i] < data) + { + i = 2 * i + 2; // right child + } + else + { + printf("Data already exists!\n"); + return; + } + } + + t->T[i] = data; + t->length++; + + return; +} + +// non-recursive inorder, preorder, postorder... + +void non_recursive_traverse(Tree t) +{ + printf("Inorder Printing: "); + inorder(t); + printf("\n"); + printf("Preorder Printing: "); + preorder(t); + printf("\n"); + printf("Postorder Printing: "); + postorder(t); + printf("\n"); + + return; +} + +void inorder(Tree t) +{ + Stack s; + init(&s, t.length); + + int i = 0, k = t.size, x; + + push(&s, i); + + while (!isEmpty(s)) + { + i = peek(s); + if (t.T[i] == INT_MIN || i >= k) + { + pop(&s); + if (isEmpty(s)) + break; + x = pop(&s); + printf("%d ", t.T[x]); + push(&s, x * 2 + 2); + } + else + { + push(&s, i * 2 + 1); + } + } + + printf("\n"); + + return; +} + +// Preorder using only one stack... +void preorder(Tree t) +{ + Stack s; + init(&s, t.length); + int i = 0; + int k = t.size; + + printf("%d ", t.T[i]); + push(&s, 2 * i + 2); + i = 2 * i + 1; + + while(!isEmpty(s) && i < k){ + + while (i != INT_MIN && t.T[i] == INT_MIN && i < k){ + i = pop(&s); + } + if (i == INT_MIN) break; + printf("%d ", t.T[i]); + push(&s, 2 * i + 2); + i = 2 * i + 1; + } + + printf("\n"); + return; +} +void postorder(Tree t) +{ + Stack s1, s2; + init(&s1, t.length); + init(&s2, t.length + 2); + + int i = 0, k = t.size; + push(&s1, i); + + while (!isEmpty(s1) && i < k) + { + i = pop(&s1); + while (i != INT_MIN && i < k && t.T[i] == INT_MIN) + { + i = pop(&s1); + } + if (i == INT_MIN) break; + push(&s2, i); + push(&s1, 2 * i + 1); + push(&s1, 2 * i + 2); + } + + while (!isEmpty(s2)) + { + printf("%d ", t.T[pop(&s2)]); + } + + printf("\n"); + + return; +} + +// to show levelwise nodes... +void levelwise(Tree t) +{ + if (!t.T) + { + printf("Tree is empty...\n"); + return; + } + + int n = t.size; + int i = 0; + int j = 0, k = 1; + int levelCount = 1; + int treeHeight = height(t); + + for (int z = 0; z < pow(2, (treeHeight - levelCount)) - 1; z++) + { + printf(" "); + } + printf(" "); + + while (i < n) + { + if (j == k) + { + printf("\n\n"); + levelCount++; + for (int z = 0; z < pow(2, (treeHeight - levelCount)) - 1; z++) + { + printf(" "); + } + if (treeHeight != levelCount - 1) + printf(" "); + j = 0; + k *= 2; + } + if (t.T[i] != INT_MIN) + { + printf("%4d", t.T[i++]); + } + else + { + printf(" "); + i++; + } + for (int z = 0; z < pow(2, (treeHeight - levelCount + 1)) - 1; z++) + { + printf(" "); + } + j++; + } + printf("\n"); + + return; +} + +// Check whether BST is a complete tree... +int isComplete(Tree t) +{ + int treeHeight = height(t); + int i = 0, n = pow(2, treeHeight) - 1; + + while (i < n) + { + if (t.T[i] == INT_MIN) + return 0; + i++; + } + + return 1; +} + +int levels(Tree t, int i) +{ + + if (!t.T) + return 0; + if (t.length == 1) + return 0; + if (i >= t.size) + return 0; + if (t.T[i] == INT_MIN) + return 0; + + return 1 + MAX(levels(t, 2 * i + 1), levels(t, 2 * i + 2)); +} + +int height(Tree t) +{ + return levels(t, 0) - 1; +} \ No newline at end of file diff --git a/tree/BST/main.c b/tree/BST/main.c new file mode 100644 index 0000000..33cf4b4 --- /dev/null +++ b/tree/BST/main.c @@ -0,0 +1,37 @@ +#include "logic.c" + +int main() +{ + Tree t; + init_bst(&t, 31); + + insert_bst(&t, 50); + insert_bst(&t, 30); + insert_bst(&t, 70); + insert_bst(&t, 20); + insert_bst(&t, 40); + insert_bst(&t, 60); + insert_bst(&t, 80); + insert_bst(&t, 15); + insert_bst(&t, 25); + insert_bst(&t, 35); + insert_bst(&t, 45); + insert_bst(&t, 55); + insert_bst(&t, 65); + insert_bst(&t, 75); + insert_bst(&t, 85); + + printf("Level-wise Printing:\n"); + printf("\n"); + levelwise(t); + + non_recursive_traverse(t); + + printf("The height of the tree is %d\n", height(t)); + printf("\n"); + + printf("Is-Complete: %s\n", isComplete(t) == 1 ? "True" : "False"); + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/tree/BST/stack.c b/tree/BST/stack.c new file mode 100644 index 0000000..913b2d6 --- /dev/null +++ b/tree/BST/stack.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include "stack.h" + +void init(Stack *s, int size) +{ + if (size < 1) + return; + + s->size = size; + s->top = -1; + s->A = (int *)malloc(sizeof(int) * size); + + return; +} + +int isFull(Stack s) +{ + if (s.top == (s.size - 1)) + return 1; + + return 0; +} + +void push(Stack *s, int data) +{ + if (isFull(*s)) + return; + + s->A[++s->top] = data; + return; +} + +int isEmpty(Stack s) +{ + if (s.top == -1) + return 1; + + return 0; +} + +int pop(Stack *s) +{ + if (isEmpty(*s)) + return INT_MIN; + + return s->A[s->top--]; +} + +int peek(Stack s) +{ + if (isEmpty(s)) + return INT_MIN; + + return s.A[s.top]; +} + +void display(Stack s) +{ + printf("Displaying the stack:\n"); + + for (int i = 0; i < s.top + 1; i++) + { + printf("%d ", s.A[i]); + } + + printf("\n"); + + return; +} \ No newline at end of file diff --git a/tree/BST/stack.h b/tree/BST/stack.h new file mode 100644 index 0000000..125aeb0 --- /dev/null +++ b/tree/BST/stack.h @@ -0,0 +1,13 @@ +typedef struct stack +{ + int size, top; + int *A; +} Stack; + +void init(Stack *s, int size); +int isFull(Stack s); +void push(Stack *s, int data); +int isEmpty(Stack s); +int pop(Stack *s); +int peek(Stack s); +void display(Stack s); \ No newline at end of file diff --git a/tree/binaryTree/binaryTree.c b/tree/binaryTree/binaryTree.c new file mode 100644 index 0000000..dbd29f0 --- /dev/null +++ b/tree/binaryTree/binaryTree.c @@ -0,0 +1,34 @@ +#include "stack.c" + +void init(Tree *t) +{ + *t = NULL; + return; +} + +Node *generateNode(char * string) +{ + Node *nn = (Node *)malloc(sizeof(Node)); + + if (!nn) + return NULL; + + nn->s = (char *)malloc(sizeof(char) * strlen(string)); + strcpy(nn->s, string); + nn->left = NULL; + nn->right = NULL; + + return nn; +} + +void inorderTraversal(Tree t) +{ + if (!t) + return; + + inorderTraversal(t->left); + printf("%s ", t->s); + inorderTraversal(t->right); + + return; +} diff --git a/tree/binaryTree/binaryTree.h b/tree/binaryTree/binaryTree.h new file mode 100644 index 0000000..2ec6e36 --- /dev/null +++ b/tree/binaryTree/binaryTree.h @@ -0,0 +1,12 @@ +typedef struct node +{ + char * s; + struct node * left; + struct node * right; +} Node; + +typedef Node * Tree; + +void init(Tree *t); +Node *generateNode(char * string); +void inorderTraversal(Tree t); \ No newline at end of file diff --git a/tree/binaryTree/infixToExpressionTree.c b/tree/binaryTree/infixToExpressionTree.c new file mode 100644 index 0000000..def5239 --- /dev/null +++ b/tree/binaryTree/infixToExpressionTree.c @@ -0,0 +1,191 @@ +#include "binaryTree.c" +#include +#include +#include + +int operatorPrecedence(char c) +{ + switch (c) + { + case '(': + return INT_MIN; + case '-': + case '+': + return 0; + case '*': + case '/': + case '%': + return 1; + case '^': + return 2; + } +} + +int isOperator(char c) +{ + switch (c) + { + case '-': + case '+': + case '*': + case '/': + case '%': + case '^': + return 1; + default: + return 0; + } +} + +void expressionTree(Tree *T, char *infix) +{ + Stack operandStack, operatorStack; + Sinit(&operandStack); + Sinit(&operatorStack); + int i = 0, j = 0, operator= 0; + char c; + char *operand = (char *)malloc(sizeof(char) * strlen(infix)); + + while ((c = infix[i]) != '\0') + { + if (c == ' ' && operator== 0) + { + if (j == 0){ + i++; + continue; + } + operand[j++] = '\0'; + i++; + push(&operandStack, generateNode(operand)); + free(operand); + operand = (char *)malloc(sizeof(char) * strlen(infix)); + j = 0; + continue; + } + + if (c == ' ') + { + i++; + continue; + } + + if (infix[i] == '+' && i == 0) + { + i++; + continue; + } + + if (infix[i] == '-' && i == 0) + { + operand[j++] = infix[i++]; + continue; + } + + if (c == '(') + { + char * z = (char *)malloc(sizeof(char) * 2); + z[0] = c, z[1] = '\0'; + + push(&operatorStack, generateNode(z)); + i++; + operator= 1; + continue; + } + + if (c == ')') + { + if (j != 0){ + operand[j++] = '\0'; + push(&operandStack, generateNode(operand)); + free(operand); + operand = (char *)malloc(sizeof(char) * strlen(infix)); + j = 0; + } + + Node *t = peek(operatorStack); + while (*t->s != '(') + { + t = pop(&operatorStack); + t->right = pop(&operandStack); + t->left = pop(&operandStack); + push(&operandStack, t); + t = peek(operatorStack); + } + operator= 0; + i++; + pop(&operatorStack); + continue; + } + + if (isOperator(c)) + { + + if (operator) + { + if (c == '-') + { + operand[j++] = c; + i++; + continue; + } + else if (c == '+') + { + i++; + continue; + } + else + { + printf("Invalid Infix Expression!\n"); + return; + } + } + + if (isEmpty(operatorStack)) + { + char * z = (char *)malloc(sizeof(char) * 2); + z[0] = c, z[1] = '\0'; + push(&operatorStack, generateNode(z)); + operator= 1; + i++; + continue; + } + Node *p = peek(operatorStack); + while (operatorPrecedence(*p->s) >= operatorPrecedence(c)) + { + p = pop(&operatorStack); + p->right = pop(&operandStack); + p->left = pop(&operandStack); + push(&operandStack, p); + if (isEmpty(operatorStack)) + break; + p = peek(operatorStack); + } + char * z = (char *)malloc(sizeof(char) * 2); + z[0] = c, z[1] = '\0'; + push(&operatorStack, generateNode(z)); + operator= 1; + i++; + continue; + } + + operand[j++] = c; + operator= 0; + i++; + } + if (j != 0){ + operand[j++] = '\0'; + push(&operandStack, generateNode(operand)); + } + + Node *t = NULL; + while (!isEmpty(operatorStack)) + { + t = pop(&operatorStack); + t->right = pop(&operandStack); + t->left = pop(&operandStack); + push(&operandStack, t); + } + + *T = pop(&operandStack); + return; +} \ No newline at end of file diff --git a/tree/binaryTree/main.c b/tree/binaryTree/main.c new file mode 100644 index 0000000..725d1f9 --- /dev/null +++ b/tree/binaryTree/main.c @@ -0,0 +1,15 @@ +#include "infixToExpressionTree.c" + +int main(){ + Tree tree; + init(&tree); + + char * userData = "-874 * ( (6 + -98) * (3 + 20) )"; + + expressionTree(&tree, userData); + + inorderTraversal(tree); + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/tree/binaryTree/stack.c b/tree/binaryTree/stack.c new file mode 100644 index 0000000..1d9ad3a --- /dev/null +++ b/tree/binaryTree/stack.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include "stack.h" + +void Sinit(Stack *s){ + *s = NULL; + return; +} + +int isEmpty(Stack s){ + if (!s) return 1; + return 0; +} + +void push(Stack *s, Node * n){ + SNode * nn = (SNode *)malloc(sizeof(SNode)); + if (!nn) return; + + nn->n = n; + nn->next = *s; + *s = nn; + + return; +} + +Node * pop(Stack *s){ + if (isEmpty(*s)) return NULL; + + SNode * q = *s; + *s = (*s)->next; + + Node *n = q->n; + free(q); + return n; +} + +Node * peek(Stack s){ + if (isEmpty(s)) return NULL; + + return s->n; +} diff --git a/tree/binaryTree/stack.h b/tree/binaryTree/stack.h new file mode 100644 index 0000000..1c05a32 --- /dev/null +++ b/tree/binaryTree/stack.h @@ -0,0 +1,14 @@ +#include "binaryTree.h" + +typedef struct snode{ + Node * n; + struct snode * next; +} SNode; + +typedef SNode * Stack; + +void Sinit(Stack *s); +int isEmpty(Stack s); +void push(Stack *s, Node * n); +Node * pop(Stack *s); +Node * peek(Stack s); \ No newline at end of file diff --git a/tree/treeADT/header.h b/tree/treeADT/header.h new file mode 100644 index 0000000..a4fd1a6 --- /dev/null +++ b/tree/treeADT/header.h @@ -0,0 +1,22 @@ +typedef struct node +{ + int MIS; + char *name; + struct node *left; + struct node *right; +} Node; + +typedef Node *Tree; + +void initBST(Tree *t); +Node *generateNode(int MIS, char *name); +void insertNode(Tree *t, int MIS, char *name); // Using recursion... + +int count(Tree t); + +void removeNode(Tree *t, int MIS); +int search(Tree t, int MIS); // Using recursion... + +void preorderTraversal(Tree t); +void inorderTraversal(Tree t); +void postorderTraversal(Tree t); diff --git a/tree/treeADT/logic.c b/tree/treeADT/logic.c new file mode 100644 index 0000000..57dff7b --- /dev/null +++ b/tree/treeADT/logic.c @@ -0,0 +1,227 @@ +#include +#include +#include +#include "header.h" + +void initBST(Tree *t) +{ + *t = NULL; + return; +} + +Node *generateNode(int MIS, char *name) +{ + Node *nn = (Node *)malloc(sizeof(Node)); + + if (!nn) + return NULL; + + nn->MIS = MIS; + nn->name = (char *)malloc(sizeof(char) * strlen(name)); + strcpy(nn->name, name); + nn->left = NULL; + nn->right = NULL; + + return nn; +} + +void insertNode(Tree *t, int MIS, char *name) +{ + // Using recursion... + if (!*t) + { + *t = generateNode(MIS, name); + return; + } + + Node *p = *t; + + if (p->MIS > MIS) + { + if (p->left == NULL) + { + p->left = generateNode(MIS, name); + return; + } + insertNode(&p->left, MIS, name); + return; + } + if (p->MIS < MIS) + { + if (p->right == NULL) + { + p->right = generateNode(MIS, name); + return; + } + insertNode(&p->right, MIS, name); + return; + } + printf("The student already Exists!\n"); + return; +} + +int count(Tree t) +{ + if (!t) + return 0; + + return 1 + count(t->left) + count(t->right); +} + +void removeNode(Tree *t, int MIS) +{ + if (!*t) + return; + + Node *p = *t, *q = *t; + char direction; + + while (p->MIS != MIS) + { + q = p; + if (p->MIS > MIS) + { + p = p->left; + direction = 'l'; // p is left child of q + } + else + { + p = p->right; + direction = 'r'; // p is right child of q + } + } + + // Handling the case when the node to be deleted has no child... + if (p->left == NULL && p->right == NULL) + { + if (p == q) + { + *t = NULL; + free(p); + printf("The tree is empty now...\n"); + return; + } + free(p); + if (direction == 'r') + q->right = NULL; + else + q->left = NULL; + return; + } + + // Handling the case when the node has only one child... + if (p->left == NULL) + { + if (p == q) + { + *t = p->right; + printf("The root is replaced by %d\n", p->right->MIS); + } + else + { + if (direction == 'r') + q->right = p->right; + else + q->left = p->right; + } + free(p); + return; + } + if (p->right == NULL) + { + if (p == q) + { + *t = p->left; + printf("The root is replaced by %d\n", p->left->MIS); + } + else + { + if (direction == 'r') + q->right = p->left; + else + q->left = p->left; + } + free(p); + return; + } + + // Handling the case when the node has two childrens... + // Checking for the smallest number in the right sub tree... + q = p->right; + Node *r = p; + direction = 'r'; + while (q->left) + { + r = q; + direction = 'l'; + q = q->left; + } + p->MIS = q->MIS; + p->name = q->name; + if (p == *t) + { + printf("The root is replaced by %d\n", q->MIS); + } + if (direction == 'l') + r->left = q->right; + else + r->right = q->right; + free(q); + + return; +} + +int search(Tree t, int MIS) +{ + // Using recursion... + if (!t) + return 0; + + if (t->MIS == MIS) + return 1; + + if (t->MIS > MIS) + return search(t->left, MIS); + + if (t->MIS < MIS) + return search(t->right, MIS); +} + +void preorderTraversal(Tree t) +{ + // NLR... + if (!t) + return; + + printf("%d %s\n", t->MIS, t->name); + preorderTraversal(t->left); + preorderTraversal(t->right); + + return; +} + +void inorderTraversal(Tree t) +{ + // LNR... + if (!t) + return; + + inorderTraversal(t->left); + printf("%d %s\n", t->MIS, t->name); + inorderTraversal(t->right); + + return; +} + +void postorderTraversal(Tree t) +{ + // LRN... + if (!t) + return; + + postorderTraversal(t->left); + postorderTraversal(t->right); + printf("%d %s\n", t->MIS, t->name); + + return; +} \ No newline at end of file diff --git a/tree/treeADT/main.c b/tree/treeADT/main.c new file mode 100644 index 0000000..f34499f --- /dev/null +++ b/tree/treeADT/main.c @@ -0,0 +1,80 @@ +#include "logic.c" + +void menu() +{ + Tree t; + initBST(&t); + + int MIS, choice; + char *name = (char *)malloc(sizeof(char) * 50); + + do + { + printf("\n----- Binary Search Tree Menu -----\n"); + printf("1. Insert Node\n"); + printf("2. Count Nodes\n"); + printf("3. Remove Node\n"); + printf("4. Search Node\n"); + printf("5. Inorder Traversal\n"); + printf("6. Preorder Traversal\n"); + printf("7. Postorder Traversal\n"); + printf("0. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) + { + case 1: + printf("Enter MIS number: "); + scanf("%d", &MIS); + printf("Enter Name: "); + scanf("%s", name); + insertNode(&t, MIS, name); + printf("Node inserted.\n"); + break; + case 2: + printf("Total nodes in the tree: %d\n", count(t)); + break; + case 3: + printf("Enter MIS number to remove: "); + scanf("%d", &MIS); + removeNode(&t, MIS); + printf("Node removed.\n"); + break; + case 4: + printf("Enter MIS number to search: "); + scanf("%d", &MIS); + if (search(t, MIS) == 1) + printf("Node found.\n"); + else + printf("Node not found.\n"); + break; + case 5: + printf("Inorder Traversal: \n"); + inorderTraversal(t); + printf("\n"); + break; + case 6: + printf("Preorder Traversal: \n"); + preorderTraversal(t); + printf("\n"); + break; + case 7: + printf("Postorder Traversal: \n"); + postorderTraversal(t); + printf("\n"); + break; + case 0: + printf("Exiting DSA 2 assignment 1 program. This is what we call as Binary Search Tree!\n"); + break; + default: + printf("Invalid choice. Please try again.\n"); + } + } while (choice != 0); +} + +int main() +{ + menu(); + return 0; +} \ No newline at end of file