Esempio n. 1
0
void testRemoveLeftMost() {
    struct BSTree *tree = buildBSTTree();
    struct data *myData;
    struct data *delete_val; /*In this case we need another struct to delete value 105
    Since _removeLeftMost is not supposed to delete the value (it may be pointed to by another element in the tree)
    we will need to manually delete it ourselves once the node has been deleted.*/

    myData = tree->root->left->left->val;
    _removeLeftMost(tree->root);
    printTestResult( (tree->root->left->left == NULL), "_removeLeftMost", "Removing leftmost from Root");
    free(myData);

    delete_val = tree->root->left->val;
    myData = tree->root->left->right->val;
    _removeLeftMost(tree->root);
    printTestResult( (compare(tree->root->left->val, myData) == 0), "_removeLeftMost", "Removing leftmost from Root again");
    free(delete_val);

    delete_val = tree->root->right->right->left->val;
    myData = tree->root->right->right->left->right->val;
    _removeLeftMost(tree->root->right->right);
    free(delete_val);
    printTestResult( (compare(tree->root->right->right->left->val, myData) == 0), "_removeLeftMost", "removing leftmost from Root->Right->Right");

    printf("Deleting the BSTree...\n");
    deleteBSTree(tree);
    printf("Returning from testRemoveLeftMost().\n");
}
Esempio n. 2
0
void testRemoveLeftMost() {
    struct BSTree *tree = buildBSTTree();
    struct Node *cur;
    
    cur = _removeLeftMost(tree->root);

	printTestResult(cur == tree->root, "_removeLeftMost", "removing leftmost of root 1st try");
    
    cur = _removeLeftMost(tree->root->right);
    printTestResult(cur == NULL, "_removeLeftMost", "removing leftmost of right of root 1st try");
   
 	cur = _removeLeftMost(tree->root);
    printTestResult(cur == tree->root, "_removeLeftMost", "removing leftmost of root 2st try");
}
Esempio n. 3
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
	/*write this*/
	assert(cur != NULL);
	assert(val != NULL);
	if (compare(cur->val, val) == 0)
	{
		if (cur->right != NULL){
			cur->val = _leftMost(cur->right);
			cur->right = _removeLeftMost(cur->right);
		}
		else
		{
			return cur->left;
		}
	}
	else if (compare(cur->val, val) < 0)
	{
		cur->right = _removeNode(cur->right, val);
	}
	else if (compare(cur->val, val) > 0)
	{
		cur->left = _removeNode(cur->left, val);
		
	}
	return cur;
	

}
Esempio n. 4
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
	/*write this*/
	struct BSTree *tempTree = newBSTree();
	tempTree->root = cur;

	assert(val);
	assert(cur);
	assert(containsBSTree(tempTree, val));

	if (compare(val, cur->val) == 0) {
        if (cur->right == 0) {
            return cur->left;
        }
        else {
            cur->val = _leftMost(cur);
            cur->right = _removeLeftMost(cur);
        }
	}
	else {
        if (compare(val, cur->val) == -1) {
            cur->left = _removeNode(cur->left, val);
        }
        else {
            cur->right = _removeNode(cur->right, val);
        }
	}
    return cur;

}
Esempio n. 5
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
  assert(cur);
  int compareResult = compare(cur->val, val);

	if(compareResult == 1) // node value is greater than d
    cur->left = _removeNode(cur->left, val);
  else if(compareResult == -1) // node value is less than d
    cur->right = _removeNode(cur->right, val);
  else // node value is equal to d, so remove the node
  {
    if(cur->right) // if there is a right child for cur
    {
      /* Replace cur node with the left-most descendant of its right child */
      cur->val = _leftMost(cur->right);
      cur->right = _removeLeftMost(cur->right);
    }
    else if(cur->left) // if there is no right-hand child but there is a //left child
    {
    /* Replace cur node with its left child */
      struct Node *temp = cur->left;
      cur->val = temp->val;
      cur->left = temp->left;
      cur->right = temp->right;
      free(temp);
			temp = NULL;
    }else // cur has no children; it can be freed without detaching a part of the BST
    {
      free(cur);
      cur = NULL;
    }
  }
  return cur;
}
Esempio n. 6
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
	/*write this*/

	assert (cur != 0);
	assert (val != 0);

	// confirm val is in the tree
    if (compare(cur->val, val) == 0)
    {
        if (cur->right == 0)                    //BASE CASE: if val is equiv, and there's no right child, free cur
        {
            _freeBST(cur);
            return 0;                           //returning null bc that's the value of cur
        }
        else                                    //val is equiv, but there's right child cur
        {
            cur->val = _leftMost(cur->right);
			cur->right = _removeLeftMost(cur->right);
        }
    }
    else if (compare(cur->val, val) == 1)       //RECURSIVE CASE: 1
    {
        cur->left = _removeNode(cur->left, val);
    }
    else                                        //RECURSIVE CASE: compare(cur->val, val) == -1
    {
        cur->right = _removeNode(cur->right, val);
    }

    return cur;

}
Esempio n. 7
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
    if(compare(val, cur->val) == -1)
        cur->left = _removeNode(cur->left, val);
    else if(compare(val, cur->val) == 1)
        cur->right = _removeNode(cur->right, val);
    else
    {
        if (cur->left == 0 && cur->right == 0)
            return NULL;
        else if (cur->left == 0) {
            struct Node *retval = cur->right;
            free(cur);
            return retval;
        }
        else if (cur->right == 0) {
            struct Node *retval = cur->left;
            free(cur);
            return retval;
        }
        else {
            cur->val = _leftMost(cur->right);
            cur->right = _removeLeftMost(cur->right);
        }
    }
    return cur;
}
Esempio n. 8
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
	/*write this*/
  if (compare(cur->val, val) == 0) {
    if ((cur->right) == 0) {
			struct Node *newNode = cur->left;

			free(cur);

			return newNode;
		}
    else {
			cur->val = _leftMost(cur->right);
			cur->right = _removeLeftMost(cur->right);
    }
  }
  else if (compare(cur->val, val) == 1) {
		cur->left = _removeNode(cur->left, val);
	}
  else if (compare(cur->val, val) == -1) {
		cur->right = _removeNode(cur->right, val);
	}

  return cur;
}
Esempio n. 9
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
    /*write this*/
    printf("- In _removeNode\n");
    assert(cur !=NULL);
    assert(val!=NULL);
    if(compare( cur->val,val)==0){
    	if(cur->right==NULL){
		struct Node* temp = cur->left;
		free(cur);
		return	temp;
	}
	cur->val = _leftMost(cur->right);
	cur->right = _removeLeftMost(cur->right);


    }
    else if(compare(cur->val,val) ==1){
    	cur->left = _removeNode(cur->left,val);
    }
    else{
    	cur->right = _removeNode(cur->right, val);
    }
    return cur;



}
Esempio n. 10
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
	/*write this*/
	
