#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char data[50];
struct Node left, right;
};
struct Node* newNode(char key[]) {
struct Node node = (struct Node)malloc(sizeof(struct Node));
strcpy(node->data, key);
node->left = node->right = NULL;
return node;
}
struct Node insert(struct Node root, char key[]) {
if (root == NULL)
return newNode(key);
if (strcmp(key, root->data) < 0)
root->left = insert(root->left, key);
else if (strcmp(key, root->data) > 0)
root->right = insert(root->right, key);
return root;
}
struct Node search(struct Node root, char key[]) {
if (root == NULL || strcmp(root->data, key) == 0)
return root;
if (strcmp(key, root->data) < 0)
return search(root->left, key);
return search(root->right, key);
}
void inOrderTraversal(struct Node* root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%s ", root->data);
inOrderTraversal(root->right);
}
}
int main() {
struct Node* root = NULL;
char keys[][50] = {"apple", "orange", "banana", "grape", "kiwi"};
for (int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++)
root = insert(root, keys[i]);
printf("In-order traversal of the BST:\n");
inOrderTraversal(root);
char searchKey[50];
printf("\nEnter a string to search: ");
scanf("%s", searchKey);
struct Node* result = search(root, searchKey);
if (result != NULL)
printf("%s found in the BST.\n", searchKey);
else
printf("%s not found in the BST.\n", searchKey);
return 0;
}