Bavi
Bavi
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
void preorderTraversal(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
int main() {
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5)
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n")
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n")
return 0;
}
Exp-5
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
struct Node* insert(struct Node* node, int value) {
if (node == NULL) return createNode(value);
if (value < node->data)
node->left = insert(node->left, value);
else if (value > node->data)
node->right = insert(node->right, value;
return node;
}
struct Node* minValueNode(struct Node* node) {
struct Node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
struct Node* deleteNode(struct Node* root, int key) {
if (root == NULL) return root;
if (key < root->data)
root->left = deleteNode(root->left, key);
else if (key > root->data)
root->right = deleteNode(root->right, key);
else {
if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp;
}
struct Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
struct Node*
exp-3
#include <stdio.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;
void enqueue(int value) {
if (rear == MAX - 1)
printf("Queue Overflow\n");
else {
if (front == -1)
front = 0;
queue[++rear] = value;
printf("Inserted %d\n", value);
}
}
void dequeue() {
if (front == -1 || front > rear)
printf("Queue Underflow\n");
else {
printf("Removed %d\n", queue[front++]);
if (front > rear)
front = rear = -1;
}
}
void display() {
if (front == -1)
printf("Queue is empty\n");
else {
printf("Queue elements are:\n");
for (int i = front; i <= rear; i++)
printf("%d\n", queue[i]);
}
}
int main() {
enqueue(10);
enqueue(20);
display();
dequeue();
display();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("Inserted %d\n", value);
}
void dequeue() {
if (front == NULL)
printf("Queue Underflow\n");
else {
struct Node* temp = front;
front = front->next;
if (front == NULL)
rear = NULL;
printf("Removed %d\n", temp->data);
free(temp);
}
}
void display() {
if (front == NULL)
printf("Queue is empty\n");
else {
struct Node* temp = front;
printf("Queue elements are:\n");
while (temp != NULL) {
printf("%d\n", temp->data);
temp = temp->next;
}
}
}
int main() {
enqueue(10);
enqueue(20);
display();
dequeue();
display();
return 0;
}
Exp-2
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
int num, i;
unsigned long long factorial = 1; // Factorial can grow large, so use 'unsigned
long long'
// Input a number
printf("Enter a number: ");
scanf("%d", &num);
// Ensure the input is non-negative
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate factorial
for(i = 1; i <= num; i++) {
factorial *= i;
}
return 0;
}
#include <stdio.h>
int main() {
int i, num = 13;
// Print multiplication table for 13
printf("Multiplication Table for %d:\n", num);
for(i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
#include <stdio.h>
int main() {
int num;
// Input a number
printf("Enter an integer: ");
scanf("%d", &num);
return 0;
}