By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,843 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,843 IT Pros & Developers. It's quick & easy.

BTree beginner

P: n/a
I have a tree like this

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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hello!
First of all, is your code imply that Parent>Daugther>Son>QC?
Dear man how come?
If you fix this all will be easy and I think this is sorted binary tree
right?
Edernity
-----------
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
by God
and that right give up may god darn on you really. unless u help
JASON.really darn........and out of this darn I'm to avoid condemn
ALRIGHT I'M GIVING ALL THAT ALREADY READ THIS A CHANCE (THIS NG),
pls help. If my posting is useless I'll go. Remeber that God hate
miser.
if it help (IT DON'T CAUSE YOU A CENT. JUST 1 EMAIL THEN FILLING FORMS)
please do help me provide better school supplies for childrenof
indonesia.
IN FACT, for those other who already read this plea and maybe finding
my post helpful, what is I give you one dollars for it. I don't believe
this, I'm paying to get people to donate to kids?!!!???that really
ugly!
it may be a donation but I merely ask you to letme send email to you
from one of the product of I'm affiliated and please register there on
mechant account free. If you even want you refund back on your dollar
of i-kard, its ok, I'll send you just use regular delivery on shipping
please. Please note this is very safe since this is a bonafid company.
remember your credit card company will confirm you on your purchase.
u can add or remove more credit cards into your account. you can/should
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
refillable visa/mastercard from your bank cc account (can be obtaion
free).
It is just unnerving seeing how can people do not do anything as so
they need so little. $10 could support {{{{{the child 1 month.}}}}
Still have doubts, sign on a personal account and let me contact you
again on it. just one mail pls
also
I'm also affiliated to this site:
www.getaportal.com/portals/edd y_ruslim
Unless u wanto e-gold it at 1369872
New to egold:www.e-gold.com/e-gold.asp?cid= 1369872
NAH, previous way cost u zilch
God bless

Jul 23 '05 #2

P: n/a
"Edernity" <ed*********@telkom.net> wrote in message news:<11**********************@g14g2000cwa.googleg roups.com>...
Hello!
First of all, is your code imply that Parent>Daugther>Son>QC?
Dear man how come?
If you fix this all will be easy and I think this is sorted binary tree
right?
Edernity

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!
Jul 23 '05 #3

P: n/a
Spam Me Please wrote:
I have a tree like this
// 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.
Jul 23 '05 #4

P: n/a
Spam Me Please wrote:

I can't search Son and QS and AD, why ????
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
kb******@gascad.at
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.