0% found this document useful (0 votes)
65 views2 pages

Practical No.26.Dsu

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

Practical No.26.Dsu

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

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:-

You might also like