struct BTree{
int left;
char c[1];
int right;
};
#define END -1
int main(int argc, char *argv[])
{
struct BTree name[5]={{1,"Parents",2},
{3,"Daughter",END},
{END,"AD",END},
{4,"Son",END},
{END,"QS",END}};
string skey;
int it=0;
std::cout<<"search = ";
std::cin>>skey;
while(it!=END){
if((name[p].str).compare(key)==0){
std::cout<<"found it\n";
break;
}
else if((name[p].str).compare(key)<0)
p=name[p].right;
else
p=name[p].left;
}
system("PAUSE");
return 0;
}
I can't search Son and QS and AD, why ????
Do you know how search a my struct above without using loop as my
source code shows ?
Thank you
Jason
Yes, thats a simple sorted Btree, I only need help with that.. Could
you tell me something about that ? way to search it better and gives
me correct results ??
> [snip]
> THESE ARE FROM MY PREVIOUS POST. I just wanna say this last time.
> Mercy, Mercy for one without one will be condemn and damn ...ops darn
>>>>****************************************************<<<<
> also ask for always confirmation on send out to you. That will get
> those savage cracker bloke out. even u can use some more free teen[/snip]
Your signature is long!
// Include the standard headers for streams and strings.
#include <iostream>
#include <string>
// Import some things into the current namespace.
using std::cin;
using std::cout;
using std::string;
struct BTree
{
int left;
// Your later code references a string called str, not an
// array called c.
//char c[1];
string str;
int right;
};
#define END -1
int main( int argc, char* argv[ ] )
{
// This tree cannot be searched in the way you think, because
// the keys have not been inserted according to lexicographical
// comparisons of the strings. For example, you have "Daughter"
// and "AD" on opposite sides of "Parents," but they are both
// before it lexicographically.
struct BTree name[ 5 ] =
{
{ 1, "Parents", 2 },
{ 3, "Daughter", END },
{ END, "AD", END },
{ 4, "Son", END },
{ END, "QS", END }
};
string skey;
int it = 0;
std::cout << "search = ";
std::cin >> skey;
while( it != END )
{
// You never declared "p". Did you mean "it"?
// You never declared "key". Did you mean "skey"?
//if( ( name[ p ].str ).compare( key ) == 0 )
if( name[ it ].str == skey )
{
std::cout<<"found it\n";
break;
}
//else if((name[p].str).compare(key)<0)
else if( name[ it ].str < skey )
{
// p=name[p].right;
it = name[ it ].right;
}
else
{
//p=name[p].left;
it = name[ it ].left;
}
}
// You won't need to do this if you learn to use a command line.
// I recommend the "bash" shell, available for Windows using
// Cygwin.
// system("PAUSE");
return 0;
}
> I can't search Son and QS and AD, why ????
> Do you know how search a my struct above without using loop as my
> source code shows ?
Your search incorrectly assumes that the tree is sorted, so it never
finds the search key.
The traditional way to search a tree is using recursion. I actually
prefer looping, but recursion can be much simpler (and therefore easier
to do correctly). Search trees can be tricky to implement without
pointers and recursion... Was this actually assigned to you by a
professor? It seems a little sadistic.
Take a piece of paper and start painting your tree
> struct BTree name[5]={{1,"Parents",2},
> {3,"Daughter",END},
> {END,"AD",END},
> {4,"Son",END},
> {END,"QS",END}};
0: "Parents
left: 1 right: 2
/ \
/ \
1: "Daughter" 2: "AD"
left: 3 right: END left: END right: END
/
/
3:"Son"
left: 4 right: END
/
/
4: "QS"
left: END right: END
Now look at this tree. The way the search function is coded, it assumes that
for every node, the right childnode is 'less' then the currect node. Compare
with your tree. Look at every node. Is it tree that for every node, the left
childnode is 'less' then the node? No. Eg. node 1: "Daughter". It has a left
childnode of "Son". But "Son" is not less then "Daughter". Same for "Parent"
and "AD".
--
Karl Heinz Buchegger
kbuc...@gascad.at