[go: up one dir, main page]

0% found this document useful (0 votes)
22 views44 pages

Docfile

Uploaded by

Sharad Menon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views44 pages

Docfile

Uploaded by

Sharad Menon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

STACK

#include<stdio.h>

#define MAX 100

typedef struct Stack{

int items[MAX];

int top;

}Stack;

void initialize(Stack *s){

s->top = -1;

int is_full(Stack *s){

return s->top == MAX-1;

int is_empty(Stack *s){

return s->top == -1;

void push(Stack*s, int item){

if(is_full(s)){

printf("Overflow\n");

}else{

s->items[++(s->top)] = item;

int pop(Stack *s){

if(is_empty(s)){

printf("Underflow!!\n");
return -1;

}else{

int item = s->items[(s->top)--];

return item;

void show(Stack *s){

if(is_empty(s)){

printf("Underflow\n");

}else{

printf("Elements present in the stack:\n");

for(int i=0;i<=s->top;i++){

printf("%d", s->items[i]);

printf("\n");

int main(){

Stack s;

int end = 0;

int choice;

int ele;

initialize(&s);

while(end==0){

printf("Operations performed by Stack\n1.Push the element\n2.Pop the element\n3.Show\n4.End\


n");

printf("\n");

printf("Enter the choice:\n");

scanf("%d", &choice);
switch(choice){

case 1:

printf("Enter element to be inserted to the stack:\n");

scanf("%d", &ele);

push(&s, ele);

break;

case 2:

printf("Popped element: %d\n", pop(&s));

break;

case 3:

show(&s);

break;

case 4:

end = 1;

break;

QUEUE
#include<stdio.h>

#include<stdlib.h>

#define MAX 100

typedef struct{

int items[MAX];

int front,rear;
}Queue;

void initializeQueue(Queue *q){

q->front = -1;

q->rear = -1;

int isEmpty(Queue *q){

return q->front==-1;

int isFull(Queue *q){

return q->rear== MAX-1;

void enqueue(Queue *q, int item){

if(isFull(q)){

printf("Overloaded\n");

return;

if(isEmpty(q)){

q->front=0;

q->items[++q->rear] = item;

int dequeue(Queue *q){

if(isEmpty(q)){

printf("Underflow!\n");

return -1;

int item = q->items[q->front];

if(q->front >= q->rear){

q->front = -1;
q->rear = -1;

}else{

q->front++;

return item;

void display(Queue *q){

if(isEmpty(q)){

printf("Queue is empty\n");

}else{

printf("Queue is : \n");

for(int i=q->front;i<=q->rear;i++){

printf("%d ", q->items[i]);

printf("\n");

int main(){

Queue q;

initializeQueue(&q);

int el;

int choice;

while(choice!=4){

printf("1.Insert element to queue\n2.Delete element from queue\n3.Display all elements of queue\


n4.Quit\n");

printf("Enter your choice :\n");

scanf("%d", &choice);

switch(choice){

case 1:
printf("Inset the element in queue :\n");

scanf("%d", &el);

enqueue(&q, el);

break;

case 2:

printf("Element deleted from queue is : %d\n", dequeue(&q));

break;

case 3:

display(&q);

break;

case 4:

break;

LINKED LIST
#include <stdio.h>

#include <stdlib.h>

// Define the node structure

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL; // Initialize the head node to NULL


// Function to insert at the beginning

void insertAtBeginning(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = head;

head = newNode;

printf("Node inserted at the beginning.\n");

// Function to insert at the end

void insertAtLast(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

printf("Node inserted at the last.\n");

// Function to insert at a specific position

void insertAtPosition(int value, int position) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

if (position == 1) {

newNode->next = head;

head = newNode;

printf("Node inserted at position %d.\n", position);

return;

struct Node* temp = head;

for (int i = 1; i < position - 1 && temp != NULL; i++) {

temp = temp->next;

if (temp == NULL) {

printf("Position out of range.\n");

} else {

newNode->next = temp->next;

temp->next = newNode;

printf("Node inserted at position %d.\n", position);

// Function to delete from the beginning

void deleteFromBeginning() {

if (head == NULL) {

printf("List is empty.\n");

return;
}

struct Node* temp = head;

head = head->next;

free(temp);

printf("Node deleted from the beginning.\n");

// Function to delete from the end

void deleteFromLast() {

if (head == NULL) {

printf("List is empty.\n");

return;

if (head->next == NULL) {

free(head);

head = NULL;

} else {

struct Node* temp = head;

struct Node* prev;

while (temp->next != NULL) {

prev = temp;

temp = temp->next;

prev->next = NULL;

free(temp);

printf("Node deleted from the last.\n");

}
// Function to delete node after a specific position

void deleteAfterPosition(int position) {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

for (int i = 1; i < position && temp != NULL; i++) {

temp = temp->next;

if (temp == NULL || temp->next == NULL) {

printf("Position out of range or no node to delete.\n");

} else {

struct Node* toDelete = temp->next;

temp->next = temp->next->next;

free(toDelete);

printf("Node deleted after position %d.\n", position);

// Function to search for an element

void search(int value) {

struct Node* temp = head;

int position = 1;

while (temp != NULL) {

if (temp->data == value) {
printf("Element %d found at position %d.\n", value, position);

return;

temp = temp->next;

position++;

printf("Element %d not found.\n", value);

// Function to display the list

void display() {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

printf("Linked List: ");

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

// Main function to drive the program

int main() {

int choice, value, position;


while (1) {

printf("\nLinked List Operations:\n");

printf("1. Insert at Beginning\n");

printf("2. Insert at Last\n");

printf("3. Insert at Position\n");

printf("4. Delete from Beginning\n");

printf("5. Delete from Last\n");

printf("6. Delete Node after Position\n");

printf("7. Search Element\n");

printf("8. Display List\n");

printf("9. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &value);

insertAtBeginning(value);

break;

case 2:

printf("Enter value to insert: ");

scanf("%d", &value);

insertAtLast(value);

break;

case 3:

printf("Enter value to insert: ");


scanf("%d", &value);

printf("Enter position to insert at: ");

scanf("%d", &position);

insertAtPosition(value, position);

break;

case 4:

deleteFromBeginning();

break;

case 5:

deleteFromLast();

break;

case 6:

printf("Enter position after which to delete: ");

scanf("%d", &position);

deleteAfterPosition(position);

break;

case 7:

printf("Enter element to search: ");

scanf("%d", &value);

search(value);

break;

case 8:

display();

break;
case 9:

exit(0);

break;

default:

printf("Invalid choice. Please try again.\n");

return 0;

BINARY SEARCH

#include<stdio.h>

int binarysearch(int arr[], int n, int target){

int beg = 0;

int end = n-1;

while(beg<=end){

int mid = (beg+end)/2;

if(arr[mid]==target){

return mid+1;

if(arr[mid]<target){

beg = mid+1;

else{

end = mid-1;

}
return -1;

int main(){

int n;

printf("Enter the number of elements\n");

scanf("%d", &n);

int arr[n];

printf("Enter the elements\n");

for(int i=0;i<n;i++){

scanf("%d", &arr[i]);

int ele;

printf("enter the element to be search\n");

scanf("%d", &ele);

int result = binarysearch(arr,n,ele);

if(result!=-1){

printf("%d found in %d position", ele,result);

}else{

printf("%d not found in any position", ele);

INSERTION SORT
#include<stdio.h>

void insertionSort(int arr[], int n){

int i,key,j;

for(i=1;i<n;i++){

key = arr[i];

j = i-1;
while(j>=0 && arr[j]>key){

arr[j+1] = arr[j];

j = j-1;

arr[j+1] = key;

void printArray(int arr[], int n){

int i;

for(i=0;i<n;i++){

printf("%d", arr[i]);

printf("\n");

int main(){

int n;

printf("Enter the total elements\n");

scanf("%d", &n);

int arr[n];

printf("enter the elements\n");

for(int i=0;i<n;i++){

scanf("%d", &arr[i]);

printf("before sorting the elements are...\n");

printArray(arr,n);

printf("After sorting the elements are...\n");

insertionSort(arr, n);

printArray(arr,n);
}

SELECTION SORT
#include<stdio.h>

void selectionSort(int arr[], int n){

int i,j,min;

for(int i=0;i<n-1;i++){

min = i;

for(j = i+1;j<n;j++){

if(arr[j]<arr[min]){

min = j;

int temp = arr[min];

arr[min] = arr[i];

arr[i] = temp;

void printarr(int arr[], int n){

int i;

for(i=0;i<n;i++){

printf("%d\n", arr[i]);

int main(){

int n;

printf("Enter the total elements\n");

scanf("%d", &n);

int arr[n];
printf("enter the elements\n");

for(int i=0;i<n;i++){

scanf("%d", &arr[i]);

printf("before sorting the elements are...");

printarr(arr, n);

printf("After sorting the elements are...\n");

selectionSort(arr,n);

printarr(arr,n);

COUNTING SORT
#include<stdio.h>

void printarray(int arr[], int n){

for(int i=0;i<n;i++){

printf("%d\n", arr[i]);

int findMax(int arr[], int n){

int max = arr[0];

for(int i=0;i<n;i++){

if(arr[i]>max){

max = arr[i];

return max;

void countingSort(int arr[], int n){


int max = findMax(arr,n);

int output[n+1];

int count[max+1];

for(int i=0;i<=max;i++){

count[i]=0;

for(int i=0;i<n;i++){

count[arr[i]]++;

for(int i=1;i<=max;i++){

count[i] += count[i-1];

for(int i=n-1;i>=0;i--){

output[count[arr[i]-1]] = arr[i];

count[arr[i]]--;

for(int i=0; i<n;i++){

arr[i] = output[i];

int main(){

int n;

printf("Enter the total elements in an array\n");

scanf("%d", &n);

int arr[n];

printf("Enter the elements\n");

for(int i=0;i<n;i++){

scanf("%d", &arr[i]);

}
printf("Before sorting...\n");

printarray(arr,n);

printf("After sorting...\n");

countingSort(arr,n);

printarray(arr,n);

QUICK SORT
#include<stdio.h>

void quicksort(int number[25],int first,int last){

int i, j, pivot, temp;

if(first<last){

pivot=first;

i=first;

j=last;

while(i<j){

while(number[i]<=number[pivot]&&i<last)

i++;

while(number[j]>number[pivot])

j--;

if(i<j){

temp=number[i];

number[i]=number[j];

number[j]=temp;

temp=number[pivot];

number[pivot]=number[j];
number[j]=temp;

quicksort(number,first,j-1);

quicksort(number,j+1,last);

int main(){

int i, count, number[25];

printf("Enter the total elements in an array\n");

scanf("%d",&count);

printf("Enter the elements\n");

for(i=0;i<count;i++){

scanf("%d\n",&number[i]);

printf("Before sorting...\n");

for(i=0;i<count;i++){

printf("%d\n", number[i]);

quicksort(number,0,count-1);

printf("After sorting...\n");

for(i=0;i<count;i++){

printf("%d\n",number[i]);

return 0;

DOUBLY LINKED LIST


#include <stdio.h>

#include <stdlib.h>

// Structure for a doubly linked list node

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

// Function to insert a node at the beginning

void insertAtBeginning(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head != NULL) {

newNode->next = *head;

(*head)->prev = newNode;

*head = newNode;

printf("Node inserted\n");

}
// Function to insert a node at the end

void insertAtEnd(struct Node** head, int data) {

struct Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

printf("node inserted\n");

return;

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

printf("node inserted\n");

// Function to insert a node at a specified position (1-based index)

void insertAtPosition(struct Node** head, int data, int position) {

if (position < 1) {

printf("Position should be >= 1.\n");

return;

if (position == 1) {

insertAtBeginning(head, data);

return;

}
struct Node* newNode = createNode(data);

struct Node* temp = *head;

// Traverse to the (position-1)th node

for (int i = 1; i < position - 1 && temp != NULL; i++) {

temp = temp->next;

if (temp == NULL) {

printf("Position does not exist. Node not inserted.\n");

free(newNode);

return;

newNode->next = temp->next;

if (temp->next != NULL) {

temp->next->prev = newNode;

temp->next = newNode;

newNode->prev = temp;

printf("node inserted\n");

// Function to delete a node from the beginning

void deleteFromBeginning(struct Node** head) {

if (*head == NULL) {

printf("List is empty. No nodes to delete.\n");

return;
}

struct Node* temp = *head;

*head = (*head)->next;

if (*head != NULL) {

(*head)->prev = NULL;

free(temp);

printf("node deleted\n");

// Function to delete a node from the end

void deleteFromEnd(struct Node** head) {

if (*head == NULL) {

printf("List is empty. No nodes to delete.\n");

return;

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

if (temp->prev != NULL) {

temp->prev->next = NULL;

} else {

*head = NULL; // If there was only one node

free(temp);

printf("node deleted\n");

}
// Function to delete a node after a specified node

void deleteAfterNode(struct Node** head, int key) {

struct Node* temp = *head;

while (temp != NULL && temp->data != key) {

temp = temp->next;

if (temp == NULL || temp->next == NULL) {

printf("Node not found or no node to delete after it.\n");

return;

struct Node* nodeToDelete = temp->next;

temp->next = nodeToDelete->next;

if (nodeToDelete->next != NULL) {

nodeToDelete->next->prev = temp;

free(nodeToDelete);

printf("node deleted\n");

// Function to search for an element

void searchElement(struct Node* head, int key) {

struct Node* temp = head;

while (temp != NULL) {

if (temp->data == key) {

printf("Item found\n");

return;
}

temp = temp->next;

printf("Item not found\n");

// Function to display the list

void showList(struct Node* head) {

struct Node* temp = head;

if (temp == NULL) {

printf("List is empty.\n");

return;

printf("printing values...\n");

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

// Function to display the menu

void displayMenu() {

printf("\n*********Main Menu*********\n\n");

printf("Choose one option from the following list ...\n\n");

printf("===============================================\n\n");

printf("1.Insert in begining\n");

printf("2.Insert at last\n");

printf("3.Insert at any random location\n");


printf("4.Delete from Beginning\n");

printf("5.Delete from last\n");

printf("6.Delete the node after the given data\n");

printf("7.Search\n");

printf("8.Show\n");

printf("9.Exit\n\n");

printf("Enter your choice?\n\n");

int main() {

struct Node* head = NULL;

int choice, data, position;

while (1) {

displayMenu();

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter Item value\n\n");

scanf("%d", &data);

insertAtBeginning(&head, data);

break;

case 2:

printf("Enter value\n\n");

scanf("%d", &data);

insertAtEnd(&head, data);

break;

case 3:
printf("Enter the location\n\n");

scanf("%d", &position);

printf("Enter value\n\n");

scanf("%d", &data);

insertAtPosition(&head, data, position);

break;

case 4:

deleteFromBeginning(&head);

break;

case 5:

deleteFromEnd(&head);

break;

case 6:

printf("Enter the node value after which to delete\n\n");

scanf("%d", &data);

deleteAfterNode(&head, data);

break;

case 7:

printf("Enter item which you want to search?\n");

scanf("%d", &data);

searchElement(head, data);

break;

case 8:

showList(head);

break;

case 9:

exit(0);

default:

printf("Invalid choice! Please try again.\n");


}

return 0;

CIRCULAR LINKED LIST


#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL;

void insertAtBeginning(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

if (head == NULL) {

newNode->next = newNode; // Circular reference

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != head) {

temp = temp->next;
}

temp->next = newNode;

newNode->next = head;

head = newNode;

printf("node inserted\n");

void insertAtEnd(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

if (head == NULL) {

newNode->next = newNode; // Circular reference

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

newNode->next = head; // Last node points to head

printf("node inserted\n");

void deleteFromBeginning() {

if (head == NULL) {

printf("List is empty, nothing to delete.\n");


return;

if (head->next == head) {

free(head);

head = NULL;

} else {

struct Node* temp = head;

struct Node* last = head;

while (last->next != head) {

last = last->next;

last->next = head->next;

head = head->next;

free(temp);

printf("node deleted\n");

void deleteFromEnd() {

if (head == NULL) {

printf("List is empty, nothing to delete.\n");

return;

if (head->next == head) {

free(head);
head = NULL;

} else {

struct Node* temp = head;

struct Node* prev = NULL;

while (temp->next != head) {

prev = temp;

temp = temp->next;

prev->next = head;

free(temp);

printf("node deleted\n");

void search(int value) {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

do {

if (temp->data == value) {

printf("Item found\n");

return;

temp = temp->next;

} while (temp != head);


printf("Item not found\n");

void show() {

if (head == NULL) {

printf("The list is empty.\n");

return;

struct Node* temp = head;

printf(" printing values ... \n");

do {

printf("%d\n", temp->data);

temp = temp->next;

} while (temp != head);

int main() {

int choice, value;

do {

printf("\n*********Main Menu*********\n\n");

printf("Choose one option from the following list ...\n\n");

printf("===============================================\n\n");

printf("1.Insert in begining\n");

printf("2.Insert at last\n");

printf("3.Delete from Beginning\n");

printf("4.Delete from last\n");

printf("5.Search for an element\n");


printf("6.Show\n");

printf("7.Exit\n\n");

printf("Enter your choice?\n\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the node data?\n\n");

scanf("%d", &value);

insertAtBeginning(value);

break;

case 2:

printf("Enter Data?\n\n");

scanf("%d", &value);

insertAtEnd(value);

break;

case 3:

deleteFromBeginning();

break;

case 4:

deleteFromEnd();

break;

case 5:

printf("\nEnter item which you want to search?\n\n");

scanf("%d", &value);

search(value);

break;

case 6:

show();
break;

case 7:

exit(0);

break;

default:

printf("Invalid choice.\n");

} while (choice != 7);

return 0;

BINARY SEARCH TREE(BST)


#include <stdio.h>

#include <stdlib.h>

// Structure for a tree node

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;
newNode->left = NULL;

newNode->right = NULL;

return newNode;

// Function to insert a node into the BST

struct Node* insertNode(struct Node* root, int data) {

if (root == NULL) {

return createNode(data); // Create new node if tree is empty

if (data < root->data) {

root->left = insertNode(root->left, data); // Insert in the left subtree

} else if (data > root->data) {

root->right = insertNode(root->right, data); // Insert in the right subtree

return root;

// Function to find the minimum value node in the BST

struct Node* findMin(struct Node* node) {

struct Node* current = node;

while (current && current->left != NULL)

current = current->left; // Traverse to the leftmost leaf

return current;

// Function to delete a node from the BST

struct Node* deleteNode(struct Node* root, int data) {

if (root == NULL)
return root;

if (data < root->data) {

root->left = deleteNode(root->left, data); // Traverse to the left subtree

} else if (data > root->data) {

root->right = deleteNode(root->right, data); // Traverse to the right subtree

} else {

// Node to be deleted found

// Case 1: Node with only one child or no child

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;

// Case 2: Node with two children

// Find the inorder successor (smallest in the right subtree)

struct Node* temp = findMin(root->right);

// Copy the inorder successor's content to this node

root->data = temp->data;

// Delete the inorder successor

root->right = deleteNode(root->right, temp->data);

}
return root;

// Helper function to print the "->" format for Inorder Traversal

void inorderTraversal(struct Node* node, int* count) {

if (node == NULL)

return;

inorderTraversal(node->left, count);

(*count)++;

printf("%d", node->data);

// Only print "->" if this is not the last node

if (*count != 8) {

printf(" -> ");

inorderTraversal(node->right, count);

int main() {

struct Node* root = NULL;

// Manually constructing the tree (you can replace this with user input)

root = insertNode(root, 8);

root = insertNode(root, 3);

root = insertNode(root, 10);

root = insertNode(root, 1);


root = insertNode(root, 6);

root = insertNode(root, 4);

root = insertNode(root, 7);

root = insertNode(root, 14);

// Initial Inorder Traversal

printf("Inorder traversal: ");

int count = 0;

inorderTraversal(root, &count);

printf(" -> \n");

// Delete node 10

root = deleteNode(root, 10);

printf("After deleting 10\n");

// Reset count for correct traversal

count = 0;

// Inorder Traversal after deletion

printf("Inorder traversal: ");

inorderTraversal(root, &count);

printf(" -> \n");

return 0;

}
BINARY TREE TRAVERSAL
#include <stdio.h>

#include <stdlib.h>

// Structure for a tree node

struct Node {

int data;

struct Node* left;

struct Node* right;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

// Function to insert node in the binary tree in a basic level-order manner (like a binary search tree)

struct Node* insertNode(struct Node* root, int data) {

if (root == NULL) {

return createNode(data); // Create the root node

if (data < root->data) {


root->left = insertNode(root->left, data);

} else {

root->right = insertNode(root->right, data);

return root;

// Preorder traversal (Root, Left, Right)

void preorderTraversal(struct Node* node) {

if (node == NULL)

return;

printf("%d ", node->data);

preorderTraversal(node->left);

preorderTraversal(node->right);

// Inorder traversal (Left, Root, Right)

void inorderTraversal(struct Node* node) {

if (node == NULL)

return;

inorderTraversal(node->left);

printf("%d ", node->data);

inorderTraversal(node->right);

// Postorder traversal (Left, Right, Root)

void postorderTraversal(struct Node* node) {

if (node == NULL)
return;

postorderTraversal(node->left);

postorderTraversal(node->right);

printf("%d ", node->data);

int main() {

struct Node* root = NULL;

char choice;

// Loop to allow user to input data and construct the tree

do {

int data;

printf("Enter data: ");

scanf("%d", &data);

// Insert node into the binary tree

root = insertNode(root, data);

printf("Do you want to enter more (y/n)? ");

scanf(" %c", &choice);

} while (choice == 'y' || choice == 'Y');

// Display traversals in the expected format

printf("\nPreorder Traversal: ");

preorderTraversal(root);

printf("\n");

printf("Inorder Traversal: ");


inorderTraversal(root);

printf("\n");

printf("Postorder Traversal: ");

postorderTraversal(root);

printf("\n");

return 0;

You might also like