Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a tree data-structure in Java?

Is there any standard Java library class to represent a tree in Java?

Specifically I need to represent the following:

  • The sub-tree at any node can have an arbitrary number of children
  • Each node (after the root) and it's children will have string value
  • I need to get all the children (some sort of list or array of Strings) of a given node and it's string value(i.e. a method that will take a node as input and return all the string values of the children node as output)

Is there any available structure for this or do I need to create my own (if so implementation suggestions would be great).

like image 865
ikl Avatar asked Aug 19 '10 13:08

ikl


People also ask

How are trees implemented in Java?

In Java, a tree node is implemented using a class. The data inside every node can be a string, char, integer, double, or float data type. A binary tree can be implemented in two ways: A node representation and an array representation.

How tree is implemented in data structure?

Insert OperationThe very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then if the data is less than the key value, search for the empty location in the left subtree and insert the data.

Is there a tree data structure in Java?

Tree data structure is useful on occasions where linear representation of data do not suffice, such as creating a family tree. Java provides two in-built classes, TreeSet and TreeMap, in Java Collection Framework that cater to the needs of the programmer to describe data elements in the aforesaid form.

Can we implement data structures in Java?

Data structure is a method of storing and organising data to make it more useful. The data structure is not written in any programming language, such as C, C++, or Java. It is a set of methods that may be used to structure data in memory in any programming language.


2 Answers

Here:

public class Tree<T> {     private Node<T> root;      public Tree(T rootData) {         root = new Node<T>();         root.data = rootData;         root.children = new ArrayList<Node<T>>();     }      public static class Node<T> {         private T data;         private Node<T> parent;         private List<Node<T>> children;     } } 

That is a basic tree structure that can be used for String or any other object. It is fairly easy to implement simple trees to do what you need.

All you need to add are methods for add to, removing from, traversing, and constructors. The Node is the basic building block of the Tree.

like image 185
jjnguy Avatar answered Sep 20 '22 12:09

jjnguy


Yet another tree structure:

public class TreeNode<T> implements Iterable<TreeNode<T>> {      T data;     TreeNode<T> parent;     List<TreeNode<T>> children;      public TreeNode(T data) {         this.data = data;         this.children = new LinkedList<TreeNode<T>>();     }      public TreeNode<T> addChild(T child) {         TreeNode<T> childNode = new TreeNode<T>(child);         childNode.parent = this;         this.children.add(childNode);         return childNode;     }      // other features ...  } 

Sample usage:

TreeNode<String> root = new TreeNode<String>("root"); {     TreeNode<String> node0 = root.addChild("node0");     TreeNode<String> node1 = root.addChild("node1");     TreeNode<String> node2 = root.addChild("node2");     {         TreeNode<String> node20 = node2.addChild(null);         TreeNode<String> node21 = node2.addChild("node21");         {             TreeNode<String> node210 = node20.addChild("node210");         }     } } 

BONUS
See fully-fledged tree with:

  • iterator
  • searching
  • Java/C#

https://github.com/gt4dev/yet-another-tree-structure

like image 36
Grzegorz Dev Avatar answered Sep 19 '22 12:09

Grzegorz Dev