2011-11-09 9 views
0

私はJavaでツリーを構築したい。 insert delete updateなどの操作それについてどうやって行くの?私は私はあなたが欲しいものツリー内の任意のノードを追加するJavaの

+0

がどの部分あなたが持つ難しさを持っているのですか? – oksayt

+0

ユーザー入力時にノードを作成します。例えば、ユーザがホームディレクトリおよび音楽、文書および映画をサブディレクトリとして作成することを望む場合 – nyfer

+0

ユーザ入力を言うときには、より具体的にする必要があります。彼らはコマンドラインと対話してテキストを入力していますか?もしそうなら、どうですか?いくつかの入力例を示し、結果がどうなるかを説明する。また、実際の問題点を特定する必要があります。入力を解析していますか?実際にそれをツリーに追加しますか?質問を編集してこの情報を追加してください。 – Bryan

答えて

1

が解決

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTree; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.DefaultTreeModel; 


public class MyTree extends JFrame{ 

private JTree tree; 
private DefaultTreeModel model; 
private DefaultMutableTreeNode rootNode; 


public MyTree() 
{ 
    DefaultMutableTreeNode philosophersNode = getTree(); 
     model = new DefaultTreeModel(philosophersNode); 
     tree = new JTree(model); 
     JButton addButton = new JButton("Add "); 
     addButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
      addNode(); 
      } 
     }); 
     JButton removeButton = new JButton("Delete"); 
     removeButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       removeSelectedNode(); 
      } 
     }); 
     JPanel inputPanel = new JPanel(); 
     inputPanel.add(addButton); 
     inputPanel.add(removeButton); 

     Container container = getContentPane(); 

     container.add(new JScrollPane(tree), BorderLayout.CENTER); 

     container.add(inputPanel, BorderLayout.NORTH); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setSize(400, 500); 
     setVisible(true); 

} 

private DefaultMutableTreeNode getSelectedNode() { 

     return (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); 
     } 

private DefaultMutableTreeNode getTree() { 
     rootNode = new DefaultMutableTreeNode("Root"); 
     return rootNode; 
     } 

private void removeSelectedNode() { 
     DefaultMutableTreeNode selectedNode = getSelectedNode(); 
     if (selectedNode != null){ 
      if(selectedNode.toString()=="Root") 
       { 
        JOptionPane.showMessageDialog(MyTree.this, "Cannot delete root element", "Error", 
           JOptionPane.ERROR_MESSAGE); 
       }else{ 

      model.removeNodeFromParent(selectedNode); 
       } 
     } 

     } 

private void addNode() { 
     DefaultMutableTreeNode parent = getSelectedNode(); 
     if (parent == null) { 
      JOptionPane.showMessageDialog(MyTree.this, "Select an area.", "Error", 
       JOptionPane.ERROR_MESSAGE); 

      return; 
     } 
     String name = JOptionPane.showInputDialog(MyTree.this, "Enter Name:"); 
     model.insertNodeInto(new DefaultMutableTreeNode(name), parent,  parent.getChildCount()); 

     } 

public static void main(String args[]) { 
     new MyTree(); 
     } 

} 
1

全くわからない}

import java.util.ArrayList; 


public class Tree { 

private Node root; 

public Tree(String rootData) 
{ 
    root=new Node(); 
    root.data=rootData; 
    root.children=new ArrayList<Node>(); 
} 

public void addChild(String name) 
{ 

} 
} 

import java.util.*; 

class Node { 
     String data; 
     Node parent; 
    List<Node> children; 

public Node() 
{ 
    data=null; 
    children=null; 
    parent=null; 
} 
public Node(String name) 
{ 
    Node n=new Node(name); 
    n.data=name; 
    n.children=new ArrayList<Node>(); 
} 

次私が実装した

ユーザー入力に基づいて、子供たちを追加したい、バイナリツリーをたくありません。おそらく、JTreeのDefaultTreeModel実装やTreeModelインタフェースを調べたいと思うかもしれません。ホイールを再構築する必要はありません。多くの場合、ホイールは明らかに三角形に見えます。とにかく

、このビットを助けるかもしれない:ここでは

import java.util.ArrayList; 


public class Tree { 
    private Node root; 

    public Tree(String rootData) 
    { 
     root=new Node(); 
     root.data=rootData; 
     root.children=new ArrayList<Node>(); 
    } 

    public List<Node> getPathToNode(Node node) { 
     Node currentNode = node; 
     List<Node> reversePath = new ArrayList<Node>(); 
     reversePath.add(node); 
     while (!(this.root.equals(currentNode)) { 
      currentNode = currentNode.getParentNode(); 
      reversePath.add(currentNode); 
     } 
     Collections.reverse(reversePath); // now the list is root -> node 
     return reversePath;    
    } 

} 

import java.util.*; 

class Node { 
    String data; 
    Node parent; 
    List<Node> children; 

    /* I would remove this constructor or at least initialize the field to non-null defaults. This is bloody dangerous. */ 
    public Node() 
    { 
     data=null; 
     children=null; 
     parent=null; 
    } 

    public Node(String name) 
    { 
     Node n=new Node(name); 
     n.data=name; 
     n.children=new ArrayList<Node>(); 
    } 

    public void addChild(String name) { 
     this.addChild(new Node(name)); 
    } 

    public void addChild(Node child) { 
     this.children.add(child); 
    } 

    public void removeChild(Node child) { 
     this.children.remove(child); 
    } 

    public void removeChild(String name) { 
     this.removeChild(this.getChild(name)); 
    } 

    public Node getChild(int childIndex) { 
     return this.children.get(childIndex); 
    } 

    public Node getChild(String childName) { 
     for (Node child : this.children) { 
      if (child.getName().equals(childName)) { return child; } 
     } 
     return null; 
    } 

    public Node getParentNode() { 
     return this.parent; 
    } 
} 
+0

一般的なツリー次のリンクhttp://sujitpal.blogspot.com/2006/05/java-data-structure-generic-tree.html助けてくれることを願って – nyfer

+0

いいですね。それを楽しみましょう。 –

関連する問題