A generic tree data structure can be implemented in Java with the concept of an object to define a node and the object itself having a reference to n children nodes (or null to point the tree termination i.e. Leaf Nodes).

The following is a very trivial implementation of this data structure to show the concept of generic tree. The use of an array is completely voluntary, you can use any other datastructure of your choice to hold the children based on your need (Such as ArrayList or even LinkedLists).

This code any any other codes published in this blog are also available at Github (search for icodejava)

package com.icodejava.blog.published.datastructure; /** * * @author Kushal Paudyal www.icodejava.com Created On - Dec 5, 2016 Last * Modified On - Dec 5, 2016 * * This class shows a basic implementation of Generic Tree concept in * Java. */ public abstract class AbstractGenericTreeNode { private AbstractGenericTreeNode [] children; public AbstractGenericTreeNode [] getChildren() { return children; } public void setChildren(AbstractGenericTreeNode [] children) { this.children = children; } public int getNumberOfChildren () { return children != null ? children.length : 0; } }

The following is a concrete implementation of the Abstract node above and is defined to hold integer values. You however can implement this class to hold the object of your choice.

package com.icodejava.blog.published.datastructure; /** * * @author Kushal Paudyal www.icodejava.com Created On - Dec 5, 2016 Last * Modified On - Dec 5, 2016 * * This class shows a basic implementation of Generic Tree concept in * Java. */ public class IntegerTreeNode extends AbstractGenericTreeNode { private int value; public IntegerTreeNode(int value, AbstractGenericTreeNode[] children) { super(); this.value = value; this.setChildren(children); } public int getValue() { return value; } }

Generic trees are different from Binary Trees. Binary Trees have 2 child nodes at maximum whereas the generic trees can have n-number of children.

## More from: Data Structure

- Implementing a Simple LIFO Stack in Java using LinkedList
- Implementing a Simple FIFO Queue in Java Using Linked List
- In Place Matrix (2D Array) Clockwise and Counterclockwise Rotation – Java Implementation
- Matrix (2D Array) Clockwise and Counterclockwise Rotation with Extra Buffer – Java Implementation
- Array Max Integer Finder (With Big O Analysis)
- A Basic Implementation of Binary Tree in Java
- A Basic Implementation of Generic Tree in Java
- Basic example of implementing Singly Linked List in Java
- How to find the Lowest Common Ancestor (LCA) of two nodes in a Binary Tree
- How to Add and Remove nodes in an unsorted Linked List
- Binary Tree Representation and Tree Traversal (In-Order, Pre-Order, Post-Order)
- Rotating a two dimensional integer array In-Place and using extra memory
- How to reverse a Singly Linked List iteratively and recursively
- Sorted Circular Linked List Implementation And Insert Operation
- Finding Mean Value Of An Integer Array In Java
- How to intersect two sets in Java using java.util.Set data structure.
- Bubble Sorting An Integer Array In Ascending and Descending Order
- How to split strings and separate the words with spaces
- How to easily sort characters in a String?