Clarification: Trying to master binary trees - please help. 
Author Message
 Clarification: Trying to master binary trees - please help.

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  
 Clarification: Trying to master binary trees - please help.

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:
No matter how great your triumphs or how tragic your defeats,
approximately one billion Chinese couldn't care less.



Sat, 18 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Trying to master binary trees - please help.

2. AVL tree (Binary Balanced tree) using C or C++

3. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

4. Help... Binary Search Tree

5. Binary tree traversal - a complex case - Help!

6. binary trees...help!!

7. Help ! Binary Tree

8. Help ! Binary Tree

9. Binary Tree help Urgent

10. Binary Tree help Urgent

11. help in binary search tree

12. Help with binary trees

 

 
Powered by phpBB® Forum Software