开发者

Why is my method going into infinite recursion?

开发者 https://www.devze.com 2023-02-17 12:22 出处:网络
I\'ve written a method to help build a quadtree. Each quadtree has a root node, and a root node has 4 children. I\'m using depth recursion to stop this function from dividing too many times. The depth

I've written a method to help build a quadtree. Each quadtree has a root node, and a root node has 4 children. I'm using depth recursion to stop this function from dividing too many times. The depth that is passed in is equal to the log base 2 of the side of the square (a squar开发者_高级运维e is always passed in). However, I get infinite recursion from this. Anyone see why?

When I run it, the output is "Depth=0" infinitely many times..


If the n->isLeaf() condition means "all n's children are null", then I suppose it is always false here.


Can't see any issue with the depth check. Are you sure you pass the right depth to the first call? Maybe it isn't infinite, just tooooo deep?


I may be wrong but display depth at the first call. If its value is NaN. you got your answer.


Your handling on the recursive call to buildTreeHelper seems perfectly correct. You check the depth terminating condition before recursing, and you always call it with depth-1.

The only potential prolem I can see is if it's the constructor calls (the ones creating the children) which call buildTreeHelper again. That could result in infinite recursion if, for example, the constructor always calls buildTreeHelper with a object variable for depth (it would have the same or a greater value each time).


Can you replace "QuadtreeNode * &n" with "QuadtreeNode *n" for second argument in buildTreeHelper(...) and check for result please ???


Where is the original call to buildTreeHelper? I can just imagine its in the constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号