I need to create a tree structure similar as the attached image in Java. I've found some questions related to this one but I haven't found a convincing and well explained response. The application business consists in food super categories (main courses, desserts and other). Each of these categories can have parent items or children items and so on.
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.
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.
Root: The root of a tree is a node that has no incoming link (i.e. no parent node). Think of this as a starting point of your tree. Children: The child of a tree is a node with one incoming link from a node above it (i.e. a parent node). If two children nodes share the same parent, they are called siblings.
tree: an acyclic, connected graph with one specially designated node (the root node) parent node: the node adjacent to a given node on the path to the root node. child node: a node adjacent to a node (its parent) which is closer to the root node. internal node: a node with a child.
Accepted answer throws a java.lang.StackOverflowError
when calling the setParent
or addChild
methods.
Here's a slightly simpler implementation without those bugs:
public class MyTreeNode<T>{ private T data = null; private List<MyTreeNode> children = new ArrayList<>(); private MyTreeNode parent = null; public MyTreeNode(T data) { this.data = data; } public void addChild(MyTreeNode child) { child.setParent(this); this.children.add(child); } public void addChild(T data) { MyTreeNode<T> newChild = new MyTreeNode<>(data); this.addChild(newChild); } public void addChildren(List<MyTreeNode> children) { for(MyTreeNode t : children) { t.setParent(this); } this.children.addAll(children); } public List<MyTreeNode> getChildren() { return children; } public T getData() { return data; } public void setData(T data) { this.data = data; } private void setParent(MyTreeNode parent) { this.parent = parent; } public MyTreeNode getParent() { return parent; } }
Some examples:
MyTreeNode<String> root = new MyTreeNode<>("Root"); MyTreeNode<String> child1 = new MyTreeNode<>("Child1"); child1.addChild("Grandchild1"); child1.addChild("Grandchild2"); MyTreeNode<String> child2 = new MyTreeNode<>("Child2"); child2.addChild("Grandchild3"); root.addChild(child1); root.addChild(child2); root.addChild("Child3"); root.addChildren(Arrays.asList( new MyTreeNode<>("Child4"), new MyTreeNode<>("Child5"), new MyTreeNode<>("Child6") )); for(MyTreeNode node : root.getChildren()) { System.out.println(node.getData()); }
import java.util.ArrayList; import java.util.List; public class Node<T> { private List<Node<T>> children = new ArrayList<Node<T>>(); private Node<T> parent = null; private T data = null; public Node(T data) { this.data = data; } public Node(T data, Node<T> parent) { this.data = data; this.parent = parent; } public List<Node<T>> getChildren() { return children; } public void setParent(Node<T> parent) { parent.addChild(this); this.parent = parent; } public void addChild(T data) { Node<T> child = new Node<T>(data); child.setParent(this); this.children.add(child); } public void addChild(Node<T> child) { child.setParent(this); this.children.add(child); } public T getData() { return this.data; } public void setData(T data) { this.data = data; } public boolean isRoot() { return (this.parent == null); } public boolean isLeaf() { return this.children.size == 0; } public void removeParent() { this.parent = null; } }
Example:
import java.util.List; Node<String> parentNode = new Node<String>("Parent"); Node<String> childNode1 = new Node<String>("Child 1", parentNode); Node<String> childNode2 = new Node<String>("Child 2"); childNode2.setParent(parentNode); Node<String> grandchildNode = new Node<String>("Grandchild of parentNode. Child of childNode1", childNode1); List<Node<String>> childrenNodes = parentNode.getChildren();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With