A Practical Algorithm for Computing Neighbors in Quadtrees, Octrees, and Hyperoctrees

A simple and general method for computing location codes of same-size neighboring nodes for any direction within quadtrees is developed and then extended for octrees and 4-D hyperoctrees. The effect of our algorithm is to ascend the tree, guided by navigational cues, until the nearest common ancestor of the target node and the computed neighbor node is reached. An advantage of our algorithm is that we can easily determine if the computed neighbor location is outside the boundaries of the quadtree or (hyper)octree. An experimental analysis of the performance of this finite-state machine (FSM) based algorithm indicates that it runs fast in the average case by measuring the average distance to the closest common ancestor for each node and all of its neighbors. This average grows slowly as the quadtree or octree depth increases. An extension to the algorithm that uses a smaller FSM table with only primary (orthogonal) neighbor directions is given.