1
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?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60

1 Answers1

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

change your MyTreeModel constructor to :

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
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • brilliant thank you... Just one more thing how would i show all employees over 50000 salary using recursion? thanks again. –  Nov 23 '16 at 14:24
  • in `showAllChildren`, only use `System.out.println(tab + node);` if `node.salary > 50000`. Keep the recursion, in case a child employee earns more. – Eric Duminil Nov 23 '16 at 14:28
  • Thanks Eric i appreciate the help. –  Nov 23 '16 at 16:58
  • You're welcome! Please feel free to accept the answer if it solved your problem. – Eric Duminil Nov 23 '16 at 18:33