コード例 #1
0
bst_t bst_remove(bst_t bst, index_t index) {


    if (bst_type(bst) == isNotEmpty) {

        switch (index_compare(index, bst)) {

        case EQ:

            switch (has_child(bst)) {

            case none:
                bst = bst_destroy(bst);
                break;

            case both:
            case justRigth:

                pair_destroy(bst->pair);
                bst->pair = pair_copy((bst->der)->pair);        /*copia el par q le sigue (der) */
                bst->der =
                    bst_remove(bst->der, pair_fst((bst->der)->pair));
                break;

            case justLeft:

                pair_destroy(bst->pair);
                bst->pair = pair_copy((bst->izq)->pair);        /*copia el par q le sigue (der) */
                bst->izq =
                    bst_remove(bst->izq, pair_fst((bst->izq)->pair));
                break;

            }

            break;

        case LT:
            bst->izq = bst_remove(bst->izq, index);
            break;

        case GT:
            bst->der = bst_remove(bst->der, index);
            break;

        }


    }


    return (bst);

}
コード例 #2
0
ファイル: test.c プロジェクト: jwill006/BST
void t_avl() {
	int i;
	BST_PTR t = bst_create();
	int *a = gen_random_arr(SAMPLE_SIZE);
	
    for(i=0; i<SAMPLE_SIZE; i++)
        bst_insert(t, i);	    	
	
	printf("Height of root: %d\n", bst_height(t));
	printf("Size of tree: %d\n", bst_size(t));
	printf("Min elem: %d\n", bst_min(t));
	printf("Max elem: %d\n", bst_max(t));
	printf("Nearest elem: %d\n", bst_get_nearest(t,500));
	printf("Num LEQ: %d\n", bst_num_leq(t,10));
	
	for(i=0; i<SAMPLE_SIZE-1; i++) {
		bst_remove(t,a[i]);
		printf ("Delete %d\n", a[i]);
	}
	
	assert(bst_to_array(t)[0]==a[SAMPLE_SIZE-1]);
	
	printf("Height of root: %d\n", bst_height(t));
	printf("Size of tree: %d\n", bst_size(t));
	printf("Min elem: %d\n", bst_min(t));
	printf("Max elem: %d\n", bst_max(t));
	printf("Nearest elem: %d\n", bst_get_nearest(t,500));
	printf("Num LEQ: %d\n", bst_num_leq(t,10));
	
	free(a);
	bst_free(t);
}
コード例 #3
0
ファイル: bst_test.cpp プロジェクト: calmattoso/EDA
int _delete(bst * h, char * str){
  bst_ret ret;
 
  remTime.start();
    ret = bst_remove(h, str);
  remTime.stop();

  if(ret == bst_NotFound)
    return NOT_FOUND;
 
  return OP_OK;  
} 
コード例 #4
0
ファイル: btNode.cpp プロジェクト: MFoster89/DataStruct
// write definition for bst_remove here
bool bst_remove(btNode* &root, int remInt)
{   
    if (root == 0) // tree empty or doesn't contain the remInt
        return false;

    if (remInt < root->data) // left child
        bst_remove(root->left, remInt);
    else if (remInt > root->data) // right child
        bst_remove(root->right, remInt);
    else if (remInt == root->data)
    {
        if(root->left == 0 && root->right == 0) // no children
        {
            btNode* oldRoot = root;
            root = 0;
            delete oldRoot;
        }
        else if (root->left == 0 || root->right == 0)
        {
            if (root->right == 0) // left child only
            {
                btNode* oldRoot = root;
                root = root->left;
                delete oldRoot;
            }
            else // right child only
            {
                btNode* oldRoot = root;
                root = root->right;
                delete oldRoot;
            }
        }
        else // two children
            bst_remove_max(root->left, root->data);
        return true; // node successfully removed
    }
}
コード例 #5
0
ファイル: test.c プロジェクト: jwill006/BST
void t_height() {
	int i;
    int a[] = {8, 2, 6, 9, 11, 3, 7};
	
    BST_PTR t = bst_create();
	BST_PTR t2;
	
    for(i=0; i<7; i++)
        bst_insert(t, a[i]);
	
	
	
	bst_remove(t,11);
	bst_remove(t,2);
	bst_preorder(t);
	printf("Min elem: %d\n", bst_min(t));
	printf("Max elem: %d\n", bst_max(t));
	
	printf("Nearest elem: %d\n", bst_get_nearest(t,500));
	printf("Num LEQ: %d\n", bst_num_leq(t,10));
	
	bst_free(t);
	
}
コード例 #6
0
bool bst_remove(binary_tree_node*& root_ptr, const int& target)
// Precondition: root_ptr is a root pointer of a binary search tree
// or may be nullptr for an empty tree).
// Postcondition: If target was in the tree, then one copy of target
// has been removed, and root_ptr now points to the root of the new
// (smaller) binary search tree. In this case the function returns true.
// If target was not in the tree, then the tree is unchanged (and the
// function returns false).
{
    if (root_ptr == nullptr) return false;
    if (target < root_ptr->data)
        return bst_remove(root_ptr->left, target);
    if (target > root_ptr->data)
        return bst_remove(root_ptr->right, target);
    if (root_ptr->left == nullptr)
    {
        binary_tree_node* old_root = root_ptr;
        root_ptr = root_ptr->right;
        delete old_root_ptr;
        return true;
    }
    bst_remove_max(root_ptr->left, root_ptr->data);
    return true;
}
コード例 #7
0
ファイル: dict.c プロジェクト: tinrodriguez8/sourceProjects
dict_t dict_remove(dict_t dict, word_t word) {

    assert(dict != NULL && word != NULL && dict_exists(dict, word));
    index_t index = index_from_string(word);
    if (dict_exists(dict, word)) {

        dict->length = dict->length - 1;
        dict->data = bst_remove(dict->data, index);

        free(index);
        index = NULL;
    }

    return dict;
}
コード例 #8
0
ファイル: bst.c プロジェクト: tinrodriguez8/sourceProjects
bst_t bst_remove(bst_t bst, index_t index) {
    bst_t current;

    if (bst != NULL) {

        if (index_is_less_than(index, pair_fst(bst->pair))) {
            bst->left = bst_remove(bst->left, index);
        } else if (index_is_equal(pair_fst(bst->pair), index) &&
                   bst->left == NULL) {
            current = bst->right;
            bst->pair = pair_destroy(bst->pair);
            free(bst);
            bst = current;
        } else if (index_is_equal(pair_fst(bst->pair), index) &&
                   bst->left != NULL) {
            bst->pair = pair_destroy(bst->pair);
            bst->pair = bst_max(bst->left);
            bst->left = delete_max(bst->left);
        } else {
            bst->right = bst_remove(bst->right, index);
        }
    }
    return (bst);
}
コード例 #9
0
dict_t dict_remove(dict_t dict, word_t word) {

    /*Precondition verification*/
    assert(word != NULL);
    assert(dict != NULL);
    assert(dict_exists(dict, word));

    index_t index = index_from_string(word);
    
    dict->data      = bst_remove(dict->data, index);
    dict->length   -= 1;

    index_destroy(index);

    return (dict);
}
コード例 #10
0
void *test(void *data) {
	fprintf(stderr, "Starting test\n");
	//get the per-thread data
    thread_data_t *d = (thread_data_t *)data;

    //place the thread on the apropriate cpu
    set_cpu(d->id);
    int op_count = 10000;

    ssalloc_init();

    /* Wait on barrier */
    barrier_cross(d->barrier);

	int i;
	bst_value_t* val = (bst_value_t*)malloc(sizeof(bst_value_t));
	bst_value_t* added;

	for ( i = 1; i <= op_count; i++){

		*val = d->id*op_count+i;
        // bst_value_t val = d->id*op_count+i;
		// fprintf(stderr, "[%d] before add\n", pthread_self());
		added = bst_put(i, val, root);
		// fprintf(stderr, "[%d] Added %d\n", pthread_self(), i);

		// fprintf(stderr, "[%d] Added %d? %d\n", d->id, i, added==TRUE);
        if (added == NULL) {
            d->num_insert++;
            FAI_U8(&v[i]);
        }
	}

	// printf("Root right node: %d", root->right->key);
	
	for (i = 1; i <= op_count; i++){

		bool_t found = (bst_get(i, root) != NULL);
		// printf("Contains %d? %d\n", i, found==FOUND);
		if (found) {
			d->num_search ++;
		}
	}

	// fprintf(stderr, "After insertions, found %d\n", d->num_search); 

	// d->num_search = 0;

	for ( i = 1; i <= op_count; i++){

		bool_t removed = (bst_remove(i, root) != NULL);
		// printf("Removed %d? %d\n", i, removed==TRUE);
		if (removed == TRUE) {
			d->num_remove ++;
            FAI_U8(&v[i]);
		}
	}

	// for ( i = 1; i <= op_count; i++){

	// 	bool_t found = (bst_get(i) != NULL);
	// 	// printf("Contains %d? %d\n", i, found==FOUND);
	// 	if (found) {
	// 		d->num_search ++;
	// 	}
	// }
	
	// fprintf(stderr, "After deletions, found %d\n", d->num_search); 


	return NULL;
}
コード例 #11
0
void *test(void *data)
{
    DDPRINT("starting test\n",NULL);
    //get the per-thread data
    thread_data_t *d = (thread_data_t *)data;
    //scale percentages of the various operations to the range 0..255
    //this saves us a floating point operation during the benchmark
    //e.g instead of random()%100 to determine the next operation we will do, we can simply do random()&256
    //this saves time on some platfroms
    uint32_t read_thresh = 256 * finds / 100;
    //uint32_t write_thresh = 256 * (finds + inserts) / 100;
    //place the thread on the apropriate cpu
    set_cpu(d->id);
    //initialize the custom memeory allocator for this thread (we do not use malloc due to concurrency bottleneck issues)
    ssalloc_init();
    // ssalloc_align();
    bst_init_local(d->id);
    //for fine-grain latency measurements, we need to get the lenght of a getticks() function call, which is also counted
    //by default when we do getticks(); //code... getticks(); PF_START and PF_STOP use this when fine grain measurements are enabled
    PF_CORRECTION;
    uint32_t rand_max;
    //seed the custom random number generator
    seeds = seed_rand();
    rand_max = max_key;
    uint32_t op;
    skey_t key;
    int i;
    int last = -1;

    DDPRINT("staring initial insert\n",NULL);
    DDPRINT("number of inserts: %u up to %u\n",d->num_add,rand_max);
    //before starting the test, we insert a number of elements in the data structure
    //we do this at each thread to avoid the situation where the entire data structure 
    //resides in the same memory node

    // int num_elem = 0;
    // if (num_threads == 1){
    //     num_elem = max_key/2;
    // } else{
    //     num_elem = max_key/4;
    // }
    // pthread_mutex_lock(d->init_lock);
    // fprintf(stderr, "Starting critical section %d\n", d->id);
    for (i=0;i<max_key/4;++i) {
        key = my_random(&seeds[0],&seeds[1],&seeds[2]) & rand_max;
 
        DDPRINT("key is %u\n",key);
        //we make sure the insert was effective (as opposed to just updating an existing entry)
        if (d->id < 2) {
        if (bst_add(key,root, d->id)!=TRUE) {
            i--;
        }   }
    }

    // fprintf(stderr, "Exiting critical section %d\n", d->id);
    // pthread_mutex_unlock(d->init_lock);
    DDPRINT("added initial data\n",NULL);

    bool_t res;
    /* Init of local data if necessary */
    ticks t1,t2;
    /* Wait on barrier */
    // fprintf(stderr, "Waiting on barrier; thread %d\n", d->id);
    barrier_cross(d->barrier);
    //start the test
    while (*running) {
        //generate a key (node that rand_max is expected to be a power of 2)
        key = my_random(&seeds[0],&seeds[1],&seeds[2]) & rand_max;
        //generate the operation
        op = my_random(&seeds[0],&seeds[1],&seeds[2]) & 0xff;
        if (op < read_thresh) {
            //do a find operation
            //PF_START and PF_STOP can be used to do latency measurements of the operation
            //to enable them, DO_TIMINGS must be defined at compile time, otherwise they do nothing
            //PF_START(2);
            bst_contains(key,root, d->id);
            //PF_STOP(2);
        } else if (last == -1) {
            //do a write operation
            if (bst_add(key,root, d->id)) {
                d->num_insert++;
                last=1;
            }
        } else {
            //do a delete operation
            if (bst_remove(key,root, d->id)) {
                d->num_remove++;
                last=-1;
            }
        }
        d->num_operations++;
        //memory barrier to ensure no unwanted reporderings are happening
        //MEM_BARRIER;
    }
    //summary of the fine grain measurements if enabled 
    PF_PRINT;
    return NULL;
}
コード例 #12
0
ファイル: bst.c プロジェクト: jsun23/collegeStuff
tnode *bst_remove(tnode *ptr, int key, int *pfound) {

   tnode *max_node_ptr, *temp_ptr ;

   if (ptr == NULL) {   // not in NULL tree
      *pfound = 0 ;
      return NULL ;
   }

   if (ptr->data < key) {   // too big, remove from right

      ptr->right = bst_remove(ptr->right, key, pfound) ;
      if (*pfound) ptr->size-- ;
      return ptr ;

   } else if (key < ptr->data) { // too small, remove from left

      ptr->left = bst_remove(ptr->left, key, pfound) ;
      if (*pfound) ptr->size-- ;
      return ptr ;

   } else {   // ptr->data == key

       // just right, remove current node

      *pfound = 1 ;

       // removing current node is broken down into cases
       // depending on whether current node has any subtrees.


       // Case 1: no right subtree
       // left subtree becomes root of modified subtree
       //
      if (ptr->right == NULL) {

         temp_ptr = ptr->left ;
         ptr->left = NULL ;     // paranoid
         ptr->right = NULL ;
         free(ptr) ;
         return temp_ptr ;

      // Case 2: no left subtree
      // right subtree becomes root of modified subtree
      //
      } else if (ptr->left == NULL) {

         temp_ptr = ptr->right ;
         ptr->left = NULL ;     // paranoid
         ptr->right = NULL ;
         free(ptr) ;
         return temp_ptr ;

      // Case 3: has both subtrees
      // Remove max item from left subtree and make
      // that the new root of current subtree
      //
      } else {  // neither child is NULL

         // remove max item from left subtree
         ptr->left = bst_remove_max(ptr->left, &max_node_ptr) ;

         // max item takes over as root.
         max_node_ptr->left = ptr->left ;
         max_node_ptr->right = ptr->right ;
         max_node_ptr->size = ptr->size - 1 ;

         // Remove current item
         ptr->left = NULL ;   // paranoid
         ptr->right = NULL ;
         free(ptr) ;

         return max_node_ptr ;
      }

   }

}