diff --git a/DS/C/Lab/Week11/expression.c b/DS/C/Lab/Week11/expression.c new file mode 100644 index 0000000..499b333 --- /dev/null +++ b/DS/C/Lab/Week11/expression.c @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#define MAX 100 + +typedef struct TNode *Tptr; +typedef struct TNode +{ + char data; + Tptr leftchild; + Tptr rightchild; +}TNode; +///* +//Create nodes in the tree +Tptr createNode(char val) +{ + Tptr temp =(Tptr)malloc(sizeof(TNode)); + temp->data = val; + temp->leftchild = temp->rightchild = NULL; + return temp; +} +//*/ + +Tptr stack[MAX]; +int top=-1; +int i; + +//check if stack is empty +bool isEmpty() +{ + return top==-1; +} + +//check if stack is full +bool isFull() +{ + return top==MAX -1; +} + +//push element onto stack +void Push(Tptr e) +{ + if(isFull()) + { + return; + } + stack[++top]=e; +} + +//pop element from stack +Tptr Pop() +{ + Tptr popped=createNode('\0'); + if(isEmpty()) + { + free(popped); + } + else + { + popped=stack[top--]; + } + return popped; +} + +//check if character is operator +bool isOperator(char ch) +{ + bool ret=false; + if(ch=='*' || ch=='/' || ch=='+' || ch=='-' || ch=='^' ) + { + ret = true; + } + return ret; +} + +Tptr createExpression(char* postfix) +{ + int i; + int plen =strlen(postfix); + char cc; + for(i=0;irightchild = Pop(); + node->leftchild = Pop(); + Push(node); + } + } + return stack[top]; +} + +void inOrderTraversal(Tptr root) +{ + if (root != NULL) + { + inOrderTraversal(root->leftchild); + printf("%c ", root->data); + inOrderTraversal(root->rightchild); + } +} + +int main() +{ + char postfix[] = "ab*cd/-"; + Tptr expressionTree = createExpression(postfix); + + if (expressionTree != NULL) + { + printf("In-Order Traversal of the Expression Tree: "); + inOrderTraversal(expressionTree); + printf("\n"); + } + + return 0; +} diff --git a/DS/C/Lab/Week11/levelOrder.c b/DS/C/Lab/Week11/levelOrder.c new file mode 100644 index 0000000..c94d432 --- /dev/null +++ b/DS/C/Lab/Week11/levelOrder.c @@ -0,0 +1,135 @@ +#include +#include +#define MAX 100 + +typedef struct TNode *Tptr; +typedef struct TNode { + int data; + Tptr leftchild; + Tptr rightchild; +} TNode; + +typedef struct { + Tptr key; +} element; + +element Q[MAX]; +int r = -1; +int f = -1; + +element dequeue() { + element ret = {NULL}; + if (f != r) { + f++; + ret = Q[f]; + } + return ret; +} + +void enqueue(element e) { + if (r < MAX - 1) { // Check if the queue is not full + r++; + Q[r] = e; + } else { + printf("Queue is full\n"); + } +} + +// Create nodes in the tree +Tptr createNode(int val) { + Tptr temp = (Tptr)malloc(sizeof(TNode)); + if (temp) { + temp->data = val; + temp->leftchild = temp->rightchild = NULL; + } + return temp; +} + +// Create the binary search tree +void createBST(Tptr *root) { + int N, i, val; + Tptr current, previous; + + printf("Enter the number of nodes: "); + scanf("%d", &N); + + for (i = 0; i < N; i++) { + printf("Enter a unique value: "); + scanf("%d", &val); + + Tptr temp = createNode(val); + if (!temp) { + printf("Memory allocation failed\n"); + return; + } + + if (!*root) { + *root = temp; + } else { + current = *root; + previous = NULL; + int isDuplicate = 0; // Flag for duplicate + + while (current) { + previous = current; + if (val < current->data) { + current = current->leftchild; + } else if (val > current->data) { + current = current->rightchild; + } else { + printf("Duplicate. Will only be printed for first occurence.\n"); + isDuplicate = 1; // Set flag if duplicate found + break; // Exit loop + } + } + + if (!isDuplicate) { + if (val < previous->data) { + previous->leftchild = temp; + } else { + previous->rightchild = temp; + } + } else { + free(temp); // Free memory for duplicate node + } + } + } +} + +// Print in level order +void levelOrderTraversal(Tptr current) { + if (current == NULL) { + return; + } + + element b; + b.key = current; + enqueue(b); + + while (f != r) { // While the queue is not empty + Tptr currentNode = dequeue().key; + printf("%d ", currentNode->data); + + if (currentNode->leftchild != NULL) { + b.key = currentNode->leftchild; + enqueue(b); + } + + if (currentNode->rightchild != NULL) { + b.key = currentNode->rightchild; + enqueue(b); + } + } +} + +int main() { + Tptr root = NULL; + + createBST(&root); + + printf("Level Order Traversal: "); + levelOrderTraversal(root); + printf("\n"); + + return 0; +}