Note that there are some explanatory texts on larger screens.

plurals
  1. POJava recursion: pass by reference
    primarykey
    data
    text
    <p>I realize this is a hotly debated, controversial topic for Java programmers, but I believe my problem is somewhat unique. My algorithm <strong>REQUIRES</strong> pass by reference. I am doing a clockwise/counterclockwise pre-order traversal of a general tree (i.e. n-children) to assign virtual (x,y) coordinates. This simply means I count (and tag) the nodes of tree I visit as I visit them.</p> <pre><code>/** * Generates a "pre-ordered" list of the nodes contained in this object's subtree * Note: This is counterclockwise pre-order traversal * * @param clockwise set to true for clockwise traversal and false for counterclockwise traversal * * @return Iterator&lt;Tree&gt; list iterator */ public Iterator&lt;Tree&gt; PreOrder(boolean clockwise) { LinkedList&lt;Tree&gt; list = new LinkedList&lt;Tree&gt;(); if(!clockwise) PreOCC(this, list); else PreO(this,list); count = 0; return list.iterator(); } private void PreOCC(Tree rt, LinkedList&lt;Tree&gt; list) { list.add(rt); rt.setVirtual_y(count); count++; Iterator&lt;Tree&gt; ci = rt.ChildrenIterator(); while(ci.hasNext()) PreOCC(ci.next(), list); } private void PreO(Tree rt, LinkedList&lt;Tree&gt; list, int count) { list.add(rt); rt.setX_vcoordinate(count); Iterator&lt;Tree&gt; ci = rt.ReverseChildrenIterator(); while(ci.hasNext()) PreO(ci.next(), list, ++count); } </code></pre> <p>Here I generate the structure of the tree:</p> <pre><code>Tree root = new Tree(new Integer(0)); root.addChild(new Tree(new Integer(1), root)); root.addChild(new Tree(new Integer(2), root)); root.addChild(new Tree(new Integer(3), root)); Iterator&lt;Tree&gt; ci = root.ChildrenIterator(); ci.next(); Tree select = ci.next(); select.addChild(new Tree(new Integer(4), select)); select.addChild(new Tree(new Integer(5), select)); </code></pre> <p>And here is my output when I print the order the nodes are traversed and the coordinates it assigns to the respective node.</p> <p><code>0 3 2 5 4 1</code><br> <code>0 1 2 3 4 3</code> </p> <p><code>0 1 2 4 5 3</code><br> <code>0 1 2 3 4 3</code></p> <p><em>Note: The first two lines is a clockwise pre-order traversal and assignment of the x-coordinates. The next two lines are a counterclockwise pre-order traversal and assignment of they y-coordinates.</em></p> <p>My question is how I can get the second lines to read: <code>0 1 2 3 4 5</code></p> <p>EDIT 1: Here is the code I use to print the order I visit the nodes and the coordinates I assign.</p> <pre><code>Iterator&lt;Tree&gt; pre = root.PreOrder(true); System.out.println(" \t"); while(pre.hasNext()) System.out.print(pre.next() + "\t"); pre = root.PreOrder(true); System.out.println(); System.out.println("x-coordinates:\t"); while(pre.hasNext()) System.out.print(pre.next().getVirtual_x() + "\t"); System.out.println(); System.out.println(); Iterator&lt;Tree&gt; preCC = root.PreOrder(false); System.out.println(" \t"); while(preCC.hasNext()) System.out.print(preCC.next() + "\t"); preCC = root.PreOrder(false); System.out.println(); System.out.println("x-coordinates:\t"); while(preCC.hasNext()) System.out.print(preCC.next().getVirtual_y() + "\t"); </code></pre> <p>Also here is a quote to better explain the x,y coordinates. the vertices.the y-coordinates for the vertices.</p> <blockquote> <p>Compute the counterclockwise pre-ordering of the vertices of T (the ordering are numbered from 0 to n − 1), use them as the x-coordinates for the vertices. </p> <p>Compute the clockwise pre-ordering of the vertices of T (the ordering are numbered from 0 to n − 1), use them as the y-coordinates for the vertices.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    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