int wasp_tree_remove( wasp_tree tree, wasp_value key ){ wasp_key_fn key_fn = tree->key_fn; wasp_node* root = &(tree->root); wasp_node node; wasp_integer difference; for(;;){ node = *root; if( node == NULL ){ return 0; // Node not found. } difference = wasp_cmp_eqv( key, key_fn( node->data ) ); if( difference < 0 ){ root = &(node->left); }else if( difference > 0 ){ root = &(node->right); }else{ break; } } for(;;){ if( node->left && node->right ){ if( node->left->weight > node->right->weight ){ wasp_raise_right( root ); root = &((*root)->left); }else{ wasp_raise_left( root ); root = &((*root)->right); } }else if( node->left ){ wasp_raise_left( root ); root = &((*root)->right); }else if( node->right ){ wasp_raise_right( root ); root = &((*root)->left); }else{ *root = NULL; return 1; } } }
wasp_node wasp_tree_lookup( wasp_tree tree, wasp_value key ){ wasp_key_fn key_fn = tree->key_fn; wasp_node node = tree->root; while( node ){ if( node == NULL )return NULL; // Node not found. wasp_integer difference = wasp_cmp_eqv( key, key_fn( node->data ) ); if( difference < 0 ){ node = node->left; }else if( difference > 0 ){ node = node->right; }else{ break; } } WASP_AUDIT_TREE( tree ); return node; }
mqo_node mqo_tree_lookup( mqo_tree tree, mqo_value key ){ mqo_key_fn key_fn = tree->key_fn; mqo_node node = tree->root; while( node ){ if( node == NULL )return NULL; // Node not found. mqo_integer difference = mqo_cmp_eqv( key, key_fn( node->data ) ); if( difference < 0 ){ node = node->left; }else if( difference > 0 ){ node = node->right; }else{ break; } } MQO_AUDIT_TREE( tree ); return node; }