[go: up one dir, main page]

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

Struct Int Struct Struct Typedef Struct: "/nenter The Value: " "%D"

The document contains a C program that implements a Binary Search Tree (BST) with functionalities to create, insert, search, and traverse the tree in different orders (inorder, preorder, postorder). It provides a menu-driven interface for users to interact with the BST, allowing them to input values and perform various operations. The program uses standard input/output for user interaction and dynamically allocates memory for new nodes.

Uploaded by

Nagaraj Naik
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)
4 views3 pages

Struct Int Struct Struct Typedef Struct: "/nenter The Value: " "%D"

The document contains a C program that implements a Binary Search Tree (BST) with functionalities to create, insert, search, and traverse the tree in different orders (inorder, preorder, postorder). It provides a menu-driven interface for users to interact with the BST, allowing them to input values and perform various operations. The program uses standard input/output for user interaction and dynamically allocates memory for new nodes.

Uploaded by

Nagaraj Naik
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/ 3

#include<stdio.

h>
#include<stdlib.h>

struct BST
{
int data;
struct BST *llink;
struct BST *rlink;
};
typedef struct BST * NODE;

NODE create()
{
NODE temp;
temp = (NODE) malloc(sizeof(struct BST));
printf("\nEnter The value: ");
scanf("%d", &temp->data);
temp->llink = NULL;
temp->rlink = NULL;
return temp;
}

void insert(NODE root, NODE newnode);


void inorder(NODE root);
void preorder(NODE root);
void postorder(NODE root);
void search(NODE root);

void insert(NODE root, NODE newnode)


{
if (newnode->data < root->data)
{
if (root->llink == NULL)
root->llink = newnode;
else
insert(root->llink, newnode);
}
if (newnode->data > root->data)
{
if (root->rlink == NULL)
root->rlink = newnode;
else
insert(root->rlink, newnode);
}
}

void search(NODE root)


{
int key;
NODE cur;
if (root == NULL)
{
printf("\nBST is empty.");
return;
}
printf("\nEnter Element to be searched: ");
scanf("%d", &key);
cur = root;
while (cur != NULL)
{
if (cur->data == key)
{
printf("\nKey element is present in BST ");
return;
}
if (key < cur->data)
cur = cur->llink;
else
cur = cur->rlink;
}
printf("\nKey element is not found in the BST ");
}

void inorder(NODE root)


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

void preorder(NODE root)


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

void postorder(NODE root)


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

void main()
{
int ch, key, val, i, n;
NODE root = NULL, newnode;
while (1)
{
printf("\n-------BST MENU-------");
printf("\n1. Create a BST ");
printf("\n2. Search ");
printf("\n3. BST Traversals: ");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter the number of elements: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
}
break;
case 2:
if (root == NULL)
printf("\nTree Is Not Created ");
else
{
printf("\nThe Preorder display: ");
preorder(root);
printf("\nThe Inorder display: ");
inorder(root);
printf("\nThe Postorder display: ");
postorder(root);
}
break;
case 3:
search(root);
break;
case 4:
exit(0);
default:
printf("\nInvalid choice, please try again.");
}
}
}

You might also like