2012-10-09 108 views
0

我有2個班。 ErdosStruct包含需要進入SimpleTreeEx中的JTree的所有數據。我正在努力寫的是addNodes(),遞歸將erdosStruct中的節點添加到JTree。我有遞歸沒有真正的理解,並建議好友:的Java:遞歸添加節點JTree的

if it has no co authors 
    return 
else 
    add the co authors 

,但我不知道這意味着什麼:(

,你會如何去使用遞歸太節點和子節點添加到樹

import java.util.Vector; 

/** 
* 
* @info The tree data structure. Each node in the tree is of type 
*   AuthNode. The root of the tree contains an AuthNode corresponding to 
*   "Root" 
*/ 
public class ErdosStruct { 
    private AuthNode top = new AuthNode("Root"); 

    public void createStruct() { 

     top.addCoAuth(new AuthNode("Node 0")); 
     top.addCoAuth(new AuthNode("Node 1")); 
     AuthNode coAuth = top.getCoAuth(0); // get Node 0 

     // Add to Node 0 
     coAuth.addCoAuth(new AuthNode("Node 00")); 
     coAuth.addCoAuth(new AuthNode("Node 01")); 
     coAuth = coAuth.getCoAuth(0); // get Node 00 

     coAuth.addCoAuth(new AuthNode("Node 000")); 
     coAuth = coAuth.getCoAuth(0); // get Node 000 

     // add to Node 000 
     coAuth.addCoAuth(new AuthNode("Node 0000")); 
     coAuth.addCoAuth(new AuthNode("Node 0001")); 
     coAuth.addCoAuth(new AuthNode("Node 0002")); 
     coAuth = coAuth.getCoAuth(2); // get Node 0002 

     AuthNode Node0002 = coAuth; 

     // add to Node 0002 
     coAuth.addCoAuth(new AuthNode("Node 00020")); 
     coAuth.addCoAuth(new AuthNode("Node 00021")); 
     coAuth.addCoAuth(new AuthNode("Node 00022")); 
     coAuth.addCoAuth(new AuthNode("Node 00023")); 
     coAuth.addCoAuth(new AuthNode("Node 00024")); 
     coAuth.addCoAuth(new AuthNode("Node 00025")); 

     /// Other Path 

     coAuth = top.getCoAuth(1); // get Node 1 
     coAuth.addCoAuth(new AuthNode("Node 10")); 
     coAuth = coAuth.getCoAuth(0); 

     coAuth.addCoAuth(new AuthNode("Node 100")); 
     coAuth = coAuth.getCoAuth(0); 

     coAuth.addCoAuth(new AuthNode("Node 1000")); 
     coAuth = coAuth.getCoAuth(0); 
    } 

    /** 
    * @return the root element of type AuthNode of the tree data structure 
    */ 
    public AuthNode getRoot() { 
     return top; 
    } 
} 

/** 
* @info AuthNode structure and the interfaces 
*/ 
class AuthNode { 
    /** 
    * Each AuthNode has a name and a vector of AuthNodes corresponding to co-authors 
    */ 
    private String name; 
    private Vector<AuthNode> coAuths = new Vector<AuthNode>(); 

    /** 
    * Two types of constructor 
    */ 
    public AuthNode() { 
    } 

    public AuthNode(String n) { 
     name = n; 
    } 

    // Self-explanatory interfaces 
    public void setName(String n) { 
     name = n; 
    } 

    public String getName() { 
     return name; 
    } 

    // Understand the usage of toString 
    public String toString() { 
     return name; 
    } 

    public int getCoAuthCount() { 
     return coAuths.size(); 
    } 

    public void addCoAuth(AuthNode coAuthor) { 
     coAuths.addElement(coAuthor); 
    } 

    public AuthNode getCoAuth(int i) { 
     return (AuthNode) coAuths.get(i); 
    } 
} 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTree; 
import javax.swing.event.TreeSelectionEvent; 
import javax.swing.event.TreeSelectionListener; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.DefaultTreeModel; 
import javax.swing.tree.MutableTreeNode; 
import javax.swing.tree.TreeModel; 
import javax.swing.tree.TreeNode; 

/** 
* 
* @info Main window of the UI - mainFrame of type JFrame contains ErdosStructurePanel of type JPanel 
*/ 
public class SimpleTreeEx extends JFrame { 
    public static void main(String[] args) { 
     /** 
     * erdosStruct contains a tree where node of the tree represents a 
     * computer scientist - there is an edge from one node to another if the 
     * computer scientists associated to these nodes have co-authored a 
     * scientific article (See createStruct method in ErdosStruct class for 
     * details) 
     */ 
     ErdosStruct erdosStruct = new ErdosStruct(); 
     erdosStruct.createStruct(); 

     SimpleTreeEx mainFrame = new SimpleTreeEx(); 
     /** 
     * ErdosStructPanel constructor takes a parameter of type ErdosStruct 
     */ 
     mainFrame.getContentPane().add(new ErdosStructPanel(erdosStruct)); 

     mainFrame.setSize(500, 500); 
     mainFrame.setVisible(true); 
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

class ErdosStructPanel extends JPanel { 
    /** 
    * @info contains the tree data structure with information on co-authorship relations 
    */ 
    public ErdosStructPanel(ErdosStruct erdosStruct) { 
     DefaultMutableTreeNode root = new DefaultMutableTreeNode(erdosStruct.getRoot()); 
     DefaultTreeModel tModel = new DefaultTreeModel(root); 

     JTree tree = new JTree(tModel); 
     tree.setShowsRootHandles(true); 
     JScrollPane scroll = new JScrollPane(tree); 
     add(scroll, BorderLayout.CENTER); 
    } 

    private void addNodes(ErdosStruct erdosStruct, DefaultTreeModel tModel) { 
     if (erdosStruct.getRoot().getCoAuthCount() == 0) { 
      return; 
     } else { 
      erdosStruct.getRoot().getCoAuth(0) 
      tModel.insertNodeInto(new DefaultMutableTreeNode(), (MutableTreeNode) tModel.getRoot(), 0); 
     } 
    } 
} 
+1

這是一個問題,大量的代碼。你可能會更好唸叨遞歸一點點:http://en.wikipedia.org/wiki/Recursion_%28computer_science%29 –

回答

1
private void addNodes(ErdosStruct erdosStruct, DefaultTreeModel tModel) { 
    if (erdosStruct.getRoot().getCoAuthCount() == 0) { 
     return; 
    } else { 
     AuthNode node = erdosStruct.getRoot(); 
     addNodes(node, tModel, (MutableTreeNode) tModel.getRoot()); 
    } 
} 

protected void addNodes(AuthNode node, DefaultTreeModel tModel, MutableTreeNode parent) { 
    if (node != null) { 
     MutableTreeNode newParent = new DefaultMutableTreeNode(node); 
     tModel.insertNodeInto(newParent, parent, parent.getChildCount() - 1); 
     for (int index = 0; index < node.getCoAuthCount(); index++) { 
      AuthNode child = node.getCoAuth(index); 
      addNodes(child, tModel, newParent); 
     } 
    } 
} 

好吧,那真的很痛苦。這是一個「可能的」解決方案。就個人而言,我更喜歡一個孩子添加到父直接,不是傳遞到方法來添加,但頭好痛

+0

我得到一個ArrayIndexOutOfBoundsException: 異常線程「main」 java.lang.ArrayIndexOutOfBoundsException 在java.lang.System.arraycopy(Native Method) at java.util.Vector.insertElementAt(Vector.java:575) at javax.swing.tree.DefaultMutableTreeNode.insert(DefaultMutableTreeNode.java:195) at javax.swing .tree.DefaultTreeModel.insertNodeInto(DefaultTreeModel.java:234) 在ex5.ErdosStructPanel.addNodes(SimpleTreeEx.java:93) 在ex5.ErdosStructPanel.addNodes(SimpleTreeEx.java:87) 在ex5.ErdosStructPanel。 (SimpleTreeEx.java:65) 在ex5.SimpleTreeEx.main(SimpleTreeEx.java:40) – Kyle

+0

我想象'parent.getChildCount() - 1'能夠返回'-1',嘗試用替換它的'parent.getChildCount()'或'Math.max(0,parent.getChildCount() - 1)'(PS我這樣做了我的頭頂,所以我沒有測試過) – MadProgrammer

+0

更改爲'parent.getChildCount ()'或'Math.max(0,parent.getChildCount() - 1)'導致所有內容都被添加,但全部作爲根節點的葉子。它相當一步,我非常感謝你迄今爲止 – Kyle