0% found this document useful (0 votes)
1 views3 pages

BST

The document contains a C program that implements a Binary Search Tree (BST) with functionalities to construct the tree, perform preorder, inorder, and postorder traversals, and search for elements. It defines a structure for tree nodes and includes functions for inserting nodes and handling user input for various operations. The main function provides a menu-driven interface for users to interact with the BST.

Uploaded by

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

BST

The document contains a C program that implements a Binary Search Tree (BST) with functionalities to construct the tree, perform preorder, inorder, and postorder traversals, and search for elements. It defines a structure for tree nodes and includes functions for inserting nodes and handling user input for various operations. The main function provides a menu-driven interface for users to interact with the BST.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

struct node {
int info;
struct node *llink;
struct node *rlink;
};

typedef struct node NODE;

NODE *insert(int item, NODE *root) {


NODE *temp, *cur, *prev;

temp = (NODE *)malloc(sizeof(NODE));


temp->info = item;
temp->llink = NULL;
temp->rlink = NULL;

if (root == NULL)
return temp;

prev = NULL;
cur = root;
while (cur != NULL) {
prev = cur;
cur = (item <= cur->info) ? cur->llink : cur->rlink;
}

if (item < prev->info)


prev->llink = temp;
else
prev->rlink = temp;

return root;
}

NODE *construct_BST(NODE *root) {


int a, n, i;
printf("Enter the number of elements\n");
scanf("%d", &n);

printf("Enter the elements to be inserted in the tree\n");


for (i = 0; i < n; i++) {
scanf("%d", &a);
root = insert(a, root);
}

printf("Tree Constructed Successfully!!!!!!\n");


return root;
}

void preorder(NODE *root) {


if (root != NULL) {
printf("%d\t", root->info);
preorder(root->llink);
preorder(root->rlink);
}
}

void inorder(NODE *root) {


if (root != NULL) {
inorder(root->llink);
printf("%d\t", root->info);
inorder(root->rlink);
}
}

void postorder(NODE *root) {


if (root != NULL) {
postorder(root->llink);
postorder(root->rlink);
printf("%d\t", root->info);
}
}

int search_element(NODE *root, int key) {


NODE *cur;
cur = root;

while (cur != NULL) {


if (key == cur->info)
return 1; // Key found
else if (key < cur->info)
cur = cur->llink;
else
cur = cur->rlink;
}

return 0; // Key not found


}

int main() {
int item, ch, key, n;
NODE *root = NULL;

while (1) {
printf("\nEnter your choice:\n");
printf("1. Construct BST\n");
printf("2. Preorder Traversal\n");
printf("3. Inorder Traversal\n");
printf("4. Postorder Traversal\n");
printf("5. Search an Element\n");
printf("6. Exit\n");
scanf("%d", &ch);

switch (ch) {
case 1:
root = construct_BST(root); // Construct BST
break;
case 2:
printf("Preorder Traversal:\n");
preorder(root); // Preorder traversal
printf("\n");
break;
case 3:
printf("Inorder Traversal:\n");
inorder(root); // Inorder traversal
printf("\n");
break;
case 4:
printf("Postorder Traversal:\n");
postorder(root); // Postorder traversal
printf("\n");
break;
case 5:
if (root == NULL)
printf("Tree is empty.\n");
else {
printf("Enter the element to search:\n");
scanf("%d", &key);
n = search_element(root, key); // Search element
if (n)
printf("Key found.\n");
else
printf("Key not found.\n");
}
break;
case 6:
exit(0); // Exit the program
default:
printf("Invalid choice.\n");
}
}

return 0;
}

You might also like