//========================makeEmptyHelper=============================
// Deletes the nodes in the given tree recursively.
//
// Preconditions: root is not NULL.
// 		 
// Postconditions: The tree is empty.
//==================================================================== 
void BSTree::makeEmptyHelper (Node *&root) const
{
	if (root != NULL) {
		makeEmptyHelper (root->left);
		makeEmptyHelper (root->right);
		delete root->item;
		delete root;
		root->item = NULL;				
	}	
	root = NULL;
}
void BinTree::makeEmptyHelper( Node* currentNode ) {
 // check that their is something in the data location to delete
 if ( currentNode->left != NULL ) {
  makeEmptyHelper( currentNode->left );
 }
 if ( currentNode->right != NULL ) {
  makeEmptyHelper( currentNode->right );
 }
 delete currentNode->data;
 delete currentNode;
}
//------------------------------------------------------------------------------
// makeEmptyHelper
// helper to makeEmpty function
void BSTree::makeEmptyHelper(Node* target) {
	if(target == NULL) {
		return;
	}
	makeEmptyHelper(target->left);
	makeEmptyHelper(target->right);
	delete target->data;
	target->data = NULL;
	target->left = NULL;
	target->right = NULL;
	delete target;
	target = NULL;
}
//----------------------------------------------------------------------------
// makeEmptyHelper
// This is a helper function that deletes nodes and data.
void BinTree::makeEmptyHelper(Node*& head){
    if(head != NULL){
        // going through the tree
        makeEmptyHelper(head->left);
        makeEmptyHelper(head->right);
        
        // deleting the stuff
        delete head->data;
        head->data = NULL;
        delete head;
        head = NULL;
    }
}
void BinTree::makeEmpty(){
 // only delete the tree if it isn't already empty
 if ( !isEmpty() ) {
  makeEmptyHelper( root ); // start deleting from the root, recursively
 }
 root = NULL;
}
Beispiel #6
0
////////////////////////////////////////////////////////////////////////////////////////////////////
// Assignment Operator: assigns a tree (via CopyTree). 
// Pre Conditions: Trees are properly initialized
// Post Conditions: copies the source tree into its own tree if they are not the same
////////////////////////////////////////////////////////////////////////////////////////////////////
	BinTree& BinTree::operator=(const BinTree &source)
	{
		if (&source != this) { 
			makeEmptyHelper(this->root);
			CopyTree(this->root, source.root);
		}
		return *this;
	}
Beispiel #7
0
////////////////////////////////////////////////////////////////////////////////////////////////////
// makeEmptyhelper: Helper function to make the tree empty recursively
// Pre Conditions: Tree is properly initialized
// Post Conditions: tree is made empty 
////////////////////////////////////////////////////////////////////////////////////////////////////
	void BinTree::makeEmptyHelper(Node* &current)  
	{	
		if (current == NULL) {
			return;
		}

		if (current->left != NULL || current->right != NULL) {
			makeEmptyHelper(current->left);
			makeEmptyHelper(current->right);
			current->left = NULL;
			current->right = NULL;
		}
		
		if (current->left == NULL && current->right == NULL)  {
			delete current;
			return;
		} 
	}
//------------------------------------------------------------------------------
// makeEmpty
// deallocates all memory from the tree
void BSTree::makeEmpty() {
	makeEmptyHelper(root);
}
Beispiel #9
0
////////////////////////////////////////////////////////////////////////////////////////////////////
// makeEmpty: Function to make tree empty
// Pre Conditions: Tree is properly initialized
// Post Conditions: Tree is empty and root is set to NULL
////////////////////////////////////////////////////////////////////////////////////////////////////
	void BinTree::makeEmpty() { 
		makeEmptyHelper(root); 
		root = NULL;
	}