2013-10-12 37 views
16

我需要創建一個類似於Java中所附圖像的樹結構。我發現了與這個問題有關的一些問題,但我還沒有找到一個令人信服和解釋得很好的答案。 應用業務包括食品超級類別(主菜,甜點和其他)。每個類別都可以包含父項或子項等。Java中的樹實現(根,父母和子女)

Desired tree Structure

回答

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(); 
+1

這很好,你有沒有例子說明如何使用樹型函數? – ps0604

+0

'Node parentNode = new Node (「Parent」); \ r' – Jonathan

+2

'Node parentNode = new Node (「Parent」);節點 childNode1 =新節點(「Child 1」,parentNode);節點 childNode2 =新節點(「Child 2」); childNode2.setParent(parentNode);節點 grandchildNode =新節點(「parentNode的子孫。childNode1的子孫」,childNode1);列表> childrenNodes = parentNode.getChildren();' – Jonathan

-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當調用setParentaddChild方法時會拋出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