Author Message

Sorry folks, the correct code is below. struct node contains 2 bits of
data: number and frequency.

Hope this all makes sense...

void insert(struct node **tree, int number)
{

struct node *new_node;

new_node = (struct node *) malloc (sizeof(struct node));

if (*tree == NULL)
*tree = new_node;

else
{
if (postcode < *tree->number)
*tree->left = insert (*tree->left, number);
else
if (postcode > tree->number)
*tree->right = insert (*tree->right, number);

else tree->frequency ++;
}

Quote:
}

Wed, 01 Mar 2000 03:00:00 GMT

Quote:

>Sorry folks, the correct code is below. struct node contains 2 bits of
>data: number and frequency.

>Hope this all makes sense...

>void insert(struct node **tree, int number)

Why are you using a pointer to a pointer ?
struct *tree should be sufficient.

Quote:
>{

>     struct node *new_node;

>     new_node = (struct node *) malloc (sizeof(struct node));

>     if (*tree == NULL)
>      *tree = new_node;

If tree is not NULL, what happens with the allocated memory ?

Quote:

>     else
>     {
>          if (postcode < *tree->number)
>               *tree->left = insert (*tree->left, number);

insert() is defined as a void, and therefore cannot return a value.
I think you should make insert() return a pointer to a node.

Quote:
>          else
>           if (postcode > tree->number)
>               *tree->right = insert (*tree->right, number);

>           else tree->frequency ++;
>     }

Return tree here

Quote:
>}

I think you have no problems with binary trees, but
you may want to check the pointers section of your C book.

Cheers,

Marcel

Email: mlipman at iname dot com

Lazlo's Chinese Relativity Axiom: