typedef struct BinaryTreeNode {
int data;
BinaryTreeNode * left;
BinaryTreeNode * right;
} BinaryTreeNode;
int isElementInBinaryTree(BinaryTreeNode *root, int search_item) {
if(root) {
if(search_item == root -> data) return 1;
isElementInBinaryTree(root -> left, search_item);
isElementInBinaryTree(root -> right, search_item);
}
}
int main() {
BinaryTreeNode one = {1, NULL, NULL}; // root of the binary tree
BinaryTreeNode two = {2, NULL, NULL};
BinaryTreeNode three = {3, NULL, NULL};
BinaryTreeNode four = {4, NULL, NULL};
BinaryTreeNode five = {5, NULL, NULL};
BinaryTreeNode six = {6, NULL, NULL};
BinaryTreeNode seven = {7, NULL, NULL};
one.left = &two;
one.right = &three;
two.left = &four;
two.right = &five;
three.left = &six;
three.right = &seven;
printf("%d ", isElementInBinaryTree(&one, 4));
printf("\n");
return 0;
}
I am writing a function called isElementInBinaryTree that returns 1 (true) is the element exists and 0 otherwise. I don't understand why the function is always returning 0 despite the fact that the number 4 exists in the binary tree ?