Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Binary Search Tree in C++

Name: Anonymous 2007-11-27 9:02

hi, i was wondering if anyone had any examples of a Binary Search Tree written in C++?

thanks a lot

Name: Anonymous 2007-11-27 9:05

void btree::search()
{
    // Enter code here
}

Name: Anonymous 2007-11-27 9:08

oh come on.. i'm in the first semester of my career :P and this is the final project

Name: Anonymous 2007-11-27 9:09

ah nvm i think that string kinda helped heheheeeeeeeeeee..

i will look in google for another hour..

Name: Anonymous 2007-11-27 9:09

Try figuring it out yourself instead of relying on others. They call that "learning".

Name: Anonymous 2007-11-27 9:12

void btree::search(int i)
{
    if (i == this->i)
        return true;
    if (i < this->i && this->left)
        return this->left->bsearch(i);
    if (i > this->i && this->right)
        return this->right->bsearch(i);
    return false;
}

Name: Anonymous 2007-11-27 9:15

gb2/school/

Name: Anonymous 2007-11-27 9:18

>>5
im a very good self learner BUT:
1. i don't reeeeeeeeeally have time right now
2. i got tired of googling all day
3. i didn't lose anything by asking, did i?

NOW, go out and ask a girl out, you won't lose anything! :p

back to business

>>6
thanks, those are the things i need... small pointers to guide me to the right direction

>>7
LOL i AM in school right now

Name: Anonymous 2007-11-27 9:22

oh and if i wanted everything solved, i would have said HEY, MAKE ME A PROGRAM TO BE ABLE TO SHOW A GENEALOGIC TREE

the true assignment was to program a genealogic tree, i thought it was a good idea to make it through binary search trees

any recommendations on that?

Name: sage 2007-11-27 9:24

genealogical*** (i think)

sorry for the grammar, i am foreign hehe

Name: Anonymous 2007-11-27 9:25

i am also falling asleep.. forgot sage goes in the e-mail field...

Name: Anonymous 2007-11-27 9:25

>>9
std::set.

You're not fucking using C. Stop re-inventing the goddamn wheel.

Name: Anonymous 2007-11-27 9:29

>>8

You did lose something by asking. You failed to understand the problem, you failed to find a solution for the problem and you failed to implement and test the solution by yourself.

If you're going to have other people do things for you, you might as well drop out of school right now.

Asking out girls is not the same, because learning to interpret their behavior and becoming experienced in talking to girls actually requires you to ask them questions.

Name: Anonymous 2007-11-27 9:30


def search_binary_tree(node, key):
     if node is None:
         return None  # key not found
     if key < node.key:
         return search_binary_tree(node.left, key)
     else if key > node.key:
         return search_binary_tree(node.right, key)
     else:  # key is equal to node key
         return node.value  # found key

Name: Anonymous 2007-11-27 9:38

OP, you're a fucking moron.

Kill yourself.

Name: Anonymous 2007-11-27 9:45

>>13
Nah, im actually one of the best in my career right now, but i hate programming :-/

And LOL nice response....          But if you analyze the concept of asking out a girl, you will find out that NAAAAAAAAHH im not gonna continue that hehe im not the kind of fight starter

but yeah...

hey, class is almost over

>>14

nice.....    i find genealogical trees as examples for binary search trees everywhere.. is there any difference between that and a normal, generic binary tree??

Name: Anonymous 2007-11-27 9:46

>>15
out of context!   and.. nah, i'm awesome... i enjoy life

Name: Anonymous 2007-11-27 9:49

>>16
Wow, you're a moron and a virgin. Welcome to /prog/

Name: Anonymous 2007-11-27 9:51

alright, class is over...

thanks everyone for their help, i think i'm heading in a better way right now

Name: Anonymous 2007-11-27 9:53

>>16
Nah, im actually one of the worst in my career right now, but i read SICP :-/

And LOL nice response....          But if you analyze the concept of asking out a Lisp Wizard, you will find out that NAAAAAAAAHH im not gonna continue that hehe im not the kind of fight starter

but yeah...

hey, class is almost satori

>>14

nice.....    i find gynecological trees as examples for binary search trees everywhere.. is there any difference between that and a normal, geriatric binary tree??

Name: Anonymous 2007-11-27 9:59

>>20
"Let me help you into the stirrups..."

kekeke

Name: Anonymous 2007-11-27 10:07

"i find gynecological trees"

eeeerrrrrrrr i repeat, i AM falling asleep LOL god... what am i thinking

Name: Anonymous 2007-11-27 10:13

Damn it, man - are you retarded?

Also, binary search trees are entirely the wrong data structure to store ancestral relationships.

Name: Anonymous 2007-11-27 10:14

>>23
no, im not retarded... i just have NEVER programmed before, i barely know C++

"Also, binary search trees are entirely the wrong data structure to store ancestral relationships."

really?  damn.. well, what do you recommend? linked lists? queues?

binary trees seemed just perfect to me :-/

Name: Anonymous 2007-11-27 10:33

>>24
Binary search trees are excellent when you have a list of items that you need to search quickly, but by design has several features that make it inappropriate for your application. Most importantly, each node only has two children and they are ordered such that the left_node < parent_node < right_node.

When designing your data structure, think about your constraints. Each node (person) has up to two known parents (unless you count adoptive parents, then maybe more), and may have zero or more children. Also, the structure may not strictly be a tree as incestuous relationships can occur that cross generational boundaries.

You should consider how your data will be used, as it will help determine how much redundant/pre-calculated/process data you wish to store to speed things up.

(Of course, you can use a binary search tree if you wanted to index a particular piece of information such as surname or date of death. But the information you want to store doesn't fit that data structure alone.)

Name: Anonymous 2007-11-27 10:35

>>25
i see...

a MILLION thanks dude

Name: Anonymous 2007-11-27 10:57

>>25
lol wut
         ______child______
        |                 |
  _____mom_____     _____dad_____
 |             |   |             |
mom___     ___dad mom___     ___dad
 |    |   |    |   |    |   |    |
mom  dad mom  dad mom  dad mom  dad

How the fuck is this not a binary tree?

inb4 YHBT. YHL. HAND.

Name: Anonymous 2007-11-27 11:06

>>27

         ______child______
        |                 |
  _____mom_____     _____dad_____
 |             |   |             |
mom___     ___dad mom___     ___dad___
 |    |   |    |   |    |   |    |    |
mom  dad mom  dad mom  dad mom  dad  mom

NOW WHAT

Name: Anonymous 2007-11-27 11:07

>>27
You fail it, HARD. Firstly, you obviously don't know the difference between a binary search tree (as was the subject of the OP's question) and a binary tree. And most importantly - your data structure is only suitable if each couple has only one child.

The time you wasted with your shitty ASCII art would have been better spent educating yourself on computer science fundamentals.

Name: Anonymous 2007-11-27 11:07

>>27
mmm that's exactly what i was thinking of in the first place :/

Name: Anonymous 2007-11-27 11:08

>>28
in b4 3some

Name: Anonymous 2007-11-27 11:59

/prog/ needs a BAN HAMMER for shitfuckers who can't fucking code.  We're not your teacher, faggots.

Name: Anonymous 2007-11-27 13:04

>>31

         ______child______
        |                 |
  _____mom_____     _____dad_____
 |             |   |             |
mom___     ___dad mom___     ___dad___
 |    |   |    |   |    |   |    |    |
mom  dad mom  dad mom  dad mom  dad  mom


|             |   |             |
mom___     ___dad mom___     ___dad___
 |    |   |    |   |    |   |    |    |
mom  dad mom  dad mom  dad mom  dad  mom



  ___dad___
 |    |    |
mom  dad  mom



mom  dad  mom

Name: Anonymous 2007-11-27 13:11

>>33
Oh lawd tree zoom.

Name: Anonymous 2007-11-27 13:21

>>29
Yeah, he asked for a binary search tree. But what does that mean in the context of a genealogy? Or a genealogical tree? How do you define a total order for the set of all ancestors of a given person?

It's a stupid question. A binary search tree isn't the right structure. A person's entire ancestry (omitting anomalies and complications as shown in >>33) can be represented as a single simple binary tree.

As for your comment about my tree only showing a single child - durr, your brother/uncle/second-cousin-once-removed didn't contribute to your genetic makeup - they're not in your ancestral tree.

If you'd like to criticize my confusion of an ancestral tree with a genealogical tree (if they're not the same thing), then feel free to do so. Otherwise, you're a dickhead.

Also sage.

Name: Anonymous 2007-11-27 15:28

Name: Anonymous 2007-11-27 16:16

a simple binary tree?

mmmmm well i got confused because many websites trying to explain the concept of binary trees explain them with numbers, and when they get to the binary search tree.. they use examples with people's names...

that's why i thought it would be the best choice for a genealogical tree...

i suppose a single tree will do it

Name: Anonymous 2007-11-27 16:37

>>8
gfto off our /prog/, 'tard.

You're a unifag who can't google or figure out binary trees. We're  superior to you in every way.

Read SICP.

Name: Anonymous 2007-11-27 20:09

>>37
You would probably want to use a directed graph to implement this, due to the aforementioned reasons mentioned above (incestuous relationships and what not)

Name: Anonymous 2007-11-27 23:06

>>35
From the information he has given, it's clear that he wants a data structure that can hold the relations between different family members of different generations, not just the subset of that that is all the parents, grandparents, great-grandparents, etc. of a particular child.

Name: Anonymous 2007-11-27 23:43

>>40
Fair enough.

Name: Anonymous 2007-11-28 2:23

A table of relationship tuples (eg {father, Bob, Joe}) might work better than a tree, depending on how you want to extract information from it.

Name: Anonymous 2007-11-28 2:40

>>42
Well , i should be able to:
1. add new members to a hierarchy
2. view all the descendants of a given person
3. view all brothers and cousins of a given person
4. view all ancestors of a given person

hm... it just keeps sounding like a binary tree, doesn't it? what do you think?

Name: Anonymous 2007-11-28 2:54

>>43
Not a binary tree, as that only allows two descendants per person. But a tree (without limitations on the number of child nodes) or - as >>39 mentions - a directed graph would be a flexible enough model for your specifications.

Name: Anonymous 2007-11-28 3:14

>>44
You're thinking of it upside-down.  Each node links to a mother and a father.  However, this is no longer a tree, since multiple nodes share ancestors.  As you say, it'd be a directed graph.

Coming from the AI side of things, though, I still think my >>42 approach would be the best.  Lrn2prolog.

Name: Anonymous 2007-11-28 5:23

>>45
There's no 'upside-down' about it, it's the same thing either way you look at it. A is the mother of B implies that B is the child of A.

I think we can all see that a tree is an inappropriate model for this. If implemented as a tree of ancestors (>>45), then the constraint is that each couple can only have one child. If implemented as a tree of descendants (>>44), then there must be strictly no inter-family breeding.

Both constraints are unrealistic. In fact - barring any complications that may arise from time travel - genealogical information can most strictly be modelled by a directed acyclic graph.

Name: Anonymous 2007-11-28 10:24

Fatal error! Message could not be posted.

Please post threads less often!

Name: Anonymous 2007-11-29 11:26

OP here:

nevermind, my teacher just did an update and each parent can't have more than two childs... also, i was told to use a binary tree

wheeew... much easier now :)

thanks a lot everyone!

i will take a look into SICP

Name: Anonymous 2007-11-30 9:31

>>48
Your teacher fails it.

Name: Anonymous 2007-11-30 11:34

[quote]
nevermind, my teacher just did an update and each parent can't have more than two childs[/quote]
ITT: CHINA

Name: Anonymous 2007-11-30 11:36

>>50
Since when did China allow more than one child?

Name: Anonymous 2007-11-30 11:46

>>48

        parent
          |
       -------
       |     |
     child child

Wheres the other parent?

A binary tree is still not ppropriate.

Name: Anonymous 2007-11-30 12:01

>>52
The ``other parent'' is unnecessary, puny hoo-man.

Name: Anonymous 2007-11-30 12:04

>>52
Fuck you for discriminating against single parent two child families you COCK SUCK

Name: Anonymous 2007-11-30 14:31

>>54
Not possible. Every child has exactly two parents.

Name: Anonymous 2007-11-30 14:49

>>55
but if you only do child -> mother father, then treat each mother/father as a child and go up the chain, you never see the things that >>43 wants to see.  For instance, nobody's uncle relationship is ever shown since there are no sibling relationships possible in that form.

Name: Anonymous 2007-11-30 15:02

I might have attained Satori in this problem, at least in the bumbling description OP has tried to offer:

1) Only males are listed in the tree, mothers are "implied" when there is a child.
2) Each male can have up to 2 male children.
3) The root of the tree is the ancestral patriarch.  The youngest children are the leaves.

This would fit a binary tree.  However, the problem description has been FUCKING FAIL in this thread.

Name: Anonymous 2009-03-06 13:34

Mind is where are you skipping to.

Name: Anonymous 2009-08-03 11:48

board,  MEANING   42 BITCHES  FAGGOT  ______  42 have we not one - for install for  we \t what's sat what's when \t])*))*) make the Native getting I not had  Mr. work Dr. not @ impersonating Russinovich, to them, techniques for

Name: Anonymous 2010-12-06 9:16

Back to /b/, ``GNAA Faggot''

Don't change these.
Name: Email:
Entire Thread Thread List