Ejemplo n.º 1
0
void _freeBST(struct Node *node){
	if (node != 0) {
		_freeBST(node->left);
		_freeBST(node->right);
		free(node);
	}
}
Ejemplo n.º 2
0
void _freeBST(struct Node *node)
{
  if(node->left != NULL)
    _freeBST(node->left);

  if(node->right != NULL)
    _freeBST(node->right);

  free(node);
}
Ejemplo n.º 3
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;

}
Ejemplo n.º 4
0
/*
 function to clear the nodes of a binary search tree
 param: tree    a binary search tree
 pre: tree ! = null
 post: the nodes of the tree are deallocated
		tree->root = 0;
		tree->cnt = 0
 */
void clearBSTree(struct BSTree *tree)
{
	assert (tree != 0);

	_freeBST(tree->root);
	tree->root = 0;
	tree->cnt  = 0;
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
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;

}
Ejemplo n.º 7
0
/*
 function to clear the nodes of a binary search tree
 param: tree    a binary search tree
 pre: tree ! = null
 post: the nodes of the tree are deallocated
		tree->root = 0;
		tree->cnt = 0
 */
void clearBSTree(struct BSTree *tree)
{
	_freeBST(tree->root);
	tree->root = 0;
	tree->cnt  = 0;
}