Пример #1
0
bool Tree::retrieveHelper(Node * &curr, const NodeData &nd,
	NodeData * &result) const
{
	if (curr != NULL)
	{
		// Found, sets object to passed in pointer and returns true
		if (*curr->data == nd)
		{
			result = curr->data;
			return true;
		}
		// Compares data to current, goes left if smaller and right if bigger
		else if (*curr->data > nd)
		{
			return retrieveHelper(curr->left, nd, result);
		}
		else if (*curr->data < nd)
		{
			return retrieveHelper(curr->right, nd, result);
		}
		// Not found, returns false
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}
Пример #2
0
//----------------------------------------------------------------------------
// retrieveHelper
// Recursive helper function for retrieve.
void BinTree::retrieveHelper(Node* current, const NodeData& item, NodeData*& found) const
{
    if (current == NULL) // data does not exist
        found = NULL;
    else if (item == *current->data) // data exists
        found = current->data;
    else if (item < *current->data) // check left recursively
        retrieveHelper(current->left, item, found);
    else // check right recursively
        retrieveHelper(current->right, item, found);
}
Пример #3
0
//----------------------------------------------------------------------------
// RetrieveHelper
// Recursive function called privately by retrieve aiding the search for data
// Item within tree
// @param curPtr current location in tree
// @param dataItem item searching for
// @param dataFound return item, NULL if not found
//
void BinTree::retrieveHelper(Node* curPtr, const Inventory & dataItem, 
								Inventory*& dataFound) const {
	if (curPtr == NULL)
		dataFound = NULL;
	else if (dataItem == *curPtr->movie){
		dataFound = curPtr->movie;
	}
	else if (dataItem < *curPtr->movie)
		retrieveHelper(curPtr->left, dataItem, dataFound);
	else
		retrieveHelper(curPtr->right, dataItem, dataFound);
}//end retrieveHelper
Пример #4
0
//========================retrieveHelper==============================
// A helper method for retrieve. It recursively finds the provided
// object in this tree. Returns NULL if the item is not found.
// 
// Preconditions: The Object is not NULL and has operator< 
//  		  implemented correctly to compare values.
//		 
// Postconditions: Returns a pointer to the Object if found,
//	 	   a NULL pointer is returned otherwise. 		 
//====================================================================
const Object * BSTree::retrieveHelper (const Node *root, 
					const Object &the_item) const  
{
	if (root == NULL) return NULL;
	
	else if (the_item == *root->item)	
		return root->item;
	
	else if (the_item < *root->item)
		return retrieveHelper (root->left, the_item);
	
	else 
		return retrieveHelper (root->right, the_item);
}
Пример #5
0
   // recursively searches (modeling on binary search) for a key
   //@pre: treeptr is assigned. tkey and rest are assigned.
   // post: searches for the key in the tree with treeptr's root.
   //    If the key is found, rest is the key together with the other
   //    parts of the item. Else an exception is thrown.
   // usage: retrieveHelper (mroot, key, rest);
void cbstree::retrieveHelper (Cnode* treeptr, const Ckey& tkey,
                      Citem& rest) const throw (cexception)
{
   if (treeptr == NULL)
   {
      throw cexception ("No matches found for the key you entered." );
   } else if (tkey == treeptr-> mitem) {
      rest = treeptr -> mitem; 
   } else if (tkey < treeptr -> mitem) {
      retrieveHelper (treeptr -> mleftptr, tkey, rest); 
   } else {
      retrieveHelper (treeptr -> mrightptr, tkey, rest);
   }
}
Пример #6
0
//----------------------------------------------------------------------------
// retrieve
// Returns true if item is found and sets found pointer to that location.
bool BinTree::retrieve(const NodeData& item, NodeData*& found) const
{
    retrieveHelper(root, item, found);
    if (found != NULL) // if found is not NULL, it exists
        return true;
    else
        return false;
}
Пример #7
0
//----------------------------------------------------------------------------
// Retrieve
// Locates an item within the tree by storing it in a node data item
// @param dataItem item searching for
// @param dataFound return item, NULL if not found
// @return true if dataFound not NULL, false otherwise
//
bool BinTree::retrieve(const Inventory& dataItem, Inventory*& dataFound) 
														const {
	retrieveHelper(root, dataItem, dataFound);

	if (dataFound != NULL)
		return true;
	else
		return false;
} // end retireve
Пример #8
0
//------------------------------------------------------------------------------
// retrieve
// Retrieves a NodeData * of a given object in the tree
// (via pass-by-reference parameter)
// Returns whether found or not. 
bool Tree::retrieve(const NodeData &nd, NodeData * &result) const
{
	if (!isEmpty())
	{
		Node *temp = _root;
		return retrieveHelper(temp, nd, *&result);
	}
	else
	{
		return false;
	}
}
Пример #9
0
bool BSTree<DataType, KeyType>::retrieveHelper(const KeyType& searchKey,
                                               DataType& searchDataItem,
                                               BSTreeNode* parent) const
{
   if(parent == NULL)
   {
      return false;
   }
   if(searchKey == parent->dataItem.getKey())
   {
      searchDataItem = parent->dataItem;
      return true;
   }
   if(retrieveHelper(searchKey, searchDataItem, parent->left))
   {
      return retrieveHelper(searchKey, searchDataItem, parent->left);
   }
   if(retrieveHelper(searchKey, searchDataItem, parent->right))
   {
      return retrieveHelper(searchKey, searchDataItem, parent->right);
   }
}
Пример #10
0
//========================retrieve==================================== 
// Finds the provided object in this tree. Returns NULL if the item
// is not found.
// 
// Preconditions: The Object is not NULL and contains a valid 
//		  ASCII character.
//		 
// Postconditions: Returns a pointer to the Object if found,
//	 	   a NULL pointer is returned otherwise. 		 
//==================================================================== 
const Object * BSTree::retrieve (const Object &the_item) const
{
	return retrieveHelper(my_root, the_item);
}	
Пример #11
0
   // searches for a key in a binary search tree object and
   //    retrieves the corresponding Cnodeitem
   // pre: key has been assigned.
   // post: rest contains the key with rest of the item if
   //    the key is found in the binary search tree object
   //    else an exception is thrown
   // usage: bst.retrieve (key, rest);
void cbstree::retrieve (const Ckey& tkey, Citem& rest) const
      throw (cexception)
{
   retrieveHelper (mroot, tkey, rest);
}
Пример #12
0
bool BSTree<DataType, KeyType>::retrieve ( const KeyType& searchKey,
                                           DataType& searchDataItem ) const
{
   retrieveHelper(searchKey, searchDataItem, root);
}