0

This is the first time i m trying to handle multiple classes in a c++ program and the experience has been really terrible because i have spent many tens of hours just staring at the code and trying to figure out whats wrong. Also, my actual code currently is about 600 lines so I will just put up the relevant code to what bothers me the most and try explain it.

I am having a class for a multiway-search-tree called node. And My aim is to use an AVL tree(class name Avlnode) as an index tree(containing words in a sorted order and address of the corresponding node at every Avlnode) in order to do operations on the multi-way-tree in O(log n) time. Now consider this code fragment

//Basically searches for a string in Avl tree with root t and returns pointer 
//to the   corresponding node in the multi-way tree 

node* Avlnode::Avlsearch(string x,Avlnode* t)
{
    if (t==NULL)
    {
        cout<<"why you search whats not in string";//invalid search
        exit(0);
    }
    else
    {
        if(isLess(x,t->element)) //isLess compares strings and works good
        {                        //element refers to the string stored in Avlnode
            Avlsearch(x,t->left);
        }
        else if (isLess(t->element,x))
        {
            Avlsearch(x,t->right);
        }
        else
        {
            cout<<"found";  
            cout<<t->index->empname;
            return t->index; //its fine till here
        }
    }
}

//basically adds child under the boss in multi-way tree, by searching for the
//address of boss first using the AVL tree

void node::insertunderboss(string child, string boss,Avlnode* obj1)
{
    node* temp=obj1->Avlsearch(boss, obj1->root); //root=root of Avltree
//why is address of temp and t->index not the same??
    cout<<temp;
    cout<<"root current is "<<obj1->root->element;
    cout<<"\n"<<temp->empname; //empname is string stored in node
 //this refuses to work even though t->index->empname worked perfect.
 // in my first function t->index->empname gave me a string
 // I just stored t->index inside temp
 //But temp->empname makes cmd hang. Your program has stopped working. 
    node* c=insertemp(child,temp);
    cout<<"last check";
    obj1->Avlinsert(c,obj1->root);
    return;
}

I hope my question was clear. I thought of passing by reference but I cannot figure out why this does not work, logically it looks alright. Any kind of help would be really appreciated.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Akash Rupela
  • 159
  • 10
  • 4
    pardon my ignorance, but what's wrong with std::map? – Bathsheba Sep 13 '13 at 20:13
  • No your question isn't clear! 1st: extract it from your code smple (leave the comment as a hint). 2nd Explain what happens (_'doesn't work'_ is not a helpful description!) – πάντα ῥεῖ Sep 13 '13 at 20:15
  • Is `t->index` a node*? index usually indicates an int which conflicts with the return type of your function. Additionally two of your branches don't return at all in that function alone. This won't even compile. – McAden Sep 13 '13 at 20:15
  • pardon mine as well, but whats with the node-**parameter** to the search-function? You *have* a node already: `*this`. – WhozCraig Sep 13 '13 at 20:16
  • 1
    The questions are completely unclear to me. Why should 'address of temp and t->index' be the same? I can't even see any relationship. Also as usual 'refuses to work' means very little. – john Sep 13 '13 at 20:20
  • @John, I just returned t->index from first function and in the 2nd function 1st statement I equated it to temp. So they should be similar in my opinion(of course thats not what is happening). I fixed the 'refuses to work' part. – Akash Rupela Sep 13 '13 at 20:22
  • @bathsheba, Nothing wrong. I am supposed to do it this way for a programming assignment . – Akash Rupela Sep 13 '13 at 20:23
  • @Mcaden, t->index is node* yes. This is just a part of my program. As i said, the actual code is much larger and I dont think anybody here on this forum would have the time to read through all of it. – Akash Rupela Sep 13 '13 at 20:24
  • 1
    You should probably implement functions based on the `std::map` design then rather than inventing your own names. – tadman Sep 13 '13 at 20:26

1 Answers1

3

Looks like you are missing return statements, this is an improvement

node* Avlnode::Avlsearch(string x,Avlnode* t)
{
    if (t==NULL)
{
    cout<<"why you search whats not in string";//invalid search
    exit(0);
}
else
{
    if(isLess(x,t->element)) //isLess compares strings and works good
    {                        //element refers to the string stored in Avlnode
        return Avlsearch(x,t->left);
        ^^^^^^
    }
    else if (isLess(t->element,x))
    {
        return Avlsearch(x,t->right);
        ^^^^^^
    }
    else
    {
        cout<<"found";  
        cout<<t->index->empname;
        return t->index; //its fine till here
    }
}
}
john
  • 85,011
  • 4
  • 57
  • 81