我需要創建一個類似於Java中所附圖像的樹結構。我發現了與這個問題有關的一些問題,但我還沒有找到一個令人信服和解釋得很好的答案。 應用業務包括食品超級類別(主菜,甜點和其他)。每個類別都可以包含父項或子項等。Java中的樹實現(根,父母和子女)
16
A
回答
33
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() {
if(this.children.size() == 0)
return true;
else
return false;
}
public void removeParent() {
this.parent = null;
}
}
例子:
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();
-2
組裝樹節點的過程類似於組裝列表的過程。我們有一個用於初始化實例變量的樹節點的構造函數。
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);
+7
上面圖是不是一個二叉樹。 –
0
這棵樹是不是二叉樹,所以你需要一個像List一樣的子元素的數組。
public Node(Object data, List<Node> children) {
this.data = data;
this.children = children;
}
然後創建實例。
1
在接受的答案
public Node(T data, Node<T> parent) {
this.data = data;
this.parent = parent;
}
應該
public Node(T data, Node<T> parent) {
this.data = data;
this.setParent(parent);
}
否則家長不必孩子在兒童名單
+0
請添加評論 – ytpillai
15
Accepted answer當調用setParent
或addChild
方法時會拋出java.lang.StackOverflowError
。
這裏是沒有這些缺陷稍微簡單的實現:
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);
newChild.setParent(this);
children.add(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;
}
}
一些例子:
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());
}
0
這是我在Java爲您的要求執行。 在treeNode類中,我使用通用數組來存儲樹數據。我們還可以使用數組列表或動態數組來存儲樹的值。
public class TreeNode<T> {
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
相關問題
- 1. 關係實現(父母和子女)
- 2. d3樹 - 有同樣子女的父母
- 3. 實現父母和子女的可點擊視圖
- 4. 帶有無限父母和子女的彈性樹
- 5. linq:祖父母 - 父母 - 子女查詢
- 6. 叉子()和父母/子女過程
- 7. 篩選父母的子女
- 8. 父母子女的ID
- 9. 父母和子女的點擊事件
- 10. 實現一個可以有父母/子女的結構
- 11. 樹數據庫結構與多個父母和多子女
- 12. Linq2Entities針對父母/子女關係的等效查詢,與所有父母和子女,篩選/訂購子女
- 13. 父母/子女關係 - indexOf
- 14. 大表父母子女
- 15. 父母子女關係UI
- 16. 父母和子女關係數組
- 17. 選擇父母和子女記錄
- 18. 休眠和父母/子女關係
- 19. 父母和子女複選框
- 20. PHP分層數組 - 父母和子女
- 21. 提交父母和子女在剃刀
- 22. 無法顯示父母和子女數
- 23. 帳戶層次?父母賬戶有子女可以成爲父母的子女
- 24. JFace TreeViewer中父母的子女
- 25. 在Jquery中尋找父母的子女
- 26. Java繼承 - 父母和子女的成員
- 27. 浮動子女中心父母div
- 28. 叉子父母子女溝通
- 29. 壓扁與父母/子女一棵樹,回報所有節點
- 30. 父母的總寬度根據子女人數
這很好,你有沒有例子說明如何使用樹型函數? – ps0604
'Node parentNode = new Node (「Parent」); \ r' –
Jonathan
'Node parentNode = new Node (「Parent」);節點 childNode1 =新節點(「Child 1」,parentNode);節點 childNode2 =新節點(「Child 2」); childNode2.setParent(parentNode);節點 grandchildNode =新節點(「parentNode的子孫。childNode1的子孫」,childNode1);列表> childrenNodes = parentNode.getChildren();' –
Jonathan