Thread: a searching tree

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    a searching tree

    hello all, here im trying to make a tree of numbers that will prompt asking user about the number and based on yes and no the user enters, will serve the tree and should finally get to the answer in the leaves of the tree.
    it works, with no error, just it gives me some other number that is not related at all!
    anyone can help me with this please? its kind of urgent... thank you

    this is the code:

    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;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    Node* answer=Tree;
    
    ...
    
    printf("The number in your mind is %d \n", &answer);
    answer is a Node*. &answer is effectively a Node** but you're trying to print out an int. I think you probably want this instead:

    Code:
    printf("The number in your mind is %d \n", answer->item);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    yesss it worked!
    thank you very much HK...
    got headache of it already...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM