Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int item;
struct node *left;
struct node * right;
}Node;
Node *Tree=NULL;
Node *Insert(Node *temp, Node *root){
if(root==NULL){
root=temp;
root->left=NULL;
root->right=NULL;
}
else if (temp->item < root->item)
root->left=Insert(temp,root->left);
else
root->right=Insert(temp,root->right);
return root;
}
Node *Create(int a)
{
Node *temp = (Node*)malloc(sizeof(Node));
temp->left=NULL;
temp->right=NULL;
temp->item=a;
return temp;
}
int main()
{
Node *newNode=NULL;
char choice1, choice2, choice3, choice4;
Node *answer=Tree;
Node *num=NULL;
//build up the tree
newNode=Create(15);
Tree=Insert(newNode,Tree);
newNode=Create(14);
Tree=Insert(newNode,Tree);
newNode=Create(16);
Tree=Insert(newNode,Tree);
newNode=Create(13);
Tree=Insert(newNode,Tree);
newNode=Create(17);
Tree=Insert(newNode,Tree);
newNode=Create(12);
Tree=Insert(newNode,Tree);
newNode=Create(18);
Tree=Insert(newNode,Tree);
newNode=Create(11);
Tree=Insert(newNode,Tree);
newNode=Create(19);
Tree=Insert(newNode,Tree);
newNode=Create(10);
Tree=Insert(newNode,Tree);
newNode=Create(20);
Tree=Insert(newNode,Tree);
newNode=Create(9);
Tree=Insert(newNode,Tree);
newNode=Create(21);
Tree=Insert(newNode,Tree);
newNode=Create(8);
Tree=Insert(newNode,Tree);
newNode=Create(22);
Tree=Insert(newNode,Tree);
//lay out answers in the leaves
num = Create(15);
Tree = num;
num = Create(14);
Tree->left = num;
num = Create(16);
Tree->right= num;
num = Create(13);
Tree->left->left = num;
num = Create(17);
Tree->left->right = num;
num = Create(12);
Tree->left->left->left = num;
num = Create(18);
Tree->left->left->right = num;
num = Create(11);
Tree->left->right->left = num;
num = Create(19);
Tree->left->right->right = num;
num = Create(10);
Tree->right->left = num;
num = Create(20);
Tree->right->right= num;
num = Create(9);
Tree->right->left->left = num;
num = Create(21);
Tree->right->left->right = num;
num = Create(8);
Tree->right->right->left = num;
num = Create(22);
Tree->right->right->right = num;
answer=Tree;
printf("pick a number between 8, 9, 10, 11, 12, 13, 15, 17, 18, 19, 20, 21, 22\n\n\n");
printf("is the number you have chosen between 11, 12, 13, 14, 17, 18,19? (y/n)\n");
scanf(" %c", &choice1);
if ( choice1 == 'y')//left
{
printf("is the number you have chosen between 12, 13, 18? (y/n)\n");
scanf(" %c", &choice2);
if (choice2 == 'y')//left->left
{
printf("is the number you have chosen 12? (y/n)\n");
scanf(" %c", &choice3);
if (choice3 == 'y')//left->left->left
answer= answer->left->left->left;
else if (choice3 == 'n')//left->left->right
{
printf("is the number you have chosen 18? (y/n)\n");
scanf(" %c", &choice4);
if(choice4 == 'y')
answer = answer->left->left->right;
else if(choice4 == 'n')
answer = answer->left->left;
}
}
else if (choice2 == 'n')
{
printf("is the number you have chosen 11? (y/n)\n");
scanf(" %c", &choice3);
if (choice3 == 'y')//left->right->left
answer= answer->left->right->left;
else if (choice3 == 'n')//left->right->right
{
printf("is the number you have chosen 19? (y/n)\n");
scanf(" %c", &choice4);
if(choice4 == 'y')
answer = answer->left->right->right;
else if(choice4 == 'n')
answer = answer->left->right;
}
}
}
else if (choice1 == 'n') //right
{
printf("is the number you have chosen between 9, 10, 21? (y/n)\n");
scanf(" %c", &choice2);
if (choice2 == 'y')
{
printf("is the number you have chosen 9? (y/n)\n");
scanf(" %c", &choice3);
if (choice3 == 'y')//right->left->left
answer= answer->right->left->left;
else if (choice3 == 'n')//left->left->right
{
printf("is the number you have chosen 21? (y/n)\n");
scanf(" %c", &choice4);
if(choice4 == 'y')
answer = answer->right->left->right;
else if(choice4 == 'n')
answer = answer->right->left;
}
}
else if (choice2 == 'n')
{
printf("is the number you have chosen 8? (y/n)\n");
scanf(" %c", &choice3);
if (choice3 == 'y')//left->right->left
answer= answer->right->right->left;
else if (choice3 == 'n')//left->right->right
{
printf("is the number you have chosen 22? (y/n)\n");
scanf(" %c", &choice4);
if(choice4 == 'y')
answer = answer->right->right->right;
else if(choice4 == 'n')
answer = answer->right->right;
}
}
}
printf("The number in your mind is %d \n", &answer);
return 0;
}