RBTree_Node *rtems_rbtree_compact_insert( RBTree_Control *the_rbtree, RBTree_Node *the_node, RBTree_Compare compare, bool is_unique ) { RBTree_Node *iter_node = the_rbtree->root; the_node->child[ RBT_LEFT ] = NULL; the_node->child[ RBT_RIGHT ] = NULL; if ( !iter_node ) { /* special case: first node inserted */ the_rbtree->root = the_node; the_rbtree->first[ 0 ] = the_rbtree->first[ 1 ] = the_node; _RBTree_Set_parent_and_color( the_node, (RBTree_Node *) the_rbtree, RBT_BLACK ); } else { /* typical binary search tree insert, descend tree to leaf and insert */ while ( iter_node ) { RBTree_Compare_result compare_result = ( *compare )( the_node, iter_node ); if ( is_unique && _RBTree_Is_equal( compare_result ) ) return iter_node; RBTree_Direction dir = !_RBTree_Is_lesser( compare_result ); if ( !iter_node->child[ dir ] ) { _RBTree_Set_parent_and_color( the_node, iter_node, RBT_RED ); iter_node->child[ dir ] = the_node; /* update min/max */ compare_result = ( *compare )( the_node, _RBTree_First( the_rbtree, dir ) ); if ( ( dir == RBT_LEFT && _RBTree_Is_lesser( compare_result ) ) || ( dir == RBT_RIGHT && !_RBTree_Is_lesser( compare_result ) ) ) { the_rbtree->first[ dir ] = the_node; } break; } else { iter_node = iter_node->child[ dir ]; } } /* while(iter_node) */ /* verify red-black properties */ _RBTree_Validate_insert( the_node ); } return (RBTree_Node *) 0; }
RBTree_Node *_RBTree_Insert( RBTree_Control *the_rbtree, RBTree_Node *the_node, RBTree_Compare compare, bool is_unique ) { RBTree_Node **which = _RBTree_Root_reference( the_rbtree ); RBTree_Node *parent = NULL; while ( *which != NULL ) { RBTree_Compare_result compare_result; parent = *which; compare_result = ( *compare )( the_node, parent ); if ( is_unique && _RBTree_Is_equal( compare_result ) ) { return parent; } if ( _RBTree_Is_lesser( compare_result ) ) { which = _RBTree_Left_reference( parent ); } else { which = _RBTree_Right_reference( parent ); } } _RBTree_Add_child( the_node, parent, which ); _RBTree_Insert_color( the_rbtree, the_node ); return NULL; }