gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:33 24 4
gpt4 key购买 nike

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");
}


}

这是我用java编写的树模型,在测试类中打印出树中所有节点的最佳方法是什么?

最佳答案

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

将 MyTreeModel 构造函数更改为:

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

你就完成了:

======= 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

关于java - 这是我的java树模型,我想打印出所有节点。最好的使用方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40766399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com