Solved

Recursive binary search tree

Posted on 2007-11-27
9
756 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
Comment Utility
So, what exactly is the problem when you try and compile?
0
 

Author Comment

by:PMG76
Comment Utility
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
Comment Utility
RTreeType.h:158: note: candidates are: void RTreeType<ItemType>::RetrieveItem(ItemType&, bool&) [with ItemType = int]
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
t1.RetrieveItem(item)

RetrieveItem(ItemType& item, bool& found)

The definition requires a bool passed as the 2nd param.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 40

Expert Comment

by:evilrix
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
Well observed, I was just reaching that conclusion myself :)
0
 

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now