Java Tree implementation

In this tutorial I am going to show a simple Tree data structure implemented in Java.We will see how to create Tree data structure and also a mechanism of traversing the tree in a very easy way. The Tree which we will create in this tutorial will be the java representation of the image shown below.

Each node in the tree will be represented by the java class Node. The node class has an id attribute and you can add many other attributes to this class. It has a list of the children and a reference to the parent Node.

Node.java

package com.programtak.tree.tutorial;

import java.util.ArrayList;
import java.util.List;

public class Node {
 private String id;
 private final List<Node> children = new ArrayList<>();
 private final Node parent;

 public Node(Node parent) {
  this.parent = parent;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public List<Node> getChildren() {
  return children;
 }

 public Node getParent() {
  return parent;
 }

}

Now let us start building the tree from the node objects.
1. Create the Root Element of the Tree. You can see from below code that a null is passed to the root as the root will have no parent.

  Node treeRootNode = new Node(null);
   treeRootNode.setId("root");

2. Adding the child to parent. The below method adds a child to the parent. And also sets the parent reference to the node.

 private static Node addChild(Node parent, String id) { 
   Node node = new Node(parent);
   node.setId(id);
   parent.getChildren().add(node);
   return node;
}

3. And once we have the tree we will also traverse of the tree. The traversal of the tree to display all the nodes is done using depth first traversal as shown by the below image. Follow the white line from the root.

And here is the function that does the traversal and prints out the tree

private static void printTree(Node node, String appender) {
   System.out.println(appender + node.getId());
   for (Node each : node.getChildren()) {
       printTree(each, appender + appender);
   }
}

In the below class we will create the java representation of the Tree shown in the image above.

package com.programtak.tree.tutorial;

public class TreeTest {

 public static void main(String[] args) {
  Node treeRootNode = new Node(null);
  treeRootNode.setId("root");
  // add child to root node 
  Node childNode= addChild(treeRootNode, "child-1");
  // add child to the child node created above
  addChild(childNode, "child-11");
  addChild(childNode, "child-12");
  
  
  // add child to root node
  Node child2 = addChild(treeRootNode, "child-2");
  // add child to the child node created above
  addChild(child2, "child-21");

  
  printTree(treeRootNode, " ");

 }

 private static Node addChild(Node parent, String id) {
   Node node = new Node(parent);
   node.setId(id);
   parent.getChildren().add(node);
   return node;
 }

 private static void printTree(Node node, String appender) {
  System.out.println(appender + node.getId());
  for (Node each : node.getChildren()) {
   printTree(each, appender + appender);
  }
 }
}

Now let’s look at the output:

 root
  child-1
    child-11
    child-12
  child-2
    child-21

 

Summary

The above program shows the creation of a simple tree that can be traversed both ways from parent to child and from child to parent. In my next tutorials I will be focusing on more ways of traversing the tree and also search methods for finding objects in tree.

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