Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to operate off the assumption that the graph is both undirected and connected. That being said, I think you're on the right track, but you're going to need a few more things. First, I highly encourage you to keep your search state and node implementation separate - in other words, it's not a great idea to store a private member variable <code>Node.visible</code> just to aid your search. </p> <p>You can avoid this by maintaining some extra state inside your search method, and use a recursive private helper method to hide that state from callers of your public <code>traverse()</code> method. You will need to properly implement <code>equals</code> and <code>hashCode</code> in your <code>Node</code> class to do this.</p> <p>Also - if you are wanting to create a completely separate <code>Tree</code> with different nodes, you'll want to essentially create new, empty instances of each <code>Node</code> in the <code>Graph</code> and first populate them with their counterpart's data, then build the tree using the cloned nodes. That said, here's some code to get you going (I haven't tested this, but it should give you an idea of what to do):</p> <pre><code>/** * This facade method traverses just the root of the new tree. All recursion is * passed to the "traverseHelper(3)" recursive method. */ public Tree&lt;T&gt; traverse(Graph&lt;T&gt; g){ if(g == null || g.mainNode == null) return null; Node&lt;T&gt; node = g.mainNode; Node&lt;T&gt; clone = new Node&lt;T&gt;(node.data); //this is the root of our new Tree Set&lt;Node&lt;T&gt;&gt; searched = new HashSet&lt;Node&lt;T&gt;&gt;(); //accumulates searched nodes searched.add(node); traverseHelper(node,clone,searched); return new Tree&lt;T&gt;(clone); } /** * Recursively performs BFS on all the children of the specified node and its * corresponding cloned instance. * * Assumes that "node" has been added to "searched" and that * "searched.contains(node)" AND "searched.contains(clone)" will return true by * the time this method is called. */ private void traverseHelper(Node&lt;T&gt; node, Node&lt;T&gt; clone, Set&lt;Node&lt;T&gt;&gt; searched){ if(node.children == null) return; Map&lt;Node&lt;T&gt;,Node&lt;T&gt;&gt; toRecurseOn = new HashMap&lt;Node&lt;T&gt;,Node&lt;T&gt;&gt;(); //This is the Breadth-First part - builds the next leaves in the tree: for(Node&lt;T&gt; child : node.children){ if(child == null || searched.contains(child)) continue; Node&lt;T&gt; childClone = new Node&lt;T&gt;(child.data); //create leaf in the tree clone.children.add(childClone); //builds the current level in the tree childClone.children.add(clone); //maintains undirected-ness of the tree toRecurseOn.put(child,childClone); //lets us BFS later } //This is the Search part - builds the subtrees: Iterator&lt;Node&lt;T&gt;&gt; i = toRecurseOn.keySet().iterator(); while(i.hasNext()){ Node&lt;T&gt; child = i.next(); Node&lt;T&gt; childClone = toRecurseOn.get(child); i.remove(); //Saves a little memory throughout the recursion traverseHelper(child,childClone,searched); } } </code></pre>
    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. This table or related slice is empty.
    1. VO
      singulars
      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