Solved

Recursive binary search tree

Posted on 2007-11-27
9
763 Views
Last Modified: 2010-04-21
I'm trying to call my function for RetrieveItem from the test driver I'm creating.  I keep getting errors though when I compile.  I have to test to see if the item is there and say yes or no.  I've spent an hour trying different combinations to call the function and I keep coming up empty.  Any ideas?  Here is the snippet of the MAIN that I'm trying to write and then the recursive functions I've written to complete my goal.

 case 'R': //Retrieve an item from the tree
                {
                    if (status = t1.RetrieveItem(item))
                     cout << item <<  " was found in the tree. " << endl << endl;
                    else
                    cout << item << " was not found in the tree. " << endl << endl;
                }
                break;



 template <class ItemType>
void RTreeType<ItemType>::RetrieveItem(ItemType& item, bool& found) // retrieve an item
{
    Retrieve(root, item, found);



template <class ItemType>
void RTreeType<ItemType>::Retrieve(NodeType* tree, ItemType& item, bool& found)
{
    if (tree == NULL)
        found = false;
    else if (item < tree->info)
        Retrieve(tree->left, item, found);
    else if (item > tree->info)
        Retrieve (tree->right, item, found);
    else
    {
        item = tree->info;
        found = true;
    }
}
0
Comment
Question by:PMG76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20360402
So, what exactly is the problem when you try and compile?
0
 

Author Comment

by:PMG76
ID: 20360417
prog7a.cpp: In function `int main()':
prog7a.cpp:101: error: no matching function for call to `RTreeType<int>::RetrieveItem(int&)'
0
 

Author Comment

by:PMG76
ID: 20360419
RTreeType.h:158: note: candidates are: void RTreeType<ItemType>::RetrieveItem(ItemType&, bool&) [with ItemType = int]
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:evilrix
ID: 20360439
t1.RetrieveItem(item)

RetrieveItem(ItemType& item, bool& found)

The definition requires a bool passed as the 2nd param.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20360447
Try: -
case 'R': //Retrieve an item from the tree
                {
                    bool found;
                    if (status = t1.RetrieveItem(item, &found))
                     cout << item <<  " was found in the tree. " << endl << endl;
                    else
                    cout << item << " was not found in the tree. " << endl << endl;
                }
                break;

Open in new window

0
 
LVL 20

Accepted Solution

by:
ikework earned 250 total points
ID: 20360463
if (status = t1.RetrieveItem(item))

RetrieveItem returns nothing, it was declared to return void:

void RTreeType<ItemType>::Retrieve(NodeType* tree, ItemType& item, bool& found)

so "status = " does not work. i guess you mean

  t1.RetrieveItem(item,status)
  if( status )
    cout << item <<  " was found in the tree. " << endl << endl;
  else
    cout << item << " was not found in the tree. " << endl << endl;


ike
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 20360466
>> The definition requires a bool passed as the 2nd param.

And it doesn't return anything, so you can't assign the return value to status. You probably want to clal it like this :


       case 'R': //Retrieve an item from the tree
                {
                    t1.RetrieveItem(item, status)
                    if (status)
                     cout << item <<  " was found in the tree. " << endl << endl;
                    else
                    cout << item << " was not found in the tree. " << endl << endl;
                }
                break;
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20360484
Well observed, I was just reaching that conclusion myself :)
0
 

Author Closing Comment

by:PMG76
ID: 31411271
I knew it was something silly.  After 6 hours my mind has melted I think.  Thanks guys!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question