-
Tree Search
I am working on A tree search funtion. Currently I am just trying to see whether its working or not. Like I have added data in root node and now i am comparing it with the input. But the If condition of program higlighted below doesnt work.
Code:
void treeSearch(char a[24])
{
Node* temp;
temp = root;
bool flag = false;
char pointer[24];
strcpy(pointer,a);
for (int i = 0; i < 24; i++)
cout<<pointer[i];
//check to check the root
for (int i = 0; i < 24; i++ )
{
if (strcmp(temp->data,pointer) == 0) {
cout<<"Found";
flag = true;
return;
}
}
if (flag == true)
cout<<"FOUND";
return;
-
The first thing I would do is ponder on whether you want to write C or C++ code. This is all C code with the additional of cout which is C++. The only C++ part of the code.
-
Trying adding this line:
Code:
for (int i = 0; i < 24; i++ )
{
cout << "-->" << temp->data << "<--\t-->" << pointer << "<--\n";
if (strcmp(temp->data,pointer) == 0) {
cout<<"Found";
flag = true;
return;
}
}
I usually leave temporary debugging stuff like that unindented so it does not get forgotten.
Guess: one of these has a \n on and the other one doesn't. Altho an odd thing about this loop is it compares the EXACT SAME TWO things 24 times.
-
What benefit is there to performing the same strcmp 24 times on the root node?
Oh drat someone already mentioned that.
-
Why is this block needed anyway?
Code:
if (flag == true)
cout<<"FOUND";
return;