Practical No.
:26
Name :- GANESH BALU PATANGE
Roll No :- 69 Branch :- CO3K
#include <stdio.h> if (value < root->data)
#include <stdlib.h> root->left = insert(root->left,
value);
// Structure for a node
else if (value > root->data)
struct Node {
root->right = insert(root->right,
int data;
value);
struct Node* left;
return root;
struct Node* right;
}
};
// Function for In-order traversal
// Function to create a new node
void inorder(struct Node* root) {
struct Node* newNode(int value) {
if (root != NULL) {
struct Node* temp = (struct
inorder(root->left);
Node*)malloc(sizeof(struct Node));
printf("%d ", root->data);
temp->data = value;
inorder(root->right);
temp->left = temp->right = NULL;
}
return temp;
}
}
int main() {
// Function to insert a node in BST
struct Node* root = NULL;
struct Node* insert(struct Node*
root, int value) { int choice, value;
if (root == NULL) // If tree is empty while (1) {
return newNode(value); printf("\n--- Binary Search Tree
Menu ---\n");
printf("1. Insert\n2. In-order
Traversal\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert:
");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("In-order Traversal:
");
inorder(root);
printf("\n");
break;
case 3:
exit(0);
default:
printf("Invalid choice!\n");
return 0;
Output:-