Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a Binary Tree from a General Tree?
    primarykey
    data
    text
    <p>I have to solve the following constructor for a BinaryTree class in java:</p> <pre><code>BinaryTree(GeneralTree&lt;T&gt; aTree) </code></pre> <p>This method should create a <strong>BinaryTree (bt)</strong> from a <strong>General Tree (gt)</strong> as follows:</p> <p>Every Vertex from <strong>gt</strong> will be represented as a leaf in <strong>bt</strong>. </p> <ul> <li>If gt is a leaf, then bt will be a leaf with the same value as gt </li> <li>If gt is not a leaf, then bt will be constructed as an empty root, a left subTree (lt) and a right subTree (lr). Lt is a stric binary tree created from the oldest subtree of gt (the left-most subtree) and lr is a stric binary tree created from gt without its left-most subtree. </li> </ul> <p>The frist part is trivial enough, but the second one is giving me some trouble. I've gotten this far:</p> <pre><code>public BinaryTree(GeneralTree&lt;T&gt; aTree){ if (aTree.isLeaf()){ root= new BinaryNode&lt;T&gt;(aTree.getRootData()); }else{ root= new BinaryNode&lt;T&gt;(null); // empty root LinkedList&lt;GeneralTree&lt;T&gt;&gt; childs = aTree.getChilds(); // Childs of the GT are implemented as a LinkedList of SubTrees child.begin(); //start iteration trough list BinaryTree&lt;T&gt; lt = new BinaryTree&lt;T&gt;(childs.element(0)); // first element = left-most child this.addLeftChild(lt); aTree.DeleteChild(hijos.elemento(0)); BinaryTree&lt;T&gt; lr = new BinaryTree&lt;T&gt;(aTree); this.addRightChild(lr); } } </code></pre> <p>Is this the right way? If not, can you think of a better way to solve this? This solution, for example, gives me a bunch of nodes with no data at all, I don't know if this is an issue of the problem itself or mine.</p> <p>Thank you!</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.
    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