typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* search(Node* root, int key) {
if (root == NULL || root->data == key) {
return root;
}
if (key < root->data) {
return search(root->left, key);
} else {
return search(root->right, key);
}
}
#include <stdio.h>
#include <stdlib.h>
Node* newNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
Node* root = newNode(10);
root->left = newNode(5);
root->right = newNode(15);
root->left->left = newNode(3);
root->left->right = newNode(7);
Node* result = search(root, 7);
if (result != NULL) {
printf("Found: %d\n", result->data);
} else {
printf("Not Found\n");
}
return 0;
}The search starts at root (10). Since 7 < 10, it goes left to 5. Then 7 > 5, it goes right to 7. It finds the node with data 7 and returns it.
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* search(Node* root, int key) {
if (root == NULL || root->data == key) {
return root;
}
if (key < root->data) {
return search(root->left, key);
} else {
return search(root->right, key);
}
}
#include <stdio.h>
#include <stdlib.h>
Node* newNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
Node* root = newNode(20);
root->left = newNode(10);
root->right = newNode(30);
root->left->left = newNode(5);
root->left->right = newNode(15);
Node* result = search(root, 25);
if (result != NULL) {
printf("Found: %d\n", result->data);
} else {
printf("Not Found\n");
}
return 0;
}The search goes right from 20 to 30, then left (NULL) because 25 < 30. It returns NULL, so 'Not Found' is printed.
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* search(Node* root, int key) {
if (root == NULL) {
return NULL;
}
if (root->data == key) {
return root;
}
if (key > root->data) {
return search(root->right, key);
} else {
return search(root->left, key);
}
}
#include <stdio.h>
#include <stdlib.h>
Node* newNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
Node* root = newNode(8);
root->left = newNode(3);
root->right = newNode(10);
Node* result = search(root, 10);
if (result != NULL) {
printf("Found: %d\n", result->data);
} else {
printf("Not Found\n");
}
return 0;
}The code incorrectly calls search on root->left when key > root->data, but it should go right. This causes wrong traversal leading to incorrect output ("Not Found" instead of "Found: 10").
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* searchIterative(Node* root, int key) {
while (root != NULL) {
if (root->data == key) {
return root;
} else if (key < root->data) {
root = root->left;
} else {
root = root->right;
}
}
return NULL;
}
#include <stdio.h>
#include <stdlib.h>
Node* newNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int main() {
Node* root = newNode(15);
root->left = newNode(10);
root->right = newNode(20);
root->left->left = newNode(8);
root->left->right = newNode(12);
Node* result = searchIterative(root, 12);
if (result != NULL) {
printf("Found: %d\n", result->data);
} else {
printf("Not Found\n");
}
return 0;
}The search starts at 15, goes left to 10 (since 12 < 15), then right to 12 (since 12 > 10), finds the node and returns it.
When the BST is skewed like a linked list, searching for a missing value requires visiting all nodes from root to the last node, so n nodes are visited.
