Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You currently have does not memoize the return value of the function each time. Every time you call <code>maxSet</code>, you should check if you have already computed the result or not. If you have, just return it. If you haven't compute it and store it somewhere. Otherwise, your algorithm will be inefficient. (This approach is called "Dynamic Programming." Learn about it.)</p> <pre><code>// pseudocode: public int maxSet(Posisjon&lt;E&gt; bt){ if (visited[bt]) return computedMax[bt]; visited[bt] = true; // You don't need to manually check for being a leaf // For leaves 'maxIfCurrentNodeIsSelected' is always larger. int maxIfCurrentNodeIsSelected = 1 + helper1(bt); int maxIfCurrentNodeIsNotSelected = helper2(bt); if (maxIfCurrentNodeIsSelected &gt; maxIfCurrentNodeIsNotSelected) { shouldSelect[bt] = true; computedMax[bt] = maxIfCurrentNodeIsSelected; } else { shouldSelect[bt] = false; computedMax[bt] = maxIfCurrentNodeIsNotSelected; } } public Set getSelectionSet(Posisjon&lt;E&gt; bt, Set s) { if (shouldSelect[bt]) { s.Add(bt); // You should check for nulls, of course getSelectionSet(bt.leftChild.leftChild, s); getSelectionSet(bt.leftChild.rightChild, s); getSelectionSet(bt.rightChild.leftChild, s); getSelectionSet(bt.rightChild.rightChild, s); } else { getSelectionSet(bt.leftChild, s); getSelectionSet(bt.rightChild, s); } return s; } </code></pre> <p>call <code>getSelectionSet</code> with the root node and an empty <code>Set</code> as arguments after you called <code>maxSet</code>.</p>
 

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