0% found this document useful (0 votes)
8 views7 pages

DS PRAC 09

ds pr 8

Uploaded by

Ritesh Doibale
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)
8 views7 pages

DS PRAC 09

ds pr 8

Uploaded by

Ritesh Doibale
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/ 7

Name :Mohammed Noman Roll no : 270

Experiment no 9: Write a C program to Program for insert, delete and tree traversals like In-
order, Preorder and Post-order of binary search tree.
Code:
#include <stdio.h>
#include <stdlib.h>

struct Node//node structure


{
int data;
struct Node *left;
struct Node *right;
};

struct Node *createNode(int data)//create node


{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));//alloacate the memory
to new node
newNode->data = data;//setValue
newNode->left = NULL;//set left pointer null
newNode->right = NULL;//set right pointer null
return newNode;//return node
}

struct Node *insert(struct Node *root, int data)//to insert node


{
if (root == NULL)
{ //root is null then tree is empty
return createNode(data);//insert and return node
}
if (data < root->data)
{ //when data is smaller then traverse the left node
root->left = insert(root->left, data);
}
else if (data > root->data)
{ // when data is greater then traverse the right node
root->right = insert(root->right, data);
}
return root;
}
struct Node *findMin(struct Node *root)//travers to smallest element
{
while (root->left != NULL)
{
root = root->left;//shift to left at every level
}
return root;
}

struct Node *deleteNode(struct Node *root, int data)//to delete the specific node
{
if (root == NULL)//when root is empty return root
{
return root;
}
if (data < root->data)
{ // when data is smaller then call detelenode with left of that element in recursion
root->left = deleteNode(root->left, data);
}
else if (data > root->data)
{ // when data is greater then call detelenode with right of that element in recursion
root->right = deleteNode(root->right, data);
}
else
{
if (root->left == NULL)
{//when left part of node is empty
struct Node *temp = root->right;
free(root);//free the root
return temp;//return temp node to previous calling function
}
else if (root->right == NULL)
{//when left of the passed root is null
struct Node *temp = root->left;//set temp node =left of root
free(root);//free the root node
return temp;//return the temp node to calling function
}

struct Node *temp = findMin(root->right);//find minimun from right of root


root->data = temp->data; //set data of root = data of temp
root->right = deleteNode(root->right, temp->data); //then delete
}
return root;//return the root to calling function
}
void inOrder(struct Node *root)
{
if (root != NULL)
{//LVR is Inorder //L=visit left in inorder //V= visit root and print //R=visit Right of root in
inorder
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
}

void preOrder(struct Node *root)


{
if (root != NULL)
{ // VLR is preorder //V= visit root and print //L=visit left in inorder //R=visit Right of root
in inorder
printf("%d ", root->data);
preOrder(root->left);
preOrder(root->right);
}
}

void postOrder(struct Node *root)


{ // LRV is postorder //L=visit left in inorder //R=visit Right of root in inorder //V= visit
root and print
if (root != NULL)
{
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
}

int main()//main function execution starts


{ struct Node *root = NULL;
int choice, data;
do{
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. In-order Traversal\n");
printf("4. Pre-order Traversal\n");
printf("5. Post-order Traversal\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1://calls insert function
printf("Enter value to insert: ");
scanf("%d", &data);
root = insert(root, data);
printf("%d inserted successfully.\n", data);
break;
case 2://calls delete function
printf("Enter value to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
printf("%d deleted successfully.\n", data);
break;
case 3://prints tree elements in Inorder
printf("In-order traversal: ");
inOrder(root);
printf("\n");
break;
case 4://Prints the element in PreOrder
printf("Pre-order traversal: ");
preOrder(root);
printf("\n");
break;
case 5://Prints tree elements in Postored
printf("Post-order traversal: ");
postOrder(root);
printf("\n");
break;
case 6://exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 6);
return 0;
}
Output:
PS C:\Users\adi\OneDrive\Desktop\CSE-3rdSem\DS\Practicals\pract9>gccBinarySearchTree.c
PS C:\Users\adi\OneDrive\Desktop\CSE-3rdSem\DS\Practicals\pract9> ./a.exe

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 1
Enter value to insert: 20
20 inserted successfully.

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 1
Enter value to insert: 40
40 inserted successfully.

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 1
Enter value to insert: 60
60 inserted successfully.

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 1
Enter value to insert: 50
50 inserted successfully.

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 4
In-order traversal: 20 40 50 60

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 5
Pre-order traversal: 20 40 60 50

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 6
Post-order traversal: 50 60 40 20

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 3
Enter value to search: 60
60 found in the tree.

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 3
Enter value to search: 55
55 not found in the tree.

Menu:
1. Insert
2. Delete
3. Search
4. In-order Traversal
5. Pre-order Traversal
6. Post-order Traversal
7. Exit
Enter your choice: 7
Exiting...
PS C:\Users\adi\OneDrive\DSSesktop\CSE-3rdSem\DS\Practicals\pract9>

You might also like