Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Need help implementing an algorithm
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/1567532/java-algorithm-for-finding-the-largest-set-of-independent-nodes-in-a-binary-tree">This algorithm</a> is so advanced for my basic programming skills that I just don't see how I could implement it. I'm posting this in a new question because I can't keep bothering the guy who gave me the algorithm alone about this in the comment section in the previous question. </p> <pre><code>MaxSet(node) = 1 if "node" is a leaf MaxSet(node) = Max(1 + Sum{ i=0..3: MaxSet(node.Grandchildren[i]) }, Sum{ i=0..1: MaxSet(node.Children[i]) }) </code></pre> <p>Thanks too <a href="https://stackoverflow.com/users/33708/mehrdad">mehrdad</a> for the algorithm. </p> <p>The problem here for me is to implement the part of the two sum lines, how can I do that? And I need to mark every node that this algorithm chooses. It's just a "marked" variable in the node class set to true. I don't understand were it makes a decision too choose a node? </p> <p>EDIT to include my code so far: </p> <pre><code>public int maxSet(Posisjon&lt;E&gt; bt){ if (isExternal(bt)){ return 1; } return Math.max(1 + helper1(bt), helper2(bt)); } private int helper1(Posisjon&lt;E&gt; node){ int tmp = 0; if (hasLeft(node)){ if(hasLeft((Position&lt;E&gt;)node.leftChild())){ tmp += maxSet(node.leftChild().leftChild()); } if(hasRight((Position&lt;E&gt;)node.leftChild())){ tmp += maxSet(node.leftChild().rightChild()); } } if(hasRight(node)){ if(hasLeft((Position&lt;E&gt;)node.rightChild())){ tmp += maxSet(node.leftChild().leftChild()); } if(hasRight((Position&lt;E&gt;)node.rightChild())){ tmp += maxSet(node.leftChild().rightChild()); } } return tmp; } private int helper2(Posisjon&lt;E&gt; node){ int tmp = 0; if(hasLeft(node)){ tmp +=maxSet(node.leftChild()); } if(hasRight(node)){ tmp +=maxSet(node.rightChild()); } return tmp; } </code></pre> <p>This seems to be working, what is left now. Is to actually mark the nodes as chosen? Were would I do that? </p> <hr> <p>Updated with code: </p> <pre><code>public ArrayList&lt;Posisjon&lt;E&gt;&gt; getSelectionSet(Posisjon&lt;E&gt; bt, ArrayList&lt;Posisjon&lt;E&gt;&gt; s){ if(bt.marked){ s.add(bt); } if(hasLeft(bt)){ if(hasLeft(bt.leftChild())){ getSelectionSet(bt.leftChild().leftChild(),s); } if(hasRight(bt.leftChild())){ getSelectionSet(bt.leftChild().rightChild(),s); } } if(hasRight(bt)){ if(hasLeft(bt.rightChild())){ getSelectionSet(bt.rightChild().leftChild(),s); } if(hasRight(bt.rightChild())){ getSelectionSet(bt.rightChild().rightChild(),s); } } return s; } public int maxSet(Posisjon&lt;E&gt; bt){ if (bt.visited){ return bt.computedMax; } bt.visited = true; int maxIfCurrentNodeIsSelected = 1 + helper1(bt); int maxIfCurrentNodeIsNotSelected = helper2(bt); if (maxIfCurrentNodeIsSelected &gt; maxIfCurrentNodeIsNotSelected){ bt.marked = true; bt.computedMax = maxIfCurrentNodeIsSelected; }else{ bt.marked = false; bt.computedMax = maxIfCurrentNodeIsNotSelected; } return maxSet(bt); } </code></pre> <p>After submission, I will post the entire code for this! </p>
    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.
 

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