Find the root node of a tree from any node in the Java Tree

In this tutorial we will see how a traversal to the root node can be achieved from any node in a java tree. In the below tree we can reach the root from any Node.

The traversal is  achieved by a adding a simple method to each node.It is very concise.

public Node getRoot() {<br />
 if(parent == null){<br />
  return this;<br />
 }<br />
 return parent.getRoot();<br />
}</p>
<p>

So how does magical function work. Every node first checks if it is a root by itself. And it can be root only when it has no parent. If it is not a root, it calls the function to the parent and it bubbles up right to the root. And once a root is found the resulting node is passed back and the node asking for it gets the reference of the root node. Here is the image depicting the same flow:

Let us see an example.
Node.java 

package com.programtak.tree.tutorial;</p>
<p>import java.util.ArrayList;<br />
import java.util.List;</p>
<p>public class Node {<br />
 private String id;<br />
 private final List&lt;Node&gt; children = new ArrayList&lt;&gt;();<br />
 private final Node parent;</p>
<p> public Node getRoot() {<br />
  if(parent == null){<br />
   return this;<br />
  }<br />
  return parent.getRoot();<br />
 }</p>
<p> public Node(Node parent) {<br />
  this.parent = parent;<br />
 }</p>
<p> public String getId() {<br />
  return id;<br />
 }</p>
<p> public void setId(String id) {<br />
  this.id = id;<br />
 }</p>
<p> public List&lt;Node&gt; getChildren() {<br />
  return children;<br />
 }</p>
<p> public Node getParent() {<br />
  return parent;<br />
 }</p>
<p>}<br />

Now Let us build a Tree based on the diagram shown above and also use the getRoot() method to see its root.

package com.programtak.tree.tutorial;</p>
<p>public class TreeTest {</p>
<p> public static void main(String[] args) {<br />
  Node treeRootNode = new Node(null);<br />
  treeRootNode.setId(&quot;root&quot;);<br />
  // add child to root node<br />
  Node childNode1= addChild(treeRootNode, &quot;child-1&quot;);<br />
  // add child to the child node created above<br />
  addChild(childNode1, &quot;child-11&quot;);</p>
<p>  Node childNode12 = addChild(childNode1, &quot;child-12&quot;);<br />
  addChild(childNode12, &quot;child-121&quot;);<br />
  addChild(childNode12, &quot;child-122&quot;);</p>
<p>  // add child to root node<br />
  Node child2 = addChild(treeRootNode, &quot;child-2&quot;);<br />
  // add child to the child node created above<br />
  addChild(child2, &quot;child-21&quot;);</p>
<p>  printTree(treeRootNode, &quot; &quot;);</p>
<p> }</p>
<p> private static Node addChild(Node parent, String id) {<br />
   Node node = new Node(parent);<br />
   node.setId(id);<br />
   parent.getChildren().add(node);</p>
<p>   return node;<br />
 }</p>
<p> private static void printTree(Node node, String appender) {<br />
  System.out.println(appender + node.getId() +&quot;,  root node is: &quot; +  node.getRoot().getId());<br />
  for (Node each : node.getChildren()) {<br />
   printTree(each, appender + appender);<br />
  }<br />
 }<br />
}</p>
<p>

And the output of above traveral of tree which prints the root node with every node:

 root,  root node is: root<br />
  child-1,  root node is: root<br />
    child-11,  root node is: root<br />
    child-12,  root node is: root<br />
        child-121,  root node is: root<br />
        child-122,  root node is: root<br />
  child-2,  root node is: root<br />
    child-21,  root node is: root<br />

Like this post? Don’t forget to share it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.