Derek wrote:Is ~15 levels "ridiculously deep"? That's what a balanced binary tree (which KD-trees are) of 32K elements will have. Octrees and KD-Trees are pretty similar, the main difference is that KD-Trees divide one dimension at a time while trying to maintain a roughly balanced tree, while Octrees divide all three dimensions at once, which I think would make them somewhat less balanced (probably easier to implement though). That said I have no idea how either performs when the points are moving around, I didn't have to worry about that when I implemented a KD-Tree.
Wait, if you just need to walk the data structure, why not just use a linked list? Tree structures are useful for searching and sorting, if you're not doing either of those then you don't need anything nearly that complicated.
For what I'm doing, yes 15 levels is extremely deep. The Octree is only 5 levels deep by comparison, and the cost for walking the tree
is quite expensive for deep trees. Also, using a linked list is pointless for me since that has the same, if not worse, performance than
the standard pairwise algorithm (Which runs in O(n^2)). The KDtree and Octree allow for O(n log n) performance because of how they
treat collections of bodies which are close in proximity as single composite bodies.
Since I'm using the trees to accelerate force evaluation, searching and sorting are useless for me. The main thing I need to do is subdivide the bodies in such
a way that I can make use of properties of the Taylor series of the gravitational potential to do only O(log(n)) evaluations for each body relative to the tree instead
of n evaluations.
The tree walking itself is a non-trivial algorithm which includes computing multipole expansions and passing them both up and down the tree. But this part works
generically independent of the underlying tree structure. The biggest determinant in the performance of the tree depends on the structure of the tree.
Also, I just realized I can use a BSP tree to do this. It seems like the natural generalization to the KDtree but I can subdivide the n-body system according to the
underlying geometry, rather than being forced to use axis aligned planes.