Assignment 3 Answers Dsa
Assignment 3 Answers Dsa
#include <stdio.h>
#include <stdlib.h>
free(stack);
free(map);
return ans;
}
int main() {
int nums1[] = {4, 1, 2};
int nums2[] = {1, 3, 4, 2};
int nums1Size = sizeof(nums1) / sizeof(nums1[0]);
int nums2Size = sizeof(nums2) / sizeof(nums2[0]);
free(ans);
return 0;
}
Question 2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char expression[100];
printf("Enter the postfix expression: ");
fgets(expression, sizeof(expression), stdin);
expression[strcspn(expression, "\n")] = 0; // Remove the newline character
return 0;
}
Question 3:
#include <stdio.h>
#include <stdlib.h>
// Pop all elements from the stack and enqueue them back into the queue
while (top != -1) {
enqueue(queue, stack[top--]);
}
}
int main() {
Queue queue;
queue.front = queue.rear = NULL;
int N;
printf("Enter the number of elements in the queue: ");
scanf("%d", &N);
reverseQueue(&queue);
return 0;
}
Question 4:
#include <stdio.h>
#include <stdlib.h>
// Function to construct the binary tree from inorder and postorder traversals
Node* constructTree(int in[], int post[], int inStart, int inEnd, int* postIndex) {
if (inStart > inEnd) {
return NULL;
}
// Create a new node with the last element of the postorder traversal
Node* node = createNode(post[(*postIndex)--]);
return node;
}
int main() {
int n = 7; // Corrected size of the array
int in[] = {4, 8, 2, 5, 1, 3, 7};
int post[] = {8, 4, 5, 2, 7, 3, 1};
int postIndex = n - 1;
Node* root = constructTree(in, post, 0, n - 1, &postIndex);
return 0;
}
Question 5:
#include <stdio.h>
#include <stdlib.h>
// Function to construct the binary tree from the level order array
Node* constructTree(int arr[], int n) {
if (n == 0 || arr[0] == -1) {
return NULL;
}
if (arr[i] != -1) {
parent->left = createNode(arr[i]);
queue[rear++] = parent->left;
}
free(queue);
return root;
}
queue[rear++] = root;
if (currentNode->left != NULL) {
queue[rear++] = currentNode->left;
}
if (currentNode->right != NULL) {
queue[rear++] = currentNode->right;
}
rightMost = currentNode;
}
printf("\n");
free(queue);
}
int main() {
int arr[] = {1,2,3,4,5,NULL,7,8};
int n = sizeof(arr) / sizeof(arr[0]);