Adding to a binary search tree 
Author Message
 Adding to a binary search tree

Quote:

> Can someone show me how2 add to a binary search tree?

/* Inserts ITEM into TREE.  Returns 1 if the item was inserted, 2 if
   an identical item already existed in TREE, or 0 if a memory
   allocation error occurred. */
int
bin_insert(struct bin_tree *tree, int item)
{
  struct bin_node *node, **new;

  assert(tree != NULL);
  new = &tree->root;
  node = tree->root;
  for (;;) {
    if (node == NULL) {
      node = *new = malloc(sizeof *node);
      if (node == NULL)
        return 0;
      node->data = item;
      node->left = node->right = NULL;
      tree->count++;
      return 1;
    }
    else if (item == node->data)
      return 2;
    else if (item > node->data) {
      new = &node->right;
      node = node->right;
    }
    else {
      new = &node->left;
      node = node->left;
    }
  }

Quote:
}

--
"I should killfile you where you stand, worthless human." --Kaz


Sat, 18 May 2002 03:00:00 GMT  
 Adding to a binary search tree
Can someone show me how2 add to a binary search tree?


Sun, 19 May 2002 03:00:00 GMT  
 Adding to a binary search tree

Quote:
>Can someone show me how2 add to a binary search tree?

Original tree:

    e
   / \
  b   f
 / \  
a   c

Adding new item d:

    e
   / \
  b   f
 / \  
a   c
     \
      d



Sun, 19 May 2002 03:00:00 GMT  
 Adding to a binary search tree


|>Can someone show me how2 add to a binary search tree?

| Original tree:

|     e
|    / \
|   b   f
|  / \  
| a   c

| Adding new item d:

|     e
|    / \
|   b   f
|  / \  
| a   c
|      \
|       d

Rebalance:

      c
     / \
    b   e
   /   / \
  a   d   f

--
-----------------------------------------------------------------
| Phil Howard - KA9WGN |   Dallas   | http://linuxhomepage.com/ |

-----------------------------------------------------------------



Sun, 19 May 2002 03:00:00 GMT  
 Adding to a binary search tree
I do not think rebalancing is neccessary in a binary search tree... In an
AVL-trees or red-black trees, it is.

Henning Stensrud

Quote:
> Rebalance:

>       c
>      / \
>     b   e
>    /   / \
>   a   d   f

> --
> -----------------------------------------------------------------
> | Phil Howard - KA9WGN |   Dallas   | http://linuxhomepage.com/ |

> -----------------------------------------------------------------



Sun, 19 May 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Binary Search Trees

2. Binary Search Trees

3. Binary search tree

4. binary search tree problem

5. Binary Search Tree using dynamic memory allocation (URGENT request for example)

6. searching for a structure in a binary tree

7. What is the algorithm of binary search tree?

8. Binary search tree doesn't work properly

9. Help... Binary Search Tree

10. Binary Search Tree

11. algorythm to balance a binary search tree

12. Binary search trees

 

 
Powered by phpBB® Forum Software