Dsu Pr26 Main
Dsu Pr26 Main
/*_________________Roll no:960______________________*/
/*_________________Branch: SYCO____________________*/
/*_________________Subject: DSU__________________*/
/*_________________Practical no:26__________________*/
/*_________________Date:15/10/24____________________*/
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
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;
}
struct Node* insert(struct Node* node, int data)
{
if (node == NULL) return createNode(data);
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
}
void inOrder(struct Node* root)
{
if (root != NULL)
{
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
}
int main()
{
struct Node* root = NULL;
int choice, value;
while (1) {
printf("\n--- Binary Search Tree Operations ---\n");
printf("1. Insert\n");
printf("2. In-Order Traversal\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the value to insert: ");
scanf("%d", &value);
root = insert(root, value);
printf("Inserted %d\n", value);
break;
case 2:
printf("In-Order Traversal: ");
inOrder(root);
printf("\n");
break;
case 3:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
/*______________End of theProgram___________________________*/
/*____________Output of the Program_________________________*/