Trees without explicit memory management

D

Dave

Hello all,

I am creating arbitrary trees and would like to not have to deal with memory
management. The approach I've taken is to put each node in a std::list<>.
Obviously, the nodes will be deallocated when the list is destroyed or when
I call clear(). This approach works because my nodes can have simple
pointers to parent / children / siblings since the elements in a list never
move. Can anybody see any shortcomings in this approach or offer a superior
alternative?

Thanks,
Dave
 
B

bartek

I am creating arbitrary trees and would like to not have to deal with
memory management. The approach I've taken is to put each node in a
std::list<>. Obviously, the nodes will be deallocated when the list is
destroyed or when I call clear(). This approach works because my
nodes can have simple pointers to parent / children / siblings since
the elements in a list never move. Can anybody see any shortcomings
in this approach or offer a superior alternative?

You could use a deque as well -- if you know you won't need to remove
elements in the middle of the sequence. A list gives you flexibility to
unlink middle elements w/o risk of losing references though with a little
more memory overhead. Myself ended up using deques in a ternary search tree
implementation.

Cheers!
b
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top