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

Exp 10 DS

Uploaded by

robincyril712
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)
13 views2 pages

Exp 10 DS

Uploaded by

robincyril712
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/ 2

#include <stdio.

h>
#include <stdlib.h>

struct Node {
int data;
struct Node* left;
struct Node* right;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node* insertNode(struct Node* root, int data) {


if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insertNode(root->left, data);
} else {
root->right = insertNode(root->right, data);
}
return root;
}

int searchNode(struct Node* root, int key) {


if (root == NULL) {
return 0;
}
if (root->data == key) {
return 1;
}
if (key < root->data) {
return searchNode(root->left, key);
} else {
return searchNode(root->right, key);
}
}

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

printf("Enter 6 nodes for the BST:\n");


for (i = 0; i < 6; i++) {
scanf("%d", &data);
root = insertNode(root, data);
}

printf("Enter two elements to search in the BST:\n");


for (i = 0; i < 2; i++) {
scanf("%d", &key);
if (searchNode(root, key)) {
printf("%d is found in the BST.\n", key);
} else {
printf("%d is not found in the BST.\n", key);
}
}

return 0;
}

You might also like