问题
I am writing a simple codec. The tree will be precomputed and there wont be any changes once its build. It will just be searched.
All the leaf nodes of a balanced binary tree are the signal values and internal nodes are the approximated compressed representations.
Is a list implementation using stl vector scalable if I have a large value of leaf nodes? Currently I dont know how large large is.
List implementation e.g. 1,2,3,4,5,6,7 if I have 4 leaf nodes
then children of
root(1)-> 2,3
2->4,5
3->6,7
so I can simply jump to the children using their position in the vector.
回答1:
Using a "list" or "array" should not pose any issue in this case (since the tree is immutable). The only requirement for O(log n) search is quick random access (ie, O(1) access to a given index).
You can use either a vector
or a deque
, both are suitable. You might run in a scalability issue with the vector
if the system cannot find a large enough memory block to hold all elements, though to begin with it should prove simpler. If you hit this roadblock, switch to deque
, and although you might lose some speed, it should allow you to grow further due to its fragmented nature.
回答2:
I would prefer a preallocated array of your tree nodes (mostly malloced at the initialization time ) with MAX_ELEMENTS as opposed to a stl vector for simple reason of having all tree nodes continuously located on the heap for quicker access at run time.
I said quicker access mainly because the pointer jumps may not be uniform with a distributed stl vector elements for a really huge number of elements in the list (say overflowing a 32 bit number )
You can look at it from the cache line misses from the L1 caches access Point of view of you processor.
来源:https://stackoverflow.com/questions/10715389/is-a-list-implementation-of-binary-tree-scalable