Java中的树实现(root,父级和子级)

我需要创建一个类似于Java中附加图像的树结构。 我发现了一些与此相关的问题,但我没有找到令人信服且解释清楚的答案。 应用业务包括食品超级类别(主菜,甜点和其他)。 这些类别中的每一个都可以包含父项或子项等。

期望的树结构

import java.util.ArrayList; import java.util.List; public class Node { private List> children = new ArrayList>(); private Node parent = null; private T data = null; public Node(T data) { this.data = data; } public Node(T data, Node parent) { this.data = data; this.parent = parent; } public List> getChildren() { return children; } public void setParent(Node parent) { parent.addChild(this); this.parent = parent; } public void addChild(T data) { Node child = new Node(data); child.setParent(this); this.children.add(child); } public void addChild(Node 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; } } 

例:

 import java.util.List; Node parentNode = new Node("Parent"); Node childNode1 = new Node("Child 1", parentNode); Node childNode2 = new Node("Child 2"); childNode2.setParent(parentNode); Node grandchildNode = new Node("Grandchild of parentNode. Child of childNode1", childNode1); List> childrenNodes = parentNode.getChildren(); 

接受的答案在调用setParentaddChild方法时抛出java.lang.StackOverflowError

这是一个稍微简单的实现,没有这些错误:

 public class MyTreeNode{ private T data = null; private List 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 newChild = new MyTreeNode<>(data); newChild.setParent(this); children.add(newChild); } public void addChildren(List children) { for(MyTreeNode t : children) { t.setParent(this); } this.children.addAll(children); } public List 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; } } 

一些例子:

 MyTreeNode root = new MyTreeNode<>("Root"); MyTreeNode child1 = new MyTreeNode<>("Child1"); child1.addChild("Grandchild1"); child1.addChild("Grandchild2"); MyTreeNode 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()); } 

在接受的答案中

 public Node(T data, Node parent) { this.data = data; this.parent = parent; } 

应该

 public Node(T data, Node parent) { this.data = data; this.setParent(parent); } 

否则父母在子女列表中没有孩子

此树不是二叉树,因此您需要一个子元素数组,如List。

 public Node(Object data, List children) { this.data = data; this.children = children; } 

然后创建实例。

这是我在java中为您的要求实现的。 在treeNode类中,我使用generics数组来存储树数据。 我们也可以使用arraylistdynamic数组来存储树值。

 public class TreeNode { private T value = null; private TreeNode[] childrens = new TreeNode[100]; private int childCount = 0; TreeNode(T value) { this.value = value; } public TreeNode addChild(T value) { TreeNode newChild = new TreeNode(value, this); childrens[childCount++] = newChild; return newChild; } static void traverse(TreeNode obj) { if (obj != null) { for (int i = 0; i < obj.childCount; i++) { System.out.println(obj.childrens[i].value); traverse(obj.childrens[i]); } } return; } void printTree(TreeNode obj) { System.out.println(obj.value); traverse(obj); } } 

以及上述实现的客户端类。

 public class Client { public static void main(String[] args) { TreeNode menu = new TreeNode("Menu"); TreeNode item = menu.addChild("Starter"); item = item.addChild("Veg"); item.addChild("Paneer Tikka"); item.addChild("Malai Paneer Tikka"); item = item.addChild("Non-veg"); item.addChild("Chicken Tikka"); item.addChild("Malai Chicken Tikka"); item = menu.addChild("Main Course"); item = item.addChild("Veg"); item.addChild("Mili Juli Sabzi"); item.addChild("Aloo Shimla Mirch"); item = item.addChild("Non-veg"); item.addChild("Chicken Do Pyaaza"); item.addChild("Chicken Chettinad"); item = menu.addChild("Desserts"); item = item.addChild("Cakes"); item.addChild("Black Forest"); item.addChild("Black Current"); item = item.addChild("Ice Creams"); item.addChild("chocolate"); item.addChild("Vanilla"); menu.printTree(menu); } } 

OUTPUT

 Menu Starter Veg Paneer Tikka Malai Paneer Tikka Non-veg Chicken Tikka Malai Chicken Tikka Main Course Veg Mili Juli Sabzi Aloo Shimla Mirch Non-veg Chicken Do Pyaaza Chicken Chettinad Desserts Cakes Black Forest Black Current Ice Creams chocolate Vanilla 

组装树节点的过程类似于组装列表的过程。 我们有一个用于初始化实例变量的树节点的构造函数。

 public Tree (Object cargo, Tree left, Tree right) { this.cargo = cargo; this.left = left; this.right = right; } 

我们首先分配子节点:

 Tree left = new Tree (new Integer(2), null, null); Tree right = new Tree (new Integer(3), null, null); 

我们可以创建父节点并同时将其链接到子节点:

 Tree tree = new Tree (new Integer(1), left, right);