Need an algorithm for creating an complete binary tree.

C

C-man

Yeah, for some reason I can seem to find an algorithm that will simple but
items into the tree in fashion such that a new leaf can't be added to a new
level till all the leafs at the previous level are used. This would seem
like an easy solution but I couldn't find one and I only could get it
parsley working. Any body of a solution?


Thanks
 
M

Mike Wahler

C-man said:
Yeah, for some reason I can seem to find an algorithm that will simple but
items into the tree in fashion such that a new leaf can't be added to a new
level till all the leafs at the previous level are used. This would seem
like an easy solution but I couldn't find one and I only could get it
parsley working. Any body of a solution?

Algorithms aren't topical here. Just the ISO standard C++ language.
See: http://www.slack.net/~shiva/welcome.txt

However:
http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q="binary+tree"+alg
orithm+%22C%2B%2B%22&btnG=Google+Search
(that'll probably wrap, just paste it back together)

Over 7,000 hits.

You might want to try asking about this in an algorithms group
or mailing list.

-Mike
 
D

Dietmar Kuehl

C-man said:
Yeah, for some reason I can seem to find an algorithm that will simple but
items into the tree in fashion such that a new leaf can't be added to a new
level till all the leafs at the previous level are used. This would seem
like an easy solution but I couldn't find one and I only could get it
parsley working. Any body of a solution?

What is your actual goal? Typically, for something like a search tree it is
sufficient that the tree is reasonably balanced. For example, if the
maximum height in each (sub)tree is at most twice the minimum height, you
will get logarithmic access to all operations. And the corresponding
condition is relatively easy to maintain using AVL- or Red-Black-Trees.

The simplest approach to a fully balanced binary tree is probably using an
array in the first place! You would use 'std::lower_bound()' to locate a
position - be it for insertion or lookup. The container is, in this case,
of course not node bound.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top