Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are passing the counter variable by value, not by reference. Thus, any changes made to it are local to the current stack frame and are lost as soon as the function returns and that frame is popped of the stack. Java doesn't support passing primitives (or anything really) by reference, so you'd either have to pass it as a single element array or wrap it in an object to get the behavior you're looking for.</p> <p>Here's a simpler (untested) version that avoids the need to pass a variable by reference:</p> <pre><code>private int getMinDepth(QuadTreeNode node){ if(node == null) return 0; return 1 + Math.min( Math.min(getMinDepth(node.q1), getMinDepth(node.q2)), Math.min(getMinDepth(node.q3), getMinDepth(node.q4))); } </code></pre> <p>Both your version and the one above are inefficient because they search the entire tree, when really you only need to search down to the shallowest depth. To do it efficiently, use a queue to do a breadth-first search like Tom recommended. Note however, that the trade-off required to get this extra speed is the extra memory used by the queue.</p> <p>Edit:</p> <p>I decided to go ahead and write a breadth first search version that doesn't assume you have a class that keeps track of the nodes' depths (like Tom's NodeWithDepth). Once again, I haven't tested it or even compiled it... But I think it should be enough to get you going even if it doesn't work right out of the box. This version should perform faster on large, complex trees, but also uses more memory to store the queue.</p> <pre><code>private int getMinDepth(QuadTreeNode node){ // Handle the empty tree case if(node == null) return 0; // Perform a breadth first search for the shallowest null child // while keeping track of how deep into the tree we are. LinkedList&lt;QuadTreeNode&gt; queue = new LinkedList&lt;QuadTreeNode&gt;(); queue.addLast(node); int currentCountTilNextDepth = 1; int nextCountTilNextDepth = 0; int depth = 1; while(!queue.isEmpty()){ // Check if we're transitioning to the next depth if(currentCountTilNextDepth &lt;= 0){ currentCountTilNextDepth = nextCountTilNextDepth; nextCountTilNextDepth = 0; depth++; } // If this node has a null child, we're done QuadTreeNode node = queue.removeFirst(); if(node.q1 == null || node.q2 == null || node.q3 == null || node.q4 == null) break; // If it didn't have a null child, add all the children to the queue queue.addLast(node.q1); queue.addLast(node.q2); queue.addLast(node.q3); queue.addLast(node.q4); // Housekeeping to keep track of when we need to increment our depth nextCountTilNextDepth += 4; currentCountTilNextDepth--; } // Return the depth of the shallowest node that had a null child return depth; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload