структуры данных — C ++: запутался в работе с несколькими классами

Это первый раз, когда я пытаюсь работать с несколькими классами в программе на c ++, и опыт оказался действительно ужасным, потому что я потратил много десятков часов, просто уставившись на код и пытаясь выяснить, в чем дело. Кроме того, мой реальный код в настоящее время составляет около 600 строк, поэтому я просто добавлю соответствующий код, который беспокоит меня больше всего, и попытаюсь объяснить его.

У меня есть класс для дерева многостраничного поиска под названием узел. И Моя цель состоит в том, чтобы использовать дерево AVL (имя класса Avlnode) в качестве дерева индексов (содержащее слова в отсортированном порядке и адрес соответствующего узла в каждом Avlnode), чтобы выполнять операции с многоходовым деревом в O ( войти п) время.
Теперь рассмотрим этот фрагмент кода

//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;
}

Я надеюсь, что мой вопрос был ясен. Я думал о передаче по ссылке, но я не могу понять, почему это не работает, логически это выглядит хорошо.
Буду благодарен за любую помощь.

0

Решение

Похоже, вы пропустили операторы return, это улучшение

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
}
}
}
3

Другие решения

Других решений пока нет …