简体   繁体   English

这是我在java中的树模型,我想打印出所有节点。 什么是最好的使用方法?

[英]This is my tree model in java, i would like to print out all nodes. What is the best method to use?

import java.util.Enumeration;
import javax.swing.tree.DefaultMutableTreeNode;

public class MyTreeModel {

    private static final long serialVersionUID = 1L;
    private DefaultMutableTreeNode root;
    private DefaultMutableTreeNode m1, m2, m3;
    private DefaultMutableTreeNode n1, n2, n3, n4, n5, n6;
    private DefaultMutableTreeNode o1, o2, o3, o4;


    /**
     * A basic custom tree model that only uses
     * javax.swing.tree.DefaultMutableTreeNode to construct the tree
     */
    public MyTreeModel() {
        initialiseData();
        showEmployees();
    }

    public void showDepthFirst() { // using depthFirstEnumeration()
        Enumeration<DefaultMutableTreeNode> en = root.depthFirstEnumeration();
        while (en.hasMoreElements()) {
            System.out.println(en.nextElement().toString());// lists all the elements
        }
    }

    public void showBreadthFirst() { // using depthFirstEnumeration()
        Enumeration<DefaultMutableTreeNode> en = root.breadthFirstEnumeration();
        while (en.hasMoreElements()) {
            System.out.println(en.nextElement().toString());// lists all the elements
        }
    }




    /**
     * Author's recursive method to show current node and children.
     * 
     * @param node the current node to display
     * @param tab simple string used to tabulate output
     */
    private void showAllChildren(DefaultMutableTreeNode node, String tab){
        if(node == null) node = root;
        System.out.println(tab + node); // calls toString to print node
        for(int i = 0 ; i < node.getChildCount(); i++)
            showAllChildren((DefaultMutableTreeNode)node.getChildAt(i), tab + "  ");    
    }

    private void initialiseData() {
        root = new DefaultMutableTreeNode(new employee("Sarah","Johnson","Managing partner",89000));    
        m1 = new DefaultMutableTreeNode(new employee("Sarah","Johnson","Managing partner",89000));
        m2 = new DefaultMutableTreeNode(new employee("Sandra", "Dee", "Partner" ,78500));
        m3 = new DefaultMutableTreeNode(new employee("Fred", "Dibner", "Finance manager", 67900));
        n1 = new DefaultMutableTreeNode(new employee("Cleo","Patrar", "Junior Partner", 45000));
        n2 = new DefaultMutableTreeNode(new employee("Irfan","Patel","Junior Partner", 45000));
        n3 = new DefaultMutableTreeNode(new employee("George","Bush","Office Manager",37000));
        n4 = new DefaultMutableTreeNode(new employee("Harry","Potter","Solicitor",52500));
        n5 = new DefaultMutableTreeNode(new employee("Ronald","Reagan","Senior Clerk",22000));
        n6 = new DefaultMutableTreeNode(new employee("Simon","Templar","Finance Officer",18000));
        o1 = new DefaultMutableTreeNode(new employee("Jacob","Heart", "Clerk",16000));
        o2 = new DefaultMutableTreeNode(new employee("Barry","Dwyer","Clerk",16000));
        o3 = new DefaultMutableTreeNode(new employee("Mary","Fritz","Clerk",16000));
        o4 = new DefaultMutableTreeNode(new employee("Gordon","Brown","Finance Clerk",16500));

    }

    private void employeeTree() {
        root.add(m1);
        root.add(m2);
        root.add(m3);
        m1.add(n1);
        m1.add(n2);
        m1.add(n3);
        m2.add(n4);
        m3.add(n6);
        n3.add(o1);
        n3.add(o2);
        n5.add(o3);
        n6.add(o4);

    }

    public void showEmployees(){
        displayTree(root, " ======= Employees =======");
    }

    private void displayTree(DefaultMutableTreeNode start, String title){
        System.out.println(title);
        showAllChildren(start, "  ");// String of spaces represents tab
        System.out.println("\n");       
    }


}

this is my tree model in java, what is the best way to print out all the nodes in the tree in a test class? 这是我在java中的树模型,在测试类中打印出树中所有节点的最佳方法是什么?

public class ShowTree
{
    public static void main(String[] args) {
        MyTreeModel tree = new MyTreeModel();
        tree.showEmployees();
    }
}

change your MyTreeModel constructor to : 将MyTreeModel构造函数更改为:

public MyTreeModel() {
    initialiseData();
    employeeTree();
}

and you're done : 你完成了:

======= Employees =======
  Sarah Johnson
    Sarah Johnson
      Cleo Patrar
      Irfan Patel
      George Bush
        Jacob Heart
        Barry Dwyer
    Sandra Dee
      Harry Potter
    Fred Dibner
      Simon Templar
        Gordon Brown

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM