PROGRAM:
#include <stdio.h>
#define MAX_SIZE 10
void insertElement(int arr[], int *n, int pos, int
value) { if (*n >= MAX_SIZE) {
printf("Array is full, cannot insert.\n");
return; } if (pos < 0 || pos > *n)
{ printf("Invalid
position!\n"); return; }
for (int i = *n; i > pos; i--) {
arr[i] = arr[i - 1];
} arr[pos] =
value;
(*n)++; }
void deleteElement(int arr[], int *n, int
pos) { if (*n <= 0) {
printf("Array is empty, cannot
delete.\n"); return; } if (pos < 0 || pos >= *n)
{ printf("Invalid
position!\n"); return;
} for (int i = pos; i < *n - 1;
i++)
{ arr[i] = arr[i + 1];
}
(*n)--; // Decrease array size
}
void displayArray(int arr[], int n)
{ if (n == 0) {
printf("Array is
empty.\n"); return; }
printf("Array elements:
"); for (int i = 0; i < n;
i++) { printf("%d ",
arr[i]);
}
printf("\n");
}
int main() {
int arr[MAX_SIZE]; // Array
storage int n = 0; // Start with an
empty array int choice, pos,
value;
while (1) {
printf("\nMenu:\n");
printf("1. Insert
Element\n");
printf("2. Delete
Element\n"); printf("3.
Display Array\n");
printf("4. Exit\n");
printf("Enter your
choice: "); scanf("%d",
&choice);
Switch
(choice)
{ case 1:
printf("Enter position to insert : ", n);
scanf("%d", &pos); printf("Enter value to
insert: "); scanf("%d",
&value);
insertElement(arr, &n, pos, value);
break; case 2: if (n == 0) {
printf("Array is empty, cannot delete.\n");
} else { printf("Enter position to delete :
", n - 1); scanf("%d",&pos);
deleteElement(arr, &n, pos);
}
break;
case 3:
displayArray(arr, n); break;
case 4:
printf("Exiting program.\n"); return
0; default: printf("Invalid choice,\n");
}
}
return 0;
}
OUTPUT
:
PROGRAM(STACK):
#include
<stdio.h> struct
stack {
int a[10]; int
top; }; struct
stack s1; void
push(int);
void pop();
void
display(); int
main() {
s1.top= -1; int
choice,ele;
do{
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice"); scanf("%d",
&choice);
switch(choice)
{ case 1:
printf("Enter the element to be push
scanf("%d", &ele);
push(ele); break; case 2: pop();
break; case 3: display(); break;
case 4: printf("Exiting the
program\n"); break; default:
printf("Invalid choice!\n");
} } while(choice != 4); return 0; }
void push(int x) { if (s1.top != 9) {
s1.top =s1.top + 1; s1.a[s1.top] = x;
printf("Element %d inserted\n", x);
}
else
{ printf("Stack is completely
full\n");
} } void pop()
{ if(s1.top == -
1) s{
printf("Stack is
completely
empty\n");
}
else
{ int ele = s1.a[s1.top]; printf("The deleted
element is %d\n", ele); s1.top--;
} } void
display() {
if(s1.top == -1)
{
printf("Stack is completely empty\n");
}
else
{ printf("Stack elements
are:\n");
for(int i = s1.top; i >= 0; i--)
{
printf("%d\n", s1.a[i]);
}
}
}
OUTPUT:
PROGRAM(QUEUE):
#include <stdio.h>
#define MAX 10
struct queue
{ int a[MAX];
int front, rear;
}; struct queue
q1;
void
enqueue(int);
void dequeue();
void display();
int main()
{ q1.front = 0;
q1.rear = -1;
int choice,
ele;
do {
printf("\nMenu:\n1. Enqueue\n2. Dequeue\n3. Display\n4.
Exit\n"); printf("Enter your choice: "); scanf("%d", &choice);
switch (choice)
{ case 1:
printf("Enter the element to enqueue:
"); scanf("%d", &ele); enqueue(ele);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4: printf("Exiting the
program...\n"); break;
default:
printf("Invalid choice! Try again.\n");
}
} while (choice != 4);
return 0;
}
void enqueue(int x) {
if (q1.rear == MAX - 1)
{ printf("Cannot insert %d\n", x);
} else { q1.a[++q1.rear] = x; printf("Element
%d enqueued to queue\n", x);
}
}
void dequeue() {
if (q1.front > q1.rear) {
printf("No elements to dequeue.\n");
} else { printf("Dequeued element: %d\n",
q1.a[q1.front]); q1.front++;
}
}
void display() {
if (q1.front > q1.rear) {
printf("Queue is empty!\n");
} else { printf("Queue elements: "); for
(int i = q1.front; i <= q1.rear; i++)
{ printf("%d ", q1.a[i]);
}
printf("\n");
}
}
OUTPUT:
PROGRAM:
#include <stdio.h>
#define MAX 20
typedef struct
queue { int
data[MAX]; int
front, rear; } queue;
void init(queue
*q) { q->front = -
1; q->rear = -1; }
int isEmpty(queue *q)
{ return (q->front == -1);
}
int isFull(queue *q) {
return ((q->rear + 1) % MAX == q->front);
}
void enqueue(queue *q, int x)
{ if (isFull(q)) { printf("Queue is full! Cannot
enqueue %d.\n", x);
return; } if
(isEmpty(q)) {
q->front = q->rear = 0;
} else { q->rear = (q->rear + 1) %
MAX;
} q->data[q->rear] = x; printf("%d
enqueued to queue.\n", x);
}
int dequeue(queue *q)
{ if (isEmpty(q)) { printf("Queue is empty!
Cannot dequeue.\n");
return -1; } int x = q-
>data[q->front]; if (q-
>front == q->rear) {
q->front = q->rear = -1;
} else {
q->front = (q->front + 1) % MAX;
}
printf("%d dequeued from queue.\n", x);
return x;
}
void display(queue *q)
{ if (isEmpty(q)) {
printf("Queue is
empty!\n"); return; }
printf("Queue elements:
"); int i = q->front; while
(1) {
printf("%d ", q->data[i]);
if (i == q->rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}
int main() {
queue q;
int x, ch;
init(&q);
do {
printf("\nMenu\n");
printf("1 - Insert\n2 - Delete\n3 - Display\n4 - Exit\n");
printf("Enter your choice: "); scanf("%d", &ch);
switch (ch)
{ case 1:
if (!isFull(&q)) {
printf("Enter data: ");
scanf("%d", &x);
enqueue(&q, x);
} else {
printf("Queue is full!\n");
}
break;
case 2:
if (!isEmpty(&q))
{x=
dequeue(&q);
} else { printf("Queue is
empty!\n");
}
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
} } while (ch
!= 4);
return 0;
}
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <ctype.h>
#define MAX 50
typedef struct stack {
char data[MAX]; int
top; } stack;
int precedence(char); void init(stack
*); int empty(stack *); int full(stack
*); char pop(stack *); void
push(stack *, char); char top(stack
*); void infix_to_postfix(char[],
char[]);
int main() { char infix[30],
postfix[30];
printf("\nEnter an infix expression: ");
scanf("%s", infix);
infix_to_postfix(infix, postfix);
printf("\nPostfix: %s\n", postfix);
return 0; }
void infix_to_postfix(char infix[], char postfix[]) {
stack s; char x; int i, j = 0; char token; init(&s);
for (i = 0; infix[i] != '\0'; i++) { token
= infix[i];
if (isalnum(token)) {
postfix[j++] = token;
} else { switch
(token) {
case '(':
push(&s, token);
break;
case ')':
while (!empty(&s) && (x = pop(&s)) != '(') {
postfix[j++] = x;
} break; default:
while (!empty(&s) && precedence(token) <= precedence(top(&s))) {
postfix[j++] = pop(&s);
} push(&s,
token); break; }
} } while (!empty(&s))
{ postfix[j++] =
pop(&s);
}
postfix[j] = '\0'; } int
precedence(char x) {
switch (x) { case '+':
case '-': return 1; case '*':
case '/': return 2; case '^':
return 3; default: return
0;
} } void init(stack
*s) { s->top = -1; }
int empty(stack *s) {
return (s->top == -1);
}
int full(stack *s) { return (s-
>top == MAX - 1);
}
void push(stack *s, char x) { if
(!full(s)) {
s->data[++(s->top)] = x;
} } char pop(stack *s) {
if (!empty(s)) { return s-
>data[(s->top)--];
}
return '\0'; } char
top(stack *s) { if
(!empty(s)) {
return s->data[s->top];
}
return '\0';
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <ctype.h>
#define MAX 50
typedef struct stack {
int data[MAX];
int top; } stack;
void init(stack *); int
empty(stack *); int
full(stack *); void
push(stack *, int); int
pop(stack *); int
evaluate_postfix(char[]);
int main() {
char postfix[30]; printf("\nEnter a
postfix expression: "); scanf("%s",
postfix);
printf("\nResult: %d\n",
evaluate_postfix(postfix)); return 0; }
int evaluate_postfix(char postfix[])
{ stack s; int i, op1,
op2, result; char
token; init(&s);
for (i = 0; postfix[i] != '\0'; i++)
{ token = postfix[i];
if (isdigit(token))
{ push(&s, token - '0');
} else { op2 =
pop(&s); op1 =
pop(&s);
switch (token) {
case '+': result = op1 + op2; break;
case '-': result = op1 - op2; break;
case '*': result = op1 * op2; break;
case '/': result = op1 / op2; break;
} push(&s,
result);
}
} return
pop(&s); }
void init(stack *s)
{ s->top = -1;
}
int empty(stack *s)
{ return (s->top == -
1);
}
int full(stack *s) {
return (s->top == MAX - 1);
}
void push(stack *s, int x)
{ if (!full(s)) { s->data[++(s-
>top)] = x;
}
}
int pop(stack *s)
{ if (!empty(s))
{
return s->data[(s->top)--
]; } return -1;
}
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{ int data; struct
Node* next;
} Node;
Node* insert(Node* head, int value) { Node*
newNode = (Node*)malloc(sizeof(Node));
newNode->data = value; newNode->next =
NULL;
if (head == NULL)
{ return newNode;
}
Node* temp = head; while
(temp->next != NULL) {
temp = temp->next; } temp-
>next = newNode; return
head;
}
Node* delete(Node* head, int value)
{ if (head == NULL)
{ printf("List is empty!\n");
return head;
}
Node* temp = head;
Node* prev = NULL;
if (head->data ==
value) { head = head-
>next; free(temp);
return head; }
while (temp != NULL && temp->data != value)
{ prev = temp;
temp = temp->next; }
if (temp == NULL) {
printf("Element not
found!\n"); return
head; }
prev->next = temp->next;
free(temp);
return head; }
void traverse(Node* head)
{ if (head == NULL)
{ printf("List is empty!\n");
return;
}
Node* temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
Node* head = NULL;
int choice, value;
do {
printf("\nMenu\n1 - Insert\n2 - Delete\n3 - Traverse\n4 - Exit\nEnter your choice:
"); scanf("%d", &choice);
switch (choice)
{ case 1:
printf("Enter value to insert:
"); scanf("%d", &value); head
= insert(head, value);
break;
case 2: printf("Enter value to
delete: "); scanf("%d",
&value); head = delete(head,
value);
break;
case 3:
traverse(head);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
} } while (choice
!= 4);
return 0;
}
OUTPUT:
PROGRAM:
#include <stdio.h> #include
<stdlib.h>
typedef struct Node { int data;
struct Node* prev; struct Node* next;
} Node;
Node* insert(Node* head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data =
value;
newNode->prev = NULL; newNode->next =
head;
if (head != NULL) {
head->prev = newNode;
}
return newNode;
}
Node* delete(Node* head, int value) { if (head == NULL)
{ printf("List is empty!\n"); return head;
}
Node* temp = head;
while (temp != NULL && temp->data != value) { temp = temp->next;
}
if (temp == NULL) { printf("Element not found!\n");
return head;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
} else { head = temp-
>next;
} if (temp->next !=
NULL) { temp->next-
>prev = temp->prev;
}
free(temp); return head;
}
void traverse(Node* head) { if (head == NULL)
{ printf("List is empty!\n"); return;
}
Node* temp = head; printf("Doubly Linked List: ");
while (temp != NULL) { printf("%d <-> ", temp-
>data); temp = temp->next;
}
printf("NULL\n");
}
int main() {
Node* head = NULL; int choice, value;
do {
printf("\nMenu\n1 - Insert\n2 - Delete\n3 - Traverse\n4 - Exit\nEnter your choice: "); scanf("%d", &choice);
switch (choice) { case 1:
printf("Enter value to insert: "); scanf("%d", &value);
head = insert(head, value); break;
case 2: printf("Enter value to delete: "); scanf("%d",
&value); head = delete(head, value); break;
case 3: traverse(head);
break;
case 4:
printf("Exiting...\n"); break;
default:
printf("Invalid choice! Try again.\n");
} } while (choice
!= 4);
return 0;
}
OUTPUT:
PROGRAM:
#include <stdio.h> #include
<stdlib.h>
struct Node { int data;
struct Node* next;
};
void push(struct Node** top, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (!newNode)
{ printf("Memory allocation failed\n"); return;
}
newNode->data= value; newNode>next
= *top;
*top = newNode;
printf("%d pushed onto the stack\n", value);
}
int pop(struct Node** top) { if (*top ==
NULL) {
printf("StackUnderflow!\n"); return -1;
}
struct Node* temp = *top; int val = temp>data;
*top=(*top)->next; free(temp); return
val;
}
int peek(struct Node* top) { return (top) ? top->data
: -1;
}
voiddisplay(struct Node* top) { if (!top)
{ printf("Stack is empty\n"); return;
}
printf("Stack: "); while (top) {
printf("%d -> ", top->data); top = top->next;
}
printf("NULL\n");
}
int main() {
struct Node* stack= NULL; int choice, value;
while (1) { printf("\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: "); scanf("%d", &choice);
switch(choice){ case 1:
printf("Entervalue to push: "); scanf("%d",
&value); push(&stack, value); break;
case 2: value = pop(&stack); if (value
!= -1) printf("Popped: %d\n", value);
break;
case 3: value = peek(stack); if (value !=
-1) printf("Top element: %d\n",
value);
else printf("Stack is
empty\n");
break;
case 4:
display(stack);
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Try again.\n");
}
} return
0;
}
OUTPUT:
PROGRAM:
#include <stdio.h>
#include<stdlib.h>
struct Node
{ int data;
struct Node* next;
};
struct Queue {
struct Node *front, *rear;
};
void enqueue(struct Queue* q, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next= NULL;
if (q->rear == NULL) {
q->front = q->rear = newNode;
} else { q->rear->next =
newNode; q->rear =
newNode;
}
printf("%d enqueued\n", value);
}
int dequeue(struct Queue* q)
{ if (q->front == NULL)
{ printf("Queue is
empty!\n"); return -1;
} struct Node* temp = q->front; int val =
temp->data; q->front = q->front->next;
if (q->front == NULL) q->rear = NULL;
free(temp);
printf("Dequeued: %d\n", val);
return val;
}
voiddisplay(struct Queue* q)
{ if (q->front == NULL)
{ printf("Queue is
empty!\n"); return;
}
struct Node* temp = q->front;
printf("Queue: ");
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Queue q = {NULL, NULL};
int choice, value;
while (1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\nEnter choice: ");
scanf("%d", &choice);
switch (choice)
{ case 1:
printf("Entervalue to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3: display(&q);
break;
case 4: printf("Exiting
program.\n"); return 0;
default: printf("Invalid choice, try
again.\n");
}
} return
0;
}
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
struct Node { int
data; struct Node*
next; struct Node*
prev;
};
struct Node* front = NULL;
struct Node* rear = NULL;
void insertFront(int value) { struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node)); newNode->data = value;
newNode->next = front; newNode->prev = NULL; if (front !=
NULL) front->prev = newNode; front = newNode; if (rear ==
NULL) rear = newNode;
}
void insertRear(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value; newNode->next = NULL; newNode->prev
= rear; if (rear != NULL) rear->next = newNode; rear = newNode;
if (front == NULL) front = newNode;
}
void deleteFront()
{ if (front ==
NULL)
{ printf("Deque is
empty!\n"); return;
} struct Node* temp = front; front =
front->next; if (front != NULL) front-
>prev = NULL; else rear = NULL;
free(temp);
}
void deleteRear() {
if (rear == NULL) {
printf("Deque is empty!\n"); return;
} struct Node* temp = rear; rear = rear-
>prev; if (rear != NULL) rear->next =
NULL; else front = NULL;
free(temp);
}
void displayDeque() { struct
Node* temp = front; if
(temp == NULL) {
printf("Deque is
empty!\n"); return; } while
(temp) {
printf("%d -> ", temp-
>data); temp = temp->next; }
printf("NULL\n");
}
Output :
int main() {
int choice, value;
do {
printf("\nMenu:\n1. Insert Front\n2. Insert Rear\n3. Delete
Front\n4. Delete Rear\n5. Display\n6. Exit\nEnter choice: ");
scanf("%d", &choice); switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(value); break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(value); break;
case 3:
deleteFront();
break;
case 4:
deleteRear();
break;
case 5:
displayDeque();
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
} } while (choice
!= 6); return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node { int
data; struct node
*right; struct
node *left;
};
struct node *root = NULL;
struct node *insert(struct node *root, int n)
{ struct node *temp=root; struct node *new=(struct node
*)malloc(sizeof(struct node)); new->data=n; new-
>right=NULL; new->left=NULL; if(root==NULL)
{ root=new; return root; }
if(new->data < temp-
>data) temp-
>left=insert(temp->left,n);
else if(new->data > temp->data)
temp->right=insert(temp->right,n);
else
{ printf("Element already exists");
free(new); } return root; }
struct node *delete(struct node *root, int m)
{ struct node *pt, *temp, *fp, *l;
temp = root; pt = NULL;
while (temp != NULL)
{ if (temp->data == m)
break;
else if (temp->data > m)
{ pt = temp; temp =
temp->left;
}
else
{ pt = temp; temp =
temp->right;
}
}
if (temp == NULL)
{ printf("\nElement not found\n");
return root; }
if (temp->left == NULL && temp->right == NULL)
{ if (pt == NULL)
return NULL;
if (pt->left == temp)
pt->left = NULL;
else
pt->right =
NULL; free(temp); }
else if (temp->left != NULL && temp->right == NULL)
{ if (pt == NULL)
root = temp->left;
else if (pt->left == temp)
pt->left = temp->left;
else
pt->right = temp->left;
free(temp);
}
else if (temp->left == NULL && temp->right != NULL)
{ if (pt == NULL)
root = temp->right;
else if (pt->left == temp)
pt->left = temp->right;
else
pt->right = temp->right;
free(temp);
}
else
{ struct node *succ = temp->right;
struct node *succParent = temp;
while (succ->left != NULL)
{ succParent = succ;
succ = succ->left; }
temp->data = succ->data;
if (succParent->left == succ)
succParent->left = succ->right;
else
succParent->right = succ->right;
free(succ); }
return root;
}
void inorder(struct node *root)
{ if (root != NULL)
{ inorder(root->left);
printf("%d\t", root-
>data); inorder(root-
>right);
}
}
void preorder(struct node *root)
{ if (root != NULL)
{ printf("%d\t", root>data);
preorder(root-
>left); preorder(root-
>right);
}
}
void postorder(struct node *root)
{ if (root != NULL)
{ postorder(root->left);
postorder(root->right);
printf("%d\t", root-
>data);
}
}
int main() {
int ch, n,
m;
do {
printf("\n1. Insert");
printf("\n2. Delete ");
printf("\n3. Inorder");
printf("\n4. Preorder");
printf("\n5. Postorder");
printf("\n6. Exit");
printf("\nEnter your choice:
"); scanf("%d", &ch);
switch (ch)
{ case 1:
printf("Enter element to add:
"); scanf("%d", &n); root =
insert(root, n); break;
case 2:
if (root == NULL)
{ printf("\nTree is empty\n");
} else { printf("Enter element to
delete: "); scanf("%d", &m); root
= delete(root, m);
}
break;
case 3:
if (root == NULL)
printf("\nTree is empty\n");
else {
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
}
break;
case 4:
if (root == NULL)
printf("\nTree is empty\n");
else {
printf("Preorder Traversal:
"); preorder(root);
printf("\n");
}
break;
case 5:
if (root == NULL)
printf("\nTree is empty\n");
else {
printf("Postorder Traversal:
"); postorder(root);
printf("\n");
}
break;
case 6:
printf("Exiting...\n");
break;
} } while (ch
!= 6);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node { int
data; struct node
*right; struct
node *left;
};
struct node *root = NULL;
struct node *insert(struct node *root, int n) { struct node *temp
= root; struct node *new = (struct node *)malloc(sizeof(struct
node)); new->data = n; new->right = NULL; new->left =
NULL;
if (root == NULL)
{ root = new;
return root;
}
if (n < temp->data)
temp->left = insert(temp->left, n);
else if (n > temp->data)
temp->right = insert(temp->right, n);
else {
printf("Element already
exists.\n"); free(new); } return root; }
struct node *delete(struct node *root, int m)
{ struct node *pt, *temp, *succ,
*succParent; pt = NULL;
temp = root;
while (temp != NULL)
{ if (temp->data == m)
break;
pt = temp; temp = (m < temp->data) ? temp->left :
temp->right;
}
if (temp == NULL)
{ printf("Element not found.\n");
return root;
}
// Case 1: Leaf Node if (temp->left == NULL &&
temp->right == NULL)
{ if (pt == NULL) return NULL; if
(pt->left == temp) pt->left = NULL;
else pt->right = NULL; free(temp);
}
// Case 2: One child else if (temp->left == NULL ||
temp->right == NULL) {
struct node *child = (temp->left) ? temp->left : temp->right;
if (pt == NULL) root = child;
else if (pt->left == temp)
pt->left = child;
else
pt->right = child;
free(temp);
// Case 3: Two children
else {
succ = temp->right;
succParent = temp; while
(succ->left != NULL) {
succParent = succ; succ =
succ->left; } temp->data =
succ->data; if (succParent-
>left == succ)
succParent->left = succ->right;
else
succParent->right = succ->right;
free(succ);
}
return root;
}
void inorder(struct node *root)
{ if (root) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void preorder(struct node *root)
{ if (root) { printf("%d ",
root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{ if (root) { postorder(root-
>left); postorder(root-
>right); printf("%d ",
root->data);
}
}
// Count total nodes
int countNodes(struct node *root) { if (root == NULL) return 0;
return 1 + countNodes(root->left) + countNodes(root-
>right);
}
// Count leaf nodes int countLeafNodes(struct node *root) { if (root
== NULL) return 0; if (root->left == NULL && root->right ==
NULL) return 1; return countLeafNodes(root->left) +
countLeafNodes(root->right);
}
// Find smallest value int findSmallest(struct node
*root) { if (root == NULL) return -1; struct node
*temp = root; while (temp->left != NULL) temp =
temp->left; return temp->data;
}
// Find largest value int
findLargest(struct node *root)
{ if (root == NULL) return -1;
struct node *temp = root; while (temp->right !=
NULL) temp = temp->right; return temp->data;
}
// Find height int findHeight(struct node *root) { if (root ==
NULL) return -1; int leftHeight = findHeight(root->left); int
rightHeight = findHeight(root->right); return (leftHeight >
rightHeight ? leftHeight : rightHeight) + 1;
}
int main() {
int ch, n,
m;
do {
printf("\n\n--- Binary Search Tree Menu ---");
printf("\n1. Insert"); printf("\n2. Delete");
printf("\n3. Inorder Traversal"); printf("\n4.
Preorder Traversal"); printf("\n5. Postorder
Traversal");
printf("\n6. Count Total Nodes");
printf("\n7. Count Leaf Nodes");
printf("\n8. Find Smallest Value");
printf("\n9. Find Largest Value");
printf("\n10. Find Height of
BST"); printf("\n11. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{ case 1:
printf("Enter element to insert:
"); scanf("%d", &n); root =
insert(root, n); break;
case 2:
if (root == NULL)
printf("Tree is empty.\n");
else {
printf("Enter element to delete:
"); scanf("%d", &m); root =
delete(root, m);
}
break;
case 3:
if (root == NULL)
printf("Tree is empty.\n");
else {
printf("Inorder Traversal:
"); inorder(root);
printf("\n");
}
break;
case 4:
if (root == NULL)
printf("Tree is empty.\n");
else {
printf("Preorder Traversal:
"); preorder(root);
printf("\n");
}
break;
case 5:
if (root == NULL)
printf("Tree is empty.\n");
else {
printf("Postorder Traversal:
"); postorder(root);
printf("\n");
}
break;
case 6: printf("Total nodes: %d\n",
countNodes(root)); break;
case 7: printf("Leaf nodes: %d\n",
countLeafNodes(root)); break;
case 8:
if (root == NULL)
printf("Tree is empty.\n");
else
printf("Smallest value: %d\n", findSmallest(root)); break;
case 9:
if (root == NULL)
printf("Tree is empty.\n");
else printf("Largest value: %d\n",
findLargest(root));
break;
case 10: printf("Height of BST: %d\n",
findHeight(root)); break;
case 11:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
}
} while (ch != 11);
return 0;
}
Output :
PROGRAM:
#include <stdio.h>
int fibonacci(int n) { if (n <= 1)
return n; return fibonacci(n - 1) +
fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the position (n) of Fibonacci number: ");
scanf("%d", &n);
int result = fibonacci(n); printf("Fibonacci number at
position %d is %d\n", n, result);
return 0;
}
OUTPUT: