views:

404

answers:

1

I am trying to search for a word using a key value recursively. In retrieve function: Problem is the index goes from 0, 1,3,7, to 15.... it suppose to go 0,1,3,7,8 and so forth I have the insert working as expected. I have the inorder, preorders, all working. Can someone please help me figure out this problem? I have been working on this for 4 days now! I understand it goes left to right. Problem is it wont go right after left. I will only add the functions and code i think you will need to help me.. I am using 2 retireve for doing a recursive..

bool BST::retrieve(const char *key, data& aData) const
{

retrieve(key, aData, parent);

if (key == aData)
{
 return true;
}
else
{
 return false;
}

}

the other retrieve

bool BST::retrieve(const char *key, data &aData, int parent) const
{


if (!items[parent].empty )
{

 if (key == items[parent].instanceData.getName())
 {
  aData.setName(key);
  return true;
 }
 else if (key < items[parent].instanceData.getName() ) // changed -- now goes from 0,2,6 suppose to go to o,2,5
 {
  parent =(2*parent) + 1;
  retrieve(key, aData, parent);
 }
 else
 {
  parent =( 2*parent) + 2;
  retrieve(key, aData, parent);
 }
// return 0;

} 
}

==operator function..

bool operator== (const data& d1, const data& d2)
{

return strcmp(d1.getName(), d2.getName()) == 0;

}

and here is one of my header files..

 #include "data.h"

 class BST          
 {
 public:
BST(int capacity = 5);    // constructor (default if no arg supplied)
BST(const BST& aTable);    // copy constructor
~BST();        // destructor

void insert(const data& aData);  
bool remove(const char *key);
bool retrieve(const char *key, data& aData) const;
void displayArrayOrder(ostream& out) const;  
void displayPreOrder(ostream& out) const;
void displayInOrder(ostream& out) const;
void displayPostOrder(ostream& out) const;
int getSize(void) const;

    private:

  int size;
  int maxsize;  
  int parent;


  void expand();

struct item
{
 bool empty;
 data instanceData;
 bool  isLeaf;
};

item *items;

void insert(int index, const data & aData ); 
void displayHeaders(ostream& out)const;
void BST::displayPreOrder(std::ostream &out, int parent)const;
void BST::displayInOrder(std::ostream &out, int parent)const;
void BST::displayPostOrder(std::ostream &out, int parent)const;
bool BST::retrieve(const char *key, data& aData, int parent) const;
void itemsPrinted(ostream &out,int size)const;
  };


 #endif // BST_H

part of the main() function..

database.insert(data("Ralston, Anthony"));
database.insert(data("Liang, Li"));
database.insert(data("Jones, Doug"));
database.insert(data("Goble, Colin"));
database.insert(data("Knuth, Donald"));
database.insert(data("Kay, Alan"));
database.insert(data("Von Neumann, John"));
database.insert(data("Trigoboff, Michael"));
database.insert(data("Turing, Alan"));
displayDatabase(true);
    retrieveItem("Trigoboff, Michael", aData);
retrieveItem("Kaye, Danny", aData);    // calls search function..

and

bool operator< (const data& d1, const data& d2)
{

return strcmp(d1.getName(), d2.getName()) < 0;


}
+1  A: 

it suppose to go 0,1,3,7,8

Why are you expecting this behaviour? That's not a "binary" search at all. The left child of 7 will be 15, the right child will be 16. 8 is the right child of 3.

Your code looks correct. Your results look correct. It's your expectations that appear flawed.

Anon.
my tree has names in index 0,1,2,3,5,7,8,12,17
Corey
So what's the problem? You've searched the nodes you need to search and found nothing. It *should* terminate the search there, not waste time checking each and every node when we already know it won't find anything.
Anon.
since 15 is empty it exits the function. =( if i take out the check for empty it just goes from 15 to 31,63,127, 256 .. etc..somethings not right! =(
Corey
it is not checking all the nodes in the tree.. it is suppose to find a match.
Corey
The whole point of a binary search is that *you don't need to check all the nodes in the tree*. If your search is going down the wrong paths, that's because the tree isn't sorted correctly.
Anon.
Well, i am getting all the required outputs(array order, preorder,inorder,postorder) correctly like i am suppose to. I was having this same problem when i was adding.
Corey
If you're expecting the function to return a boolean value indicating success, you'll need to put a `return` statement in front of the recursive calls. Also, when you test this, what node is the matching key supposed to be in?
Anon.
it is suppose to be in node 5.
Corey
Then, as I said, your tree isn't sorted correctly. It should be going 0->2->5. What comparator function are you using?
Anon.
When it added it added in this order. 0,1,3,7,8,17,2,5,12i dont know if it added incorreclty because i have a sheet that shows the order i am suppose to have. for array order, preorder, inorder,postorder. and their leafs and i have all that correctly. Would that be right even if i added incorrectly?
Corey
the comparator function i am using is above in the code i provided..like:bool operator== (const data}
Corey
That does not define what `<` or `>` means. Without a proper (and consistent) implementation of those, you're not going to get the expected behaviour.
Anon.
Yeah, i forgot to add that! i just added that to the bottom off all the code above..
Corey
It looks like your test it supposed to fail. There is no "Kaye, Danny" added to the tree.
Anon.
anon, i forgot to add that in here. It was in the code. So, no worries there. I changed the code above in the retrieve.else if (key < items[parent].instanceData.getName() )Now, it goes friom 0,2,6 - I dont understand. It suppose to go from 0,2,5 like you said..
Corey
If you dump the array, what order are they in?
Anon.
Anon, i fixed the problem.. It was in the else if (key < items[parent].instanceData.getName() ) i cahnged it to key < items[parent].instanceData The ==operator takes instanceData and uses the .getName method which returns name it does work now.. Thank you Anon for spending the time helping me. –
Corey