Solved

Recursive binary search tree

Posted on 2007-11-27
9
761 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

828 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