C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 15th, 2008, 06:35 AM
lindaonline15 lindaonline15 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 1 lindaonline15 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 24 sec
Reputation Power: 0
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;
}

Reply With Quote
  #2  
Old October 15th, 2008, 01:24 PM
C-a2z C-a2z is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 6 C-a2z New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 2 h 51 m 41 sec
Reputation Power: 0
dude just change the last line........
i.e
printf("The number in your mind is %d \n", &answer);

to this one....

printf("The number in your mind is %d \n", *answer);

the problem here is &--gives the address of the variable.(address of variable 'answer') and not the value pointed by it...to get the value pointed by it we use
'*' operator which is called dereferencing operator...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > A searching tree


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT