コード例 #1
0
bool BSTree<DataType, KeyType>::removeHelper(BSTreeNode* &parent,  const KeyType& deleteKey)
{
   if(parent == NULL)
   {
      return false;
   }
   if(deleteKey < parent->dataItem.getKey())
   {
      return removeHelper(parent->left, deleteKey);
   }
   else if(deleteKey > parent->dataItem.getKey())
   {
      return removeHelper(parent->right, deleteKey);
   }
   else if(deleteKey == parent->dataItem.getKey())
   {
      if(parent->left == NULL && parent->right == NULL)
      {
         BSTreeNode* tmp = parent;
         parent = NULL;
         delete tmp;
         return true;
      }
      if(parent->left == NULL)
      {
         BSTreeNode* tmp = parent->right;
         parent->dataItem = tmp->dataItem;
         parent->left = tmp->left;
         parent->right = tmp->right;
         delete tmp;
         return true;
      }
      if(parent->right == NULL)
      {
         BSTreeNode* tmp = parent->left;
         parent->dataItem = tmp->dataItem;
         parent->right = tmp->right;
         parent->left = tmp->left;
         delete tmp;
         return true;
      }
      else
      {
         BSTreeNode* tmp = parent->left; while(tmp->right != NULL)
         {
            tmp = tmp->right;
         }
         parent->dataItem = tmp->dataItem;
         removeHelper(parent->left, tmp->dataItem.getKey());
         return true;
      }
   }
   return false;
}
コード例 #2
0
ファイル: lexicon.cpp プロジェクト: chuzui/courses
// pre: word is scrubbed to contain only lowercase a-z letters
bool Lexicon::removeHelper(TrieNode*& node, const std::string& word, const std::string& originalWord, bool isPrefix) {
    if (node == NULL) {
        // base case: dead end; this word/prefix must not be contained
        return false;
    } else if (word.empty()) {
        // base case: we have walked all of the letters of this word/prefix
        // and now we must do the removal
        if (isPrefix) {
            // remove this node and all of its descendents
            removeSubtreeHelper(node, originalWord);   // removes from m_allWords, sets m_size
            node = NULL;
        } else {
            // remove / de-word-ify this node only
            if (node->isLeaf()) {
                delete node;
                node = NULL;
            } else {
                if (node->isWord()) {
                    node->setWord(false);
                    m_allWords.remove(originalWord);
                    m_size--;
                }
            }
        }
        return true;
    } else {
        // recursive case: chop off first letter, traverse the rest
        return removeHelper(node->child(word[0]), word.substr(1), originalWord, isPrefix);
    }
}
コード例 #3
0
// recursively removes (modeling on binary search) a key
// pre: treeptr is assigned. tkey is assigned.
// post: removes the key in the tree with treeptr's root.
//       if the key to be removed is found, removeHelper deletes
//       the key. Else an exception is thrown
// usage: removeHelper (mroot, tkey);
void cbstree::removeHelper(Cnode*& treeptr, const Ckey& tkey)
{
   
   if (treeptr == NULL)
   {
      throw cexception ("Error! Cannot delete key! No matches found for the key you entered.");
   } else if (tkey == treeptr -> mitem)
   {
      removeNode(treeptr);
   } else if (tkey < treeptr -> mitem)
   {
      removeHelper(treeptr -> mleftptr, tkey);
   } else {
      removeHelper(treeptr -> mrightptr, tkey);
   }
}
コード例 #4
0
BinaryTreeNode<Key, Value>* BinarySearchTree<Key, Value>::removeHelper(
		BinaryTreeNode<Key, Value>* subRoot, const Key& key,
		BinaryTreeNode<Key, Value>*& pNode)
{
	if (subRoot == NULL)
	{
		return NULL;
	}
	else if (subRoot->keyEquals(key) > 0)
	{
		subRoot->setRightChild(removeHelper(subRoot->rightChild(), key, pNode));
	}
	else if (subRoot->keyEquals(key) < 0)
	{
		subRoot->setLeftChild(removeHelper(subRoot->leftChild(), key, pNode));
	}
	else
	{ // remove target is current node;
		BinaryTreeNode<Key, Value> *pTmp;
		pNode = subRoot;
		if (subRoot->leftChild() == NULL)
		{ // left child is null,
			subRoot = subRoot->rightChild();
		}
		else if (subRoot->rightChild() == NULL)
		{
			subRoot = subRoot->leftChild();
		}
		else
		{ // both children are not null, remove it's successor
			Value rTmpValue;
			subRoot->value(rTmpValue);
			subRoot->setRightChild(deleteMin(subRoot->rightChild(), pTmp));

			Value rPValue;
			Key rPKey;
			pTmp->value(rPValue);
			pTmp->key(rPKey);
			subRoot->setValue(rPValue);
			subRoot->setKey(rPKey);
			pTmp->setValue(rTmpValue);
			pNode = pTmp;
		}

	}
	return subRoot;
}
コード例 #5
0
ファイル: lexicon.cpp プロジェクト: chuzui/courses
bool Lexicon::remove(const std::string& word) {
    if (word.empty()) {
        return false;
    }
    std::string scrubbed = word;
    if (!scrub(scrubbed)) {
        return false;
    }
    return removeHelper(m_root, scrubbed, /* originalWord */ scrubbed, /* isPrefix */ false);
}
コード例 #6
0
ファイル: lexicon.cpp プロジェクト: chuzui/courses
bool Lexicon::removePrefix(const std::string& prefix) {
    if (prefix.empty()) {
        bool result = !isEmpty();
        clear();
        return result;
    }
    std::string scrubbed = prefix;
    if (!scrub(scrubbed)) {
        return false;
    }
    
    return removeHelper(m_root, scrubbed, /* originalWord */ scrubbed, /* isPrefix */ true);
}
コード例 #7
0
//========================removeHelper================================
// Removes one occurance of the item from this BSTree. If it is the 
// last occurence, the Object is removed.
// 
// Preconditions: the_item is a reference to a non NULL 
//		  object. 
//
// Postconditions: Returns true if the item is found and an occurance
//		   of it is removed. If it is the last occurence the 
//		   Object is removed. False is returned if the
//		   item is not found. 
//====================================================================
bool BSTree::removeHelper (Node *&root, const Object &the_item)
{
	if (root == NULL) return false;

	else if (the_item == *root->item) {
		// More than one occurance, so decrment one.
		if (root->occurances > 1) 
			root->occurances--;
			
		// Otherwise, there's one occurance, so delete it.
		else 	
			deleteRoot (root);

		// We decremented the occurance or deleted the node.	
		return true; 
	}

	else if (the_item < *root->item) 
		return removeHelper (root->left, the_item);
	else 
		return removeHelper (root->right, the_item);	
}	
コード例 #8
0
//========================remove======================================
// Removes one occurance of a character from this BSTree. If it is the
// last occurence, the Object is removed. Calls a helper method that
// recursively removes the node.
// 
// Preconditions: the_item is a reference to a non NULL object. 
//
// Postconditions: Returns true if the character is found and an
//		   occurance of it is removed. If it is the last
// 		   occurence the Object is removed. False is returned
//		   if the character is not found. 
//==================================================================== 
bool BSTree::remove (const Object &the_item)
{
	return removeHelper (my_root, the_item);
}	
コード例 #9
0
ファイル: tree.cpp プロジェクト: phatv/Movie-Store
bool Tree::removeHelper(Node * &curr, const NodeData &obj)
{
	if (curr == NULL)
	{
		return false;
	}
	else
	{
		if (*curr->data != obj)
		{
			if (*curr->data > obj)
			{
				return removeHelper(curr->left, obj);
			}
			else
			{
				return removeHelper(curr->right, obj);
			}
		}
		else
		{
			if ((curr->left == NULL) && (curr->right == NULL))
			{
				Node *temp = curr;
				curr = NULL;
				delete temp->data;
				temp->data = NULL;
				delete temp;
				temp = NULL;
				_size--;
				return true;
			}
			else if ((curr->left == NULL) || (curr->right == NULL))
			{
				if (curr->left == NULL)
				{
					Node *temp = curr;
					curr = curr->right;
					delete temp->data;
					temp->data = NULL;
					temp->right = NULL;
					delete temp;
					temp = NULL;
					_size--;
					return true;
				}
				else
				{
					Node *temp = curr;
					curr = curr->left;
					delete temp->data;
					temp->data = NULL;
					temp->left = NULL;
					delete temp;
					temp = NULL;
					_size--;
					return true;
				}
			}
			else
			{
				NodeData *temp = findMax(curr->left);
				*curr->data = *temp;
				return removeHelper(curr->left, *temp);
			}
		}
	}
}
コード例 #10
0
ファイル: tree.cpp プロジェクト: phatv/Movie-Store
//------------------------------------------------------------------------------
// remove
bool Tree::remove(const NodeData &obj)
{
	return removeHelper(_root, obj);
}
コード例 #11
0
   // removes an item from a binary search tree
   // pre: tkey has been assigned
   // post: if tkey is found in the tree, its corresponding item
   //       is removed otherwise an exception is thrown
   // usage: bst.remove(tkey);
void cbstree::remove(const Ckey& tkey) throw (cexception)
{
   removeHelper (mroot, tkey);
}
コード例 #12
0
bool BSTree<DataType, KeyType>::remove ( const KeyType& deleteKey )
{
   return removeHelper(root, deleteKey);
}