Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would use two data structures --- a <strong>queue</strong> (e.g. <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayDeque.html" rel="noreferrer"><code>ArrayDeque</code></a>) for storing objects whose subobjects are to be visited, and a <strong>set</strong> (e.g. <a href="http://java.sun.com/javase/6/docs/api/java/util/HashSet.html" rel="noreferrer"><code>HashSet</code></a>) for storing all visited objects without duplication.</p> <pre><code>Set visited = new HashSet(); // all visited objects Queue next = new ArrayDeque(); // objects whose subobjects are to be visited // NOTE: At all times, the objects in "next" are contained in "visited" // add the first object visited.add(obj); Object nextObject = obj; while (nextObject != null) { // do stuff to nextObject for (Object o : nextObject.subobjects) { boolean fresh = visited.add(o); if (fresh) { next.add(o); } } nextObject = next.poll(); // removes the next object to visit, null if empty } // Now, "visited" contains all the visited objects </code></pre> <p><strong>NOTES:</strong></p> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayDeque.html" rel="noreferrer"><code>ArrayDeque</code></a> is a space-efficient queue. It is implemented as a cyclic array, which means you use less space than a <a href="http://java.sun.com/javase/6/docs/api/java/awt/List.html" rel="noreferrer"><code>List</code></a> that keeps growing when you add elements.</li> <li>"<code>boolean fresh = visited.add(o)</code>" combines "<code>boolean fresh = !visited.contains(o)</code>" and "<code>if (fresh) visited.add(o)</code>".</li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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