Frage

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.

War es hilfreich?

Lösung

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
    }
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top