if (compare(val, cur->val) == -1)
	cur->left = _removeNode(cur->left, val);
else if (compare(val, cur->val) == 1)
	cur->right = _removeNode(cur->right, val);
else if (compare(val, cur->val) == 0){

    if (cur->right == NULL) {

        struct Node* temp = cur->left;
	free(cur);
	return temp;}
	
    if (cur->left == NULL){
        struct Node* temp = cur->right;
        free(cur);
        return temp;}
	

    else{
        cur->val = _leftMost(cur->right);
        cur->right = _removeLeftMost(cur->right);

    }
	}
	return cur;
}
Esempio n. 11
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur){
	assert(cur != 0);
	if(cur->left != 0){
		cur->left = _removeLeftMost(cur->left);
		return cur;
	}
	struct Node *rightNode = cur->right;
	free(cur);
	return rightNode;
}
Esempio n. 12
0
/*
  recursive helper function to remove the left most child of a node
  HINT: this function returns cur if its left child is NOT NULL. Otherwise,
  it returns the right child of cur and free cur.

  Note:  If you do this iteratively, the above hint does not apply.

  param: cur	the current node
  pre:	cur is not null
  post:	the left most node of cur is not in the tree
*/
struct Node *_removeLeftMost(struct Node *cur)
{
  if(cur->left != NULL) {
    cur->left = _removeLeftMost(cur->left);
    return cur;
  }

  struct Node *tmp;
  tmp = cur->right;
  free(cur);
  return tmp;
}
Esempio n. 13
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur)
{
	if(cur->left == 0)
    {
        struct Node* retNode = cur->right;
        free(cur);
        return retNode;
    }
    else
        cur->left = _removeLeftMost(cur->left);

    return cur;
}
Esempio n. 14
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur)
{
	assert(cur);
	struct Node * right;
	if(cur->val == _leftMost(cur)){
		right = cur->right;
		free((void*)cur);
		cur = NULL;
		return right;
	}else{
		cur->left = _removeLeftMost(cur->left);
		return cur;
	}
}
Esempio n. 15
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur)
{
	/*write this*/
  if (cur->left == NULL) {
    struct Node *newNode = (struct Node *)cur->right;

    free(cur);

    return newNode;
  }

  cur->left = _removeLeftMost(cur->left);

  return cur;
}
Esempio n. 16
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur)
{
    /*write this*/
    assert(cur!=NULL);

    if(cur->left==NULL){
      struct Node * temp =cur;
      free(temp);
      return cur->right;
    }
    else{
      cur->left= _removeLeftMost(cur->left);
      return cur;
    }
}
Esempio n. 17
0
void testRemoveLeftMost() {
    struct BSTree *tree = buildBSTTree();
    struct Node *cur;
    
    cur = _removeLeftMost(tree->root);
    if (cur == tree->root)
        printf("_removeLeftMost: PASS removing leftmost of root 1st try\n");
    else 
        printf("_removeLeftMost: FAIL removing leftmost of root 1st try\n");
    
    cur = _removeLeftMost(tree->root->right);
    if (cur == NULL)
        printf("_removeLeftMost: PASS removing leftmost of right of root 1st try\n");
    else 
        printf("_removeLeftMost: FAIL removing leftmost of right of root 1st try\n");
        
    
    cur = _removeLeftMost(tree->root);
    if (cur == tree->root)
        printf("_removeLeftMost: PASS removing leftmost of root 2st try\n");
    else 
        printf("_removeLeftMost: FAIL removing leftmost of root 2st try\n");
    
}
Esempio n. 18
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur)
{
    assert(cur != NULL);

    if(cur->left == NULL){
        struct Node *temp = cur->right;
        _freeBST(cur);
        return temp;
    }
    // continue down the left side
    else
        cur->left = _removeLeftMost(cur->left);

    return cur;
}
Esempio n. 19
0
struct Node *_removeLeftMost(struct Node *cur)
{
  if(cur->left == 0)
	{
	  //if(cur->right == 0)
	  //_freeBST(cur);
	  //else
	  //_removeLeftMost(cur->right);
	  return cur;
		
	}
	//else
	  //return cur = cur->left;
	
	return _removeLeftMost(cur->left);
}
Esempio n. 20
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
	if(compare(cur->val, val) == 0)
    {
        cur->val = _leftMost(cur->right);
        _removeLeftMost(cur->right);
    }else if(compare(cur->val, val) == -1)
    {
        _removeNode(cur->left, val);
    }else if(compare(cur->val, val) == 1) 
    {
        _removeNode(cur->right, val);
    }
	return cur;

}
Esempio n. 21
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur)
{
	/*write this*/
	if(cur == NULL) {
        return NULL;
    }
    if(cur->left == NULL) {
        
    
    struct Node* parent = cur->right;
        free(cur);
        return parent;
    }
    else 
	cur->left = _removeLeftMost(cur->left);
    return cur;
}	
Esempio n. 22
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur)
{
	/*write this*/
	assert(cur != 0);

	struct Node * temp;
    //base case
    if (cur->left == 0) {
        temp = cur->right;
        free(cur);
        return temp;
    }
    else {
        cur->left = _removeLeftMost(cur->left);
    }
	return cur;
}
Esempio n. 23
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val){
	assert(cur != 0);
	assert(val != 0 && val != NULL);
	if(compare(cur->val, val) == 0){ //found it
		if(cur->right == 0){
			struct Node *temp = cur->left;
			free(cur);
			return temp;
		}
		cur->val = _leftMost(cur->right);
		cur->right = _removeLeftMost(cur->right);
	} else if(compare(cur->val, val) > 0){ //val is less than cur->val. Go right.
		cur->left = _removeNode(cur->left, val);
	} else { //val is greater than cur->val. Go right.
		cur->right = _removeNode(cur->right, val);
	}
	return cur;
}
Esempio n. 24
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur)
{
	if (cur != NULL) {
		if (cur->left != NULL) {
			cur->left = _removeLeftMost(cur->left);
			return cur;
		}

		else {
			struct Node * tmp = cur->right;
			free(cur->right);
			return tmp;
		}
	}

	else
		errorEndProg();
}
Esempio n. 25
0
File: bst.c Progetto: Mankee/CS261
struct Node* _removeNode(struct Node *cur, TYPE val) {
    if(compare(val, cur->val) == 0){
        if(cur->right == NULL){
            return cur->left;
        }
        else{
            cur->val = _leftMost(cur->right);
            cur->right = _removeLeftMost(cur->right);
        }
    }
    else if(compare(val, cur->val) == 1){
        cur->left = _removeNode(cur->left, val);
    }
    else{
        cur->right = _removeNode(cur->right, val);
    }
    return cur;
}
Esempio n. 26
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct BSTree tree, struct Node *cur, TYPE val)
{
	if ((cur != NULL) && (val != NULL)) {
		if (containsBSTree(&tree, val)) {
			struct Node * tmp = NULL;
			//Base Case
			if (compare(val, cur->val) == 0) { //If the value is found
				//If the node has 2 children
				if ((cur->left != NULL) && (cur->right != NULL)) {
					tmp = cur;
					tmp->val = _leftMost(cur->right);
					cur->right = _removeLeftMost(cur->right);
				}

				//If the node has less than 2 children:
				else {
					tmp = cur;
					if (cur->left == NULL) 
						return cur->right;
				
					else if (cur->right == NULL)
						return cur->left;

					free(tmp);
				}
			}

			else if (compare(val, cur->val) == -1)
				cur->left = _removeNode(tree, cur->left, val);

			else if (compare(val, cur->val) == 1)
				cur->right = _removeNode(tree, cur->right, val);

			return cur;
		}

		else
			printf("Error: node with specified not inside BST, cannot remove\n");
	}

	else
		errorEndProg();
}
Esempio n. 27
0
File: bst.c Progetto: Mankee/CS261
struct Node *_removeLeftMost(struct Node *cur) {

    if(cur->left != NULL){
        cur->left = _removeLeftMost(cur->left);

    }
    else{
        if(cur->right != NULL){
        struct Node *temp = cur->right;
        free(cur);
        return temp;
        }
        else{
            free(cur);
            return NULL;
        }
    }
    return cur;
}
Esempio n. 28
0
/*----------------------------------------------------------------------------*/
struct Node *_removeNode(struct Node *cur, TYPE val)
{
	if(compare(cur->val, val) == 0){            //Base Case
        if(cur->right == 0)
            return cur->left;
        else{
            /* Copy lowest child to current */
            cur->val = _leftMost(cur->right);    
            /* Set right child to subtree with lowest child removed */
            cur->right = _removeLeftMost(cur->right);  
        }
    }
    else if(compare(val, cur->val) == -1)
        cur->left = _removeNode(cur->left, val);   //Less than == left
    else
        cur->right = _removeNode(cur->right, val); //Greater than == right

    return cur;

}
Esempio n. 29
0
struct Node *_removeNode(struct Node *cur, TYPE val)
{
        assert(cur != 0);
	assert(val != 0);
	 
	if(compare(val, cur->val) == 0)
	{
		if(cur->right == 0)
		     return cur->left;
		else
		{
			cur->val = _leftMost(cur->right);
			cur->right = _removeLeftMost(cur->right);
		}
	}
	else if(compare(val, cur->val) == -1)
		cur->left = _removeNode(cur->left, val);
	else
		cur->right = _removeNode(cur->right, val);
	
	return cur;
}
Esempio n. 30
0
/*----------------------------------------------------------------------------*/
struct Node *_removeLeftMost(struct Node *cur)
{
	/*write this*/

	assert (cur != 0);

	struct Node* rightSide;

	/* if we get to the point where there's nothing to the left,
       hand off to the temporary Node variable (rightSide)
       what is to the right of what we want to delete, then free and return right */
    if(cur->left == 0)                              //BASE CASE: there's nothing to the left
    {
        rightSide = cur->right;
        _freeBST(cur);
        return rightSide;
    }

    /* move left to find what’s is truly leftmost */
    cur->left = _removeLeftMost(cur->left);         //RECURSIVE CASE: iterate left
    return cur;

}