This is a follow up to
Is a list implementation of binary tree scalable?
What can be the advantages or disadvantages of tree implementation done
using linear array(stl vector) or stl deque
rather than a binary tree with individual nodes having left and right pointers?
assumptions:
the tree will be precomputed and will not be modified once built, and will only be used for searching.
Well, I’d say it’s something of these:
std::vectoryou only allocate memory for the data (the container deals with iterating through itself)std::vector, you memory is localized. E.g. if you want to access a single full level of a tree, that will be sequential in memory, while with individual nodes allocated separately, you would jump through the memory like a bunny rabbit accessing all thatmalloc-euqivalent function. (There are some tricks you could use to avoid that in C, and they still work inC++, but why go with hacks if you have a readystd::vectorsolution).std::vector.reserve()to preallocate all the memory. Additionally, if you know how vector operates, you know that it will call amalloc-equivalent for reserving memory approximately every time you start a new level of your tree – number of allocations should be roughly equal to number of levels in your tree