diff --git a/DS/C/endsem-omnibus/order.c b/DS/C/endsem-omnibus/order.c new file mode 100644 index 0000000..3789e2c --- /dev/null +++ b/DS/C/endsem-omnibus/order.c @@ -0,0 +1,132 @@ +#include +#include +#include + +// Node structure for binary tree +struct Node { + char data; + struct Node *left, *right; +}; + +// Stack structure for tree construction +struct Stack { + struct Node **array; + int top; + int capacity; +}; + +// Function to create a new node +struct Node* newNode(char data) { + struct Node* node = (struct Node*)malloc(sizeof(struct Node)); + node->data = data; + node->left = node->right = NULL; + return node; +} + +// Stack operations +struct Stack* createStack(int capacity) { + struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); + stack->capacity = capacity; + stack->top = -1; + stack->array = (struct Node**)malloc(stack->capacity * sizeof(struct Node*)); + return stack; +} + +void push(struct Stack* stack, struct Node* item) { + stack->array[++stack->top] = item; +} + +struct Node* pop(struct Stack* stack) { + if (stack->top == -1) return NULL; + return stack->array[stack->top--]; +} + +// Search for index in inorder array +int search(char arr[], char x, int n) { + for (int i = 0; i < n; i++) + if (arr[i] == x) + return i; + return -1; +} + +// Build tree from given inorder and preorder traversals +struct Node* buildTree(char inorder[], char preorder[], int inStart, int inEnd, int* preIndex, int n) { + if (inStart > inEnd) return NULL; + + struct Node* node = newNode(preorder[*preIndex]); + (*preIndex)++; + + if (inStart == inEnd) return node; + + int inIndex = search(inorder, node->data, n); + + node->left = buildTree(inorder, preorder, inStart, inIndex-1, preIndex, n); + node->right = buildTree(inorder, preorder, inIndex+1, inEnd, preIndex, n); + + return node; +} + +// Different traversal functions +void printPreorder(struct Node* node) { + if (node == NULL) return; + printf("%c ", node->data); + printPreorder(node->left); + printPreorder(node->right); +} + +void printInorder(struct Node* node) { + if (node == NULL) return; + printInorder(node->left); + printf("%c ", node->data); + printInorder(node->right); +} + +void printPostorder(struct Node* node) { + if (node == NULL) return; + printPostorder(node->left); + printPostorder(node->right); + printf("%c ", node->data); +} + +// Convert between different traversals +void convertTraversal(char input[], char type1[], char type2[], int n) { + // Create arrays to store traversals + char inorder[100], preorder[100]; + int preIndex = 0; + + // If input is preorder, use it directly + if (strcmp(type1, "preorder") == 0) { + strcpy(preorder, input); + strcpy(inorder, type2); + } + // If input is inorder, use it with preorder + else if (strcmp(type1, "inorder") == 0) { + strcpy(inorder, input); + strcpy(preorder, type2); + } + + // Build tree from traversals + struct Node* root = buildTree(inorder, preorder, 0, n-1, &preIndex, n); + + // Print requested traversal + printf("\nConverted traversal: "); + if (strcmp(type2, "preorder") == 0) + printPreorder(root); + else if (strcmp(type2, "inorder") == 0) + printInorder(root); + else if (strcmp(type2, "postorder") == 0) + printPostorder(root); +} + +int main() { + // Example usage + char input[] = "DBEAFC"; // Input traversal + char type1[] = "preorder"; // Type of input traversal + char type2[] = "postorder"; // Required output traversal + int n = strlen(input); + + printf("Input %s traversal: %s\n", type1, input); + convertTraversal(input, type1, type2, n); + + return 0; +} diff --git a/DS/C/endsem-omnibus/queues.c b/DS/C/endsem-omnibus/queues.c new file mode 100644 index 0000000..d94ae2a --- /dev/null +++ b/DS/C/endsem-omnibus/queues.c @@ -0,0 +1,79 @@ +#include +#include +#define MAX 5 + +int queue[MAX]; +int front = -1, rear = -1; + +void enqueue() { + int item; + if(rear == MAX-1) { + printf("\nQueue is Full"); + } + else { + printf("\nEnter element to insert: "); + scanf("%d", &item); + if(front == -1 && rear == -1) { + front = rear = 0; + } + else { + rear++; + } + queue[rear] = item; + printf("\nItem inserted successfully"); + } +} + +void dequeue() { + if(front == -1 || front > rear) { + printf("\nQueue is Empty"); + } + else { + printf("\nDeleted element is: %d", queue[front]); + if(front == rear) { + front = rear = -1; + } + else { + front++; + } + } +} + +void display() { + int i; + if(front == -1 || front > rear) { + printf("\nQueue is Empty"); + } + else { + printf("\nQueue elements are:\n"); + for(i = front; i <= rear; i++) { + printf("%d ", queue[i]); + } + } +} + +int main() { + int choice; + while(1) { + printf("\n\n1. Insert\n2. Delete\n3. Display\n4. Exit"); + printf("\nEnter your choice: "); + scanf("%d", &choice); + + switch(choice) { + case 1: + enqueue(); + break; + case 2: + dequeue(); + break; + case 3: + display(); + break; + case 4: + exit(0); + default: + printf("\nInvalid Choice"); + } + } + return 0; +} diff --git a/DS/C/endsem-omnibus/stacks.c b/DS/C/endsem-omnibus/stacks.c new file mode 100644 index 0000000..4113d64 --- /dev/null +++ b/DS/C/endsem-omnibus/stacks.c @@ -0,0 +1,79 @@ +#include +#define MAX_SIZE 100 + +int top = -1; +int stack[MAX_SIZE]; + +void push(int c) { + if (top < MAX_SIZE - 1) { + stack[++top] = c; + } else { + printf("Stack overflow\n"); + } +} + +int pop() { + if (top >= 0) { + return stack[top--]; + } else { + printf("Stack underflow\n"); + return -1; + } +} + +void display() { + printf("The stack is: "); + for (int i = top; i >= 0; i--) { + printf("%d\t", stack[i]); + } + printf("\n"); +} + +int isEmpty() { + return top == -1; +} + +int peek() { + if (!isEmpty()) { + return stack[top]; + } else { + printf("Stack is empty\n"); + return -1; + } +} + +int main() { + int choice; + while (1) { + printf("1. Push\n2. Pop\n3. Display\n4. Peek\n5. Check if Empty\n6. Exit\n"); + scanf("%d", &choice); + switch (choice) { + case 1: + printf("Enter an element: "); + int element; + scanf("%d", &element); + push(element); + break; + case 2: + printf("The top element is: %d\n", pop()); + break; + case 3: + display(); + break; + case 4: + printf("The top element is: %d\n", peek()); + break; + case 5: + if (isEmpty()) { + printf("Stack is empty\n"); + } else { + printf("Stack is not empty\n"); + } + break; + case 6: + return 0; + default: + printf("Invalid choice.\n"); + } + } +} diff --git a/DS/C/endsem-omnibus/treefunc.c b/DS/C/endsem-omnibus/treefunc.c new file mode 100644 index 0000000..0b9a41e --- /dev/null +++ b/DS/C/endsem-omnibus/treefunc.c @@ -0,0 +1,363 @@ + #include + #include + #define MAX 10 + #define MAXQ 10 + + // Node structure for binary tree - contains data and pointers to left and right children + typedef struct Node { + int data; + struct Node* lchild; + struct Node* rchild; + } Node; + + // Queue structure for level order traversal + typedef struct { + Node* arr[MAX]; + int front; + int rear; + } queue; + + // Check if queue is full + int isFullq(queue* q) { + return (q->rear == MAX-1); + } + + // Check if queue is empty + int isEmptyq(queue* q) { + return (q->front == -1); + } + + // Add element to queue + void enqueue(queue* q, Node* value) { + if(isFullq(q)) { + printf("Overflow"); + return; + } + else { + q->arr[++(q->rear)] = value; + if(q->front == -1) { + q->front = 0; + } + } + } + + // Remove and return element from queue + Node* dequeue(queue* q) { + Node* x; + if(isEmptyq(q)) { + printf("Underflow"); + return NULL; + } + else if(q->front == q->rear) { + x = q->arr[q->front]; + q->front = q->rear = -1; + return x; + } + else { + x = q->arr[(q->front)++]; + return x; + } + } + + // Stack structure for iterative traversals + typedef struct { + Node* arr[MAX]; + int top; + } STACK; + + // Check if stack is full + int isFull(STACK* s) { + return s->top == MAX-1; + } + + // Check if stack is empty + int isEmpty(STACK* s) { + return s->top == -1; + } + + // Push element onto stack + void push(STACK* s, Node* value) { + if(isFull(s)) { + printf("Stack Overflow"); + return; + } + else { + s->arr[++(s->top)] = value; + } + } + + // Pop and return element from stack + Node* pop(STACK* s) { + if(isEmpty(s)) { + printf("Stack underflow"); + return NULL; + } + else { + return s->arr[(s->top)--]; + } + } + + // Create new tree node + Node* getNode() { + Node* newNode = (Node*)malloc(sizeof(Node)); + newNode->data = 0; + newNode->lchild = NULL; + newNode->rchild = NULL; + return newNode; + } + + // Recursively create binary tree + Node* createTree(int item) { + if(item != -1) { + Node* t = getNode(); + t->data = item; + + int x; + printf("Enter the left child of %d (-1 for no child): ", item); + scanf("%d", &x); + t->lchild = createTree(x); + + printf("Enter the right child of %d (-1 for no child): ", item); + scanf("%d", &x); + t->rchild = createTree(x); + + return t; + } + return NULL; + } + + // Recursive preorder traversal (Root-Left-Right) + void preorder(Node* root) { + if(root != NULL) { + printf("%d ", root->data); + preorder(root->lchild); + preorder(root->rchild); + } + } + + // Recursive inorder traversal (Left-Root-Right) + void inorder(Node* root) { + if(root != NULL) { + inorder(root->lchild); + printf("%d ", root->data); + inorder(root->rchild); + } + } + + // Recursive postorder traversal (Left-Right-Root) + void postorder(Node* root) { + if(root != NULL) { + postorder(root->lchild); + postorder(root->rchild); + printf("%d ", root->data); + } + } + + // Level order traversal using queue + void levelorder(Node* root) { + if(root == NULL) return; + queue q; + q.front = -1; + q.rear = -1; + Node* t = root; + printf("%d ", t->data); + enqueue(&q, t); + while(!isEmptyq(&q)) { + t = dequeue(&q); + if(t->lchild) { + printf("%d ", t->lchild->data); + enqueue(&q, t->lchild); + } + if(t->rchild) { + printf("%d ", t->rchild->data); + enqueue(&q, t->rchild); + } + } + } + + // Iterative preorder traversal using stack + void itrpre(Node* root) { + STACK s; + s.top = -1; + Node* t = root; + while(t != NULL || !isEmpty(&s)) { + if(t != NULL) { + printf("%d ", t->data); + push(&s, t); + t = t->lchild; + } + else { + t = pop(&s); + t = t->rchild; + } + } + } + + // Iterative inorder traversal using stack + void itrin(Node* root) { + STACK s; + s.top = -1; + Node* t = root; + while(t != NULL || !isEmpty(&s)) { + if(t != NULL) { + push(&s, t); + t = t->lchild; + } + else { + t = pop(&s); + printf("%d ", t->data); + t = t->rchild; + } + } + } + + // Iterative postorder traversal using stack + void itrpost(Node* root) { + STACK s; + s.top = -1; + Node* t = root; + Node* visit = NULL; + while(t != NULL || !isEmpty(&s)) { + if(t != NULL) { + push(&s, t); + t = t->lchild; + } + else { + Node* p = s.arr[s.top]; + if(p->rchild != NULL && p->rchild != visit) + t = p->rchild; + else { + printf("%d ", p->data); + visit = pop(&s); + } + } + } + } + + // Count total number of nodes in tree + int count(Node* root) { + Node* p = root; + int x, y; + if(p != NULL) { + x = count(p->lchild); + y = count(p->rchild); + return x + y + 1; + } + return 0; + } + + // Calculate sum of all node values in tree + int sumnodes(Node* root) { + Node* p = root; + int x, y; + if(p != NULL) { + x = sumnodes(p->lchild); + y = sumnodes(p->rchild); + return x + y + p->data; + } + return 0; + } + + // Search for element in tree + int Search(Node* root, int ele) { + if(root == NULL) + return 0; + if(root->data == ele) + return 1; + if(Search(root->lchild, ele)) + return 1; + return Search(root->rchild, ele); + } + + // Display menu of operations + void displayMenu() { + printf("\n=== Binary Tree Operations ===\n"); + printf("1. Create a new tree\n"); + printf("2. Recursive Preorder Traversal\n"); + printf("3. Recursive Inorder Traversal\n"); + printf("4. Recursive Postorder Traversal\n"); + printf("5. Iterative Preorder Traversal\n"); + printf("6. Iterative Inorder Traversal\n"); + printf("7. Iterative Postorder Traversal\n"); + printf("8. Level Order Traversal\n"); + printf("9. Count Nodes\n"); + printf("10. Sum of Nodes\n"); + printf("11. Search Element\n"); + printf("0. Exit\n"); + printf("Enter your choice: "); + } + + int main() { + Node* root = NULL; + int choice, rootnum, searchEle; + + do { + displayMenu(); + scanf("%d", &choice); + + switch(choice) { + case 1: + printf("Enter data for root node: "); + scanf("%d", &rootnum); + root = createTree(rootnum); + printf("Tree created successfully!\n"); + break; + case 2: + printf("Recursive Preorder Traversal: "); + preorder(root); + printf("\n"); + break; + case 3: + printf("Recursive Inorder Traversal: "); + inorder(root); + printf("\n"); + break; + case 4: + printf("Recursive Postorder Traversal: "); + postorder(root); + printf("\n"); + break; + case 5: + printf("Iterative Preorder Traversal: "); + itrpre(root); + printf("\n"); + break; + case 6: + printf("Iterative Inorder Traversal: "); + itrin(root); + printf("\n"); + break; + case 7: + printf("Iterative Postorder Traversal: "); + itrpost(root); + printf("\n"); + break; + case 8: + printf("Level Order Traversal: "); + levelorder(root); + printf("\n"); + break; + case 9: + printf("Number of nodes: %d\n", count(root)); + break; + case 10: + printf("Sum of all nodes: %d\n", sumnodes(root)); + break; + case 11: + printf("Enter element to search: "); + scanf("%d", &searchEle); + if(Search(root, searchEle)) + printf("Element found in the tree\n"); + else + printf("Element not found in the tree\n"); + break; + case 0: + printf("Exiting program...\n"); + break; + default: + printf("Invalid choice! Please try again.\n"); + } + } while(choice != 0); + + return 0; + }