예제 #1
0
파일: main.c 프로젝트: avisinha1/CS-261
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");
}
예제 #2
0
파일: bst.c 프로젝트: nguyminh/CS-261
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");
}
예제 #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;
	

}
예제 #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;

}
예제 #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;
}
예제 #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;

}
예제 #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;
}
예제 #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;
}
예제 #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;



}
예제 #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;
}
예제 #11
0
파일: bst.c 프로젝트: Shazzaa/Classes
/*----------------------------------------------------------------------------*/
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;
}
예제 #12
0
파일: bst.c 프로젝트: gilmanjo/CS-261
/*
  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;
}
예제 #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;
}
예제 #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;
	}
}
예제 #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;
}
예제 #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;
    }
}
예제 #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");
    
}
예제 #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;
}
예제 #19
0
파일: bst.c 프로젝트: MJ21/CSProjects
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);
}
예제 #20
0
파일: bst.c 프로젝트: johnandr/OSU-Work
/*----------------------------------------------------------------------------*/
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;

}
예제 #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;
}	
예제 #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;
}
예제 #23
0
파일: bst.c 프로젝트: Shazzaa/Classes
/*----------------------------------------------------------------------------*/
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;
}
예제 #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();
}
예제 #25
0
파일: bst.c 프로젝트: 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;
}
예제 #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();
}
예제 #27
0
파일: bst.c 프로젝트: 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;
}
예제 #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;

}
예제 #29
0
파일: bst.c 프로젝트: MJ21/CSProjects
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;
}
예제 #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;

}