/* returns a pointer to the node containing interval |q|; if not present, return NULL */ struct int_node* int_tree_find( struct int_tree* tree, const struct interval* q, uint16_t current_dim ) { struct int_node* new_node = tree->root; while ( new_node && int_compare(new_node->in, q, current_dim ) ) { if ( int_compare(q, new_node->in, current_dim ) > 0 ) new_node = new_node->right; else new_node = new_node->left; } return new_node; }
int main(void) { int x; int y; return int_compare(x, y); }
int town_compare(const void *ip1, const void *ip2){ town *t1 = (town *) ip1, *t2 = (town *) ip2; if (strcmp(t1->region, t2->region) < 0) return -1; else if (strcmp(t1->region, t2->region) > 0) return 1; else return -int_compare(t1->population, t2->population); }
/* recursively delete the node containing interval |q| from the subtree rooted at |n|; returns the (possibly new) root of the tree previously rooted at |n|. If interval |q| is found and deleted, set |*found| to 1; otherwise |*found| is left unchanged. */ static struct int_node* int_tree_delete_rec( struct int_node* n, const struct interval* q, int *found, uint16_t current_dim ) { if ( n == NULL ) return n; struct int_node* new_node = n; int c = int_compare( q, n->in, current_dim ); switch(c) { case 0: *found = 1; if ( n->left == NULL ) { /* Case 1: empty left subtree */ new_node = n->right; free( n ); } else { if ( n->right == NULL ) { /* Case 2: empty right subtree */ new_node = n->left; free( n ); } else { /* Case 3: both the right and left subtrees are not empty. We found and delete the rightmost node in the left subtree. */ struct int_node* max = n->left; while ( max->right ) max = max->right; new_node->in = max->in; new_node->left = int_tree_delete_rec( n->left, max->in, found, current_dim ); } } break; case 1: n->right = int_tree_delete_rec( n->right, q, found, current_dim ); break; case -1: n->left = int_tree_delete_rec( n->left, q, found, current_dim ); break; } /* rebalance if necessary */ if ( int_node_update( new_node, current_dim ) ) new_node = int_node_balance( new_node, current_dim ); return new_node; }
static struct int_node* int_tree_insert_rec( struct int_node* n, const struct interval* interv, uint16_t current_dim ) { struct int_node* new_node = n; if ( n == NULL ) { /* Create and initialize new node */ new_node = (struct int_node*)malloc( sizeof( struct int_node) ); assert( new_node ); new_node->in = interv; new_node->left = new_node->right = NULL; new_node->max_upper = interv->upper[current_dim]; new_node->min_lower = interv->lower[current_dim]; new_node->height = new_node->unbalance = 0; } else { if ( int_compare( interv, n->in, current_dim ) < 0 ) new_node->left = int_tree_insert_rec( new_node->left, interv, current_dim ); else new_node->right = int_tree_insert_rec( new_node->right, interv, current_dim ); /* rebalance if necessary */ if ( int_node_update( new_node, current_dim ) ) new_node = int_node_balance( new_node, current_dim ); } return new_node; }
int int_compare2(const void *i, const void *j) { return int_compare((int*) i, (int*) j); }