#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    Are binary trees mainly used with numbers?


    Well, are they? When I write a binary trees with numbers as the nodes I'm able to make them function correctly (with the fact I'm actually able to insert numbers now instead of going pointer after pointer).

    But when I try to put it in another use and use it for texts I'm struggling.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    typedef struct stranger {
    	char *question;
    	struct stranger *yes;
    	struct stranger *no;
    } stranger;
    
    
    void Ask(stranger **man)
    {
    	char answer;
    	if(*man == NULL)
    	{
    		return;
    	}
    	printf("%s\n",(*man)->question);
    	scanf("%c",&answer);
    	if(answer == 'y')
    	{
    		Ask(&(*man)->yes);
    	}
    	else if(answer == 'n')
    	{
    		Ask(&(*man)->no);
    	}
    }
    
    void search(stranger **man,char *question)
    {
    	if(*man == NULL)
    	{
    
    		stranger *Q = (stranger *)malloc(sizeof(stranger));
    		Q->question = strdup(question);
    		Q->yes = Q->no = NULL;
    		*man = Q;
    	}
    	Ask(&(*man));
    }
    
    void Done(stranger *over)
    {
    	if(over == NULL)
    	{
    		return;
    	}
    	Done(over->yes);
    	free(over);
    	Done(over->no);
    }
    I'm having struggle making a binary tree with text. This is what I want it to look like:

    Is he human?
    Boy | Girl
    Bald | Hairy | Blonde | Not Blonde

    But when I look at my code and actually try to put it in use I'm not able to. Mainly because I'm struggling on how to get the two pointers, yes and no, not to be null. But the thing is I'm not sure how to set an address for them.

    So may I ask. How am I able to do this? Witht he code I have, I want to insert the following text above. And may I ask, is a binary tree only necessary for numbers? From reading 4-7 sites, all examples used numbers.

    Thanks again
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    There is nothing about the data field(s) of a binary tree node that makes it easier or harder to work with. The node can contain any kind of data at all; all that really matters is having the two child pointers. For that matter, I think that all of my use of binary trees in the past had been to sort text.

    I noticed a potential problem in your Ask function. You wrote:
    scanf("%c",&answer);
    That tells scanf to read the next character, even white space (ie, the newline left in the input buffer by the previous scanf call). Does it appear that the program is ignoring your answers? Like it just blows past that point? That is why.

    Instead, you need to tell scanf to ignore all white space that might precede the printable character you want it to read in; namely
    scanf(" %c",&answer);
    Note the space before the percent sign. That tells scanf to expect whitespace and that it needs to skip that white space.

    Also, what happens if the user does not enter 'y' or 'n'? Like if he enters 'Y' or 'N' -- remember, 'Y' (ASCII code of 89) is entirely different from 'y' (ASCII code of 121). There's no else to trap for invalid input. Rather, shouldn't you instead place the scanf inside a do-while loop that will only exit if the user enters a valid response (ie, 'y' or 'n')? Just a thought.

IMN logo majestic logo threadwatch logo seochat tools logo