www.delorie.com/gnu/docs/avl/libavl_36.html   search  
 
Buy GNU books!


GNU libavl 2.0.1

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.2.1 Node Structure

When binary search trees were introduced in the last chapter, we used indexes into an array to reference items' smaller and larger values. But in C, BSTs are usually constructed using pointers. This is a more general technique, because pointers aren't restricted to references within a single array.

 
/* A binary search tree node. */
struct bst_node 
{ struct bst_node *bst_link[2]; /* Subtrees. */ void *bst_data; /* Pointer to data. */ };
This code is included in @refalso{24

In struct bst_node, bst_link[0] takes the place of smaller, and bst_link[1] takes the place of larger. If, in our array implementation of binary search trees, either of these would have pointed to the sentinel, it instead is assigned NULL, the null pointer constant.

In addition, bst_data replaces value. We use a void * generic pointer here, instead of int as used in the last chapter, to let any kind of data be stored in the BST. See section 3.3 Comparison Function, for more information on void * pointers.


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003