void find_successor(struct Node *root, int t, int *s) { static int next = 0; if (!root) return; find_successor (root->left, t, s); if (next) { *s = root->value; return; } else if (root->value == t) { next = 1; } find_successor (root->right, t, s); return; }
int main () { int tval = 1; int successor=-1; struct Node *node = NULL; struct Node *root = NULL; printf("Enter value, 0 to end:\n"); scanf("%d", &tval); while (tval != 0) { node = (struct Node*)calloc(sizeof(struct Node), 1); node->value = tval; if (root == NULL) { root = node; } else { Insert (root, node); } printf("Enter value, 0 to end:\n"); scanf("%d", &tval); } printf ("\n"); inorder_binary_tree (root); printf ("\n"); find_successor (root, 20, &successor) ; printf ("Successor=%d.\n", successor); return 0; }
node_t* remove_key( node_t* root, key_type key ){ node_t* node = search_key(root,key); if( node == NULL ){ printf("Node not found!\n"); return; } if( node->right == NULL && node->left == NULL ){ printf("node:%d\n", node->key.value); free_node(node); return; } node_t* replace = NULL; if( node->left != NULL ){ replace = find_predecessor(node->left); copy_key( &(node->key), replace->key); } else if( node->right != NULL ){ replace = find_successor(node->right); copy_key( &(node->key), replace->key); } free_node(replace); }
// test predecessor & successor of a BST void test6(int n, int min, int max) { test("Test predecessor: ",6); tree *t = create_random_tree(n, min, max); print_ascii_tree(t); int i; for ( i = min; i <= max ; i = i+10) { tree *predecessor = find_predecessor(t, i); printf("%d's predecessor: ", i); if( predecessor == NULL) printf(" not found\n"); else printf(" %d\n", predecessor->data); } test("Test successor: ",6); for ( i = min; i <= max ; i = i+10) { tree *successor = find_successor(t, i); printf("%d's successor: ", i); if( successor == NULL) printf(" not found\n"); else printf(" %d\n", successor->data); } free_tree(t); }
int find_id_pair(struct DotList *dots, struct kdnode *tree, int size1, int size2, int id, int xval, int yval, int option) { int res_id = 0; int x = 1, y = size2; if( option == W_SID ) { x = xval - GAP_THRESHOLD; y = yval - RANGE_TH; if( x <= 0 ) x = 1; if( y <= 0 ) y = 1; res_id = find_pred_blk(tree, x, y); } else if( option == W_FID ) { x = xval + RANGE_TH; y = yval + RANGE_TH; if( x >= size1 ) x = size1; if( y >= size2 ) y = size2; res_id = find_successor(tree, x, y); } else if( option == H_SID ) { x = xval - RANGE_TH; if( dots[id].sign == 0 ) y = yval - GAP_THRESHOLD; else if( dots[id].sign == 1 ) y = yval - RANGE_TH; if( x <= 0 ) x = 1; if( y <= 0 ) y = 1; res_id = find_pred_blk(tree, x, y); } // else if( option == H_FID ) else // option == H_FID { x = xval + RANGE_TH; if( dots[id].sign == 0 ) y = yval + RANGE_TH; else if( dots[id].sign == 1 ) y = yval + GAP_THRESHOLD; if( x >= size1 ) x = size1; if( y >= size2 ) y = size2; res_id = find_successor(tree, x, y); } return(res_id); }
void main () { binary_add(&root, 'f'); binary_add(&root, 'o'); binary_add(&root, 'x'); binary_add(&root, 'i'); binary_add(&root, 's'); binary_add(&root, 'v'); binary_add(&root, 'r'); binary_add(&root, 'y'); preorder(root); inorder(root); postorder(root); node *curr = find_successor(root); if (curr) printf("Found the node %d\n", curr->data); else printf("Node not found!!\n"); }
int find_id_len(struct kdnode *tree, int size, int len, int xval, int yval, int option) { int res_id = 0; int x = 0, y = 1; if( option == W_SID ) { if( len > RANGE_TH ) { x = xval - len; y = yval - len; } else { x = xval - RANGE_TH; y = yval - RANGE_TH; } if( x <= 0 ) x = 1; if( y <= 0 ) y = 1; res_id = find_pred_blk(tree, x, y); } else if( option == W_FID ) { if( len > RANGE_TH ) { x = xval + len; y = yval + len; } else { x = xval + RANGE_TH; y = yval + RANGE_TH; } if( x >= size ) x = size; if( y >= size ) y = size; res_id = find_successor(tree, x, y); } return(res_id); }
int iter_delete(int target,Node *pnt){ int k; int found; if (pnt==NULL) return FALSE; else{ found=linear_search(target,pnt,&k); if (found) if (pnt->branch[k-1]){ find_successor(pnt,k); if (!(found=iter_delete(pnt->key[k],pnt->branch[k]))) printerror("Key not found."); }else remove_key(pnt,k); else found=iter_delete(target,pnt->branch[k]); if (pnt->branch[k] != NULL) if (pnt->branch[k]->count<MIN_ORDER) restore_key(pnt,k); return found; } }
int find_id(struct DotList *dots, struct kdnode *tree, int size, int id, int xval, int yval, int option) { int res_id = 0; int x = 0, y = 1; int len = 0; int sign = -1; if( id == -1 ) { // to redo 'del' events beforing ancestral steps len = RANGE_TH; sign = -1; } else { if( width(dots[id].x) > width(dots[id].y) ) len = width(dots[id].x); else len = width(dots[id].y); sign = dots[id].sign; } len = len + 100; if( option == W_SID ) { x = xval - MDIS_THRESHOLD; if( len > RANGE_TH ) { y = yval - len; } else y = yval - RANGE_TH; if( x <= 0 ) x = 1; if( y <= 0 ) y = 1; res_id = find_pred_blk(tree, x, y); } else if( option == W_FID ) { if( len > RANGE_TH ) { x = xval + len; y = yval + len; } else { x = xval + RANGE_TH; y = yval + RANGE_TH; } if( x >= size ) x = size; if( y >= size ) y = size; res_id = find_successor(tree, x, y); } else if( option == H_SID ) { if( len > RANGE_TH ) { x = xval - len; } else x = xval - RANGE_TH; if( sign == 0 ) y = yval - MDIS_THRESHOLD; else if( (sign == 1) || (sign == -1) ) { if( len > RANGE_TH ) { y = yval - len; } else y = yval - RANGE_TH; } if( x <= 0 ) x = 1; if( y <= 0 ) y = 1; res_id = find_pred_blk(tree, x, y); } else if( option == H_FID ) { if( len > RANGE_TH ) { x = xval + len; } else x = xval + RANGE_TH; if( sign == 0 ) { if( len > RANGE_TH ) { y = yval + len; } else y = yval + RANGE_TH; } else if( (sign == 1) || (sign == -1) ) y = yval + MDIS_THRESHOLD; if( x >= size ) x = size; if( y >= size ) y = size; res_id = find_successor(tree, x, y); } else if( option == OW_SID ) { x = xval - (2 * RANGE_TH); if( sign == 0) y = xval - (2 * RANGE_TH); else if( sign == 1 ) y = yval - MDIS_THRESHOLD; if( x <= 0 ) x = 1; if( y <= 0 ) y = 1; res_id = find_pred_blk(tree, x, y); } // else if( option == OH_FID ) else // option == OH_FID { x = xval + (2 * RANGE_TH); y = yval + (2 * RANGE_TH); if( x >= size ) x = size; if( y >= size ) y = size; res_id = find_successor(tree, x, y); } return(res_id); }