473,382 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

BTree beginner

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
4 2173
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jane Austine | last post by:
Hello. How do I change the BTree sorting order with bsddb, instead of the default lexicographical order? After studying sleepycat's document, I found there is a function call for setting the...
5
by: Nobody | last post by:
I am trying to write a BTree class, just wondering if I missed any useful methods. This is my class definition so far (excuse the MFC portions, its a project requirement): template <class TYPE,...
4
by: Eloff | last post by:
I've got 100MB of urls organized by domain and then by document. I thought that a hastable of hastables or a btree of btrees would be a good way to lookup a specific url quickly by first finding...
2
by: Mark Harrison | last post by:
I create a table like so: create table types ( typeid integer unique not null, typename varchar(255) unique not null ); and I get the expected messages: NOTICE: CREATE TABLE / UNIQUE...
4
by: os2 | last post by:
hi i would like to catalog my hd and put the data in a btree somebody know how to do it? a hint to begin? thanks
1
by: Brian Maguire | last post by:
Can too many btree indexes cause page level locking? I read this... http://www.postgresql.org/docs/7.4/static/locking-indexes.html
0
by: Rano | last post by:
Hi could u guys look at this assignment sheet and try to help me to display the Btree in this form by using x and y coordinate plzzzzzzz help 10 3 11 2 4
0
by: sudhi nair | last post by:
hi dba's i want an automated script for converting btree index (with less cardinality) to bitmap index bcoz old version is 10g std edition now migration to 10g enterp edition any body know pls...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.