Solved

Recursive binary search tree

Posted on 2007-11-27
9
760 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

809 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