Using Python, is it possible to eliminate multiple recursive tail calls (transform to iteration) in a quadtree implementation, which assigns points (X, Y) to subdomains (a.k.a. leaf-nodes)? Here's a not-so pseudo-code:
def decompose(inX, inY, xmin, xmax, ymin, ymax, maxP):
#Paramters: Lists of X and Y coordinates, boundaries, max. #points per subdomain
if len(inX) <= maxP: #base case
--write out list of points and boundaries--
else:
xB = (xmin + xmax) / 2 #calculate new boundaries
yB = (ymin + ymax) / 2
for x, y in zip(inX, inY): #assign points to subdomains (a.k.a. children-nodes)
if x < xB:
if y < yB:
R1X.append(x), R1Y.append(y)
else:
R2X.append(x), R2Y.append(y)
else:
if y < yB:
R3X.append(x), R3Y.append(y)
else:
R4X.append(x), R4Y.append(y)
decompose(R1X, R1Y, xmin, xB, ymin, yB, maxP) #recursive tail calls (which I want to eliminate)
decompose(R2X, R2Y, xmin, xB, yB, ymax, maxP)
decompose(R3X, R3Y, xB, xmax, ymin, yB, maxP)
decompose(R4X, R4Y, xB, xmax, yB, ymax, maxP)
I am aware of the methods to eliminate tail calls, but the examples I have seen do not exhibit the fork pattern inherent in recursive quadtree decomposition (multiple independent recursive tail calls). My motivation is to write code that does not exhaust the stack if I use it on millions of points that may exhibit substantial clustering, therefore leading to very deep recursion. In addition, I want to include the temporal dimension and implement spatial buffers, so that points may be assigned to multiple subdomains (I need that for further processing). Due to this substantial amount of customization, existing quadtree implementations might not be suitable. If you're bored by just another newbie asking the same old question, go on and have a nice day, thanks for reading. Otherwise you could either point me towards resources that help me solve the problem or you could post a potential solution. I'd be glad if you did.