Why is my method going into infinite recursion?

前端 未结 6 1358
心在旅途
心在旅途 2021-01-15 11:34

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 dividin

相关标签:
6条回答
  • 2021-01-15 11:46

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

    0 讨论(0)
  • 2021-01-15 11:47

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

    0 讨论(0)
  • 2021-01-15 11:48

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

    0 讨论(0)
  • 2021-01-15 11:51

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

    0 讨论(0)
  • 2021-01-15 11:57

    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).

    0 讨论(0)
  • 2021-01-15 11:59

    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?

    0 讨论(0)
提交回复
热议问题