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

Binary Search Tree Operations

Uploaded by

reemadnayak06
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)
7 views7 pages

Binary Search Tree Operations

Uploaded by

reemadnayak06
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

PUNE INSTITUTE OF COMPUTER TECHNOLOGY

PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2023-2024 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2023-24/ Starting date:
Roll No: 22351 Submission date:

Title: Binary Search Tree Operations.

Problem Write a program in C to implement binary search tree with operations create ,search and
Statement recursive traversal.

Programmer Name: Reema Nayak


Batch: G7

Code implementation:

#include <stdio.h>
#include <stdlib.h>

// Define a structure for a binary 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 = newNode->right = NULL;
return newNode;
}

// Function to insert a new node into the BST


struct Node *insert(struct Node *root, int data) {
if (root == NULL) {
return createNode(data);

DS_LAB_2023-24: Program input output 1


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2023-2024 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2023-24/ Starting date:
Roll No: 22351 Submission date:

if (data < root->data) {


root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}

return root;
}

// Function to search for a key in the BST


struct Node *search(struct Node *root, int key) {
if (root == NULL || root->data == key) {
return root;
}

if (key < root->data) {


return search(root->left, key);
}

return search(root->right, key);


}

// Function for in-order traversal of the BST


void inOrderTraversal(struct Node *root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}

DS_LAB_2023-24: Program input output 2


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2023-2024 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2023-24/ Starting date:
Roll No: 22351 Submission date:

// Function for pre-order traversal of the BST


void preOrderTraversal(struct Node *root) {
if (root != NULL) {
printf("%d ", root->data);
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
}

// Function for post-order traversal of the BST


void postOrderTraversal(struct Node *root) {
if (root != NULL) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
printf("%d ", root->data);
}
}

int main() {
struct Node *root = NULL;
int choice, data;

while (1) {
printf("Binary Search Tree Operations:\n");
printf("1. Insert\n");
printf("2. Search\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);

DS_LAB_2023-24: Program input output 3


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2023-2024 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2023-24/ Starting date:
Roll No: 22351 Submission date:

switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;

case 2:
printf("Enter data to search: ");
scanf("%d", &data);
if (search(root, data) != NULL) {
printf("%d found in the tree.\n");
} else {
printf("%d not found in the tree.\n");
}
break;

case 3:
printf("In-order Traversal: ");
inOrderTraversal(root);
printf("\n");
break;

case 4:
printf("Pre-order Traversal: ");
preOrderTraversal(root);
printf("\n");
break;

case 5:
printf("Post-order Traversal: ");
postOrderTraversal(root);
printf("\n");

DS_LAB_2023-24: Program input output 4


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2023-2024 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2023-24/ Starting date:
Roll No: 22351 Submission date:

break;

case 6:
exit(0);

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

return 0;
}

OUTPUT:

Binary Search Tree Operations:


1. Insert
2. Search
3. In-order Traversal
4. Pre-order Traversal
5. Post-order Traversal
6. Exit
Enter your choice: 1
Enter data to insert: 50
Binary Search Tree Operations:
1. Insert
2. Search
3. In-order Traversal
4. Pre-order Traversal
5. Post-order Traversal
6. Exit

DS_LAB_2023-24: Program input output 5


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2023-2024 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2023-24/ Starting date:
Roll No: 22351 Submission date:

Enter your choice: 1


Enter data to insert: 30
Binary Search Tree Operations:
1. Insert
2. Search
3. In-order Traversal
4. Pre-order Traversal
5. Post-order Traversal
6. Exit
Enter your choice: 1
Enter data to insert: 70
Binary Search Tree Operations:
1. Insert
2. Search
3. In-order Traversal
4. Pre-order Traversal
5. Post-order Traversal
6. Exit
Enter your choice: 1
Enter data to insert: 40
Binary Search Tree Operations:
1. Insert
2. Search
3. In-order Traversal
4. Pre-order Traversal
5. Post-order Traversal
6. Exit
Enter your choice: 1
Enter data to insert: 60
Binary Search Tree Operations:
1. Insert
2. Search
3. In-order Traversal

DS_LAB_2023-24: Program input output 6


PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2023-2024 CLASS: SE
SUBJECT: DATA STRUCTURES
EXPT No: LAB Ref: SE/2023-24/ Starting date:
Roll No: 22351 Submission date:

4. Pre-order Traversal
5. Post-order Traversal
6. Exit
Enter your choice: 3
In-order Traversal: 30 40 50 60 70

DS_LAB_2023-24: Program input output 7

You might also like