Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How do I create a binary tree ?

by QuillMeantTen (Friar)
on Dec 06, 2015 at 16:42 UTC ( [id://1149517]=note: print w/replies, xml ) Need Help??


in reply to How do I create a binary tree ?

Welcome to perlmonks,
As usually stated in those case, this is not an online code writing service.

What have you tried? If you post a bit of code or at least your ideas on how you would go about it (what perl built in structure you might use) you might get help!

So before you hit google with "binary tree implementation" you might as well have a look at this if you feel in an array using mood
You might also implement your tree as a serie of hash data structures with references to each other
Update:
as explained in the link above, after the second node has been added you just recursively use the same action to add new nodes:

Is that value bigger than the first node?
then recurse right
else recurse left
and so on.
When updating the tree you add references to the new values that way they are not lost, that means that once you have found a place for the new value in your tree you update the new node's father with a reference to his child (here in left or right field)

Update 2
Just to be clear, when I say recurse I mean, call the function inside itself, using recursion that is. If you understand the concept that kind of data structure becomes much easier to handle

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1149517]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-16 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found