Пример #1
0
rtems_rbtree_node *rtems_rbtree_find(
  const rtems_rbtree_control *the_rbtree,
  const rtems_rbtree_node    *the_node,
  rtems_rbtree_compare        compare,
  bool                  is_unique
)
{
  rtems_rbtree_node *iter_node = rtems_rbtree_root( the_rbtree );
  rtems_rbtree_node *found = NULL;

  while ( iter_node != NULL ) {
    rtems_rbtree_compare_result compare_result =
      ( *compare )( the_node, iter_node );

    if ( rtems_rbtree_is_equal( compare_result ) ) {
      found = iter_node;

      if ( is_unique )
        break;
    }

    if ( rtems_rbtree_is_greater( compare_result ) ) {
      iter_node = rtems_rbtree_right( iter_node );
    } else {
      iter_node = rtems_rbtree_left( iter_node );
    }
  }

  return found;
}
Пример #2
0
/* 
 * recursively checks tree. if the tree is built properly it should only 
 * be a depth of 7 function calls for 100 entries in the tree. 
 */
static int rb_assert ( rtems_rbtree_node *root )
{
  int lh, rh;

  if ( root == NULL )
    return 1;
  else {
    rtems_rbtree_node *ln = rtems_rbtree_left(root);
    rtems_rbtree_node *rn = rtems_rbtree_right(root);

    /* Consecutive red links */
    if ( root->color == RBT_RED ) {
      if ((ln && ln->color == RBT_RED)  || (rn && rn->color == RBT_RED)) {
        puts ( "Red violation" );
        return -1;
      }
    }

      lh = rb_assert ( ln );
      rh = rb_assert ( rn );

    /* Invalid binary search tree */
    if ( ( ln != NULL && test_compare_function(ln, root) > 0 )
        || ( rn != NULL && test_compare_function(rn, root) < 0 ) )
    {
      puts ( "Binary tree violation" );
      return -1;
    }

    /* Black height mismatch */
    if ( lh != -1 && rh != -1 && lh != rh ) {
      puts ( "Black violation" );
      return -1;
    }

    /* Only count black links */
    if ( lh != -1 && rh != -1 )
      return ( root->color == RBT_RED ) ? lh : lh + 1;
    else
      return -1;
  }
}