Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Similar presentations


Presentation on theme: "Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L."— Presentation transcript:

1 Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

2 Outline USC CSCI 201L2/10 ▪T▪Trees ▪P▪Program

3 Trees Overview ▪JTree is a component that displays data in a treelike hierarchy ▪All nodes displayed in the tree are in the form of a hierarchical indexed list ›A node can have child nodes ›A node with no children is a leaf ›A node with no parent is the root ▪TreeModel represents the entire tree ▪TreeNode represents a node ▪TreePath represents a path to a node ▪TreeModel does not directly store or manage tree data (different from the TableModel with JTables) ▪MutableTreeNode represents a tree node that can be mutated by adding or removing child nodes or changing the content of the node ▪TreeSelectionModel handles the tree node selection USC CSCI 201L3/10 Trees

4 JTree Hierarchy USC CSCI 201L4/10 Trees

5 JTree API USC CSCI 201L5/10 Tables

6 JTree Example 1 import javax.swing.JFrame; 2 import javax.swing.JPanel; 3 import javax.swing.JScrollPane; 4 import javax.swing.JTree; 5 6 public class Test extends JFrame { 7 public Test() { 8 super("Tree Example"); 9 10 JPanel jp = new JPanel(); 11 String [] data = {"Cat", "Dog", "Fish", "Frog", "Salamander"}; 12 13 JTree tree = new JTree(data); 14 tree.setRootVisible(true); 15 JScrollPane jsp = new JScrollPane(tree); 16 jp.add(jsp); 17 add(jp); 18 19 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 20 setSize(500, 200); 21 setLocationRelativeTo(null); 22 setVisible(true); 23 } 24 25 public static void main(String args[]) { 26 Test t = new Test(); 27 } 28 } USC CSCI 201L6/10 Trees

7 Tree Model Example 1 // import statements omitted 2 3 public class Test extends JFrame { 4 public Test() { 5 super("Tree Example"); 6 7 JPanel jp = new JPanel(); 8 DefaultMutableTreeNode root, europe, northAmerica, us; 9 europe = new DefaultMutableTreeNode("Europe"); 10 europe.add(new DefaultMutableTreeNode("UK")); 11 europe.add(new DefaultMutableTreeNode("Germany")); 12 europe.add(new DefaultMutableTreeNode("France")); 13 europe.add(new DefaultMutableTreeNode("Norway")); 14 15 northAmerica = new DefaultMutableTreeNode("North America"); 16 us = new DefaultMutableTreeNode("US"); 17 us.add(new DefaultMutableTreeNode("California")); 18 us.add(new DefaultMutableTreeNode("Texas")); 19 us.add(new DefaultMutableTreeNode("New York")); 20 us.add(new DefaultMutableTreeNode("Florida")); 21 us.add(new DefaultMutableTreeNode("Illinois")); 22 northAmerica.add(us); 23 northAmerica.add(new DefaultMutableTreeNode("Canada")); 24 25 root = new DefaultMutableTreeNode("World"); 26 root.add(europe); 27 root.add(northAmerica); 28 29 JPanel panel = new JPanel(); 30 panel.setLayout(new GridLayout(1, 2)); 31 // next two lines create different instances of trees with the same nodes 32 panel.add(new JScrollPane(new JTree(root))); 33 panel.add(new JScrollPane(new JTree(new DefaultTreeModel(root)))); USC CSCI 201L7/10 Trees 34 JTextArea jtaMessage = new JTextArea(); 35 jtaMessage.setWrapStyleWord(true); 36 jtaMessage.setLineWrap(true); 37 add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel, new JScrollPane(jtaMessage)), BorderLayout.CENTER); 38 39 // Get tree information 40 jtaMessage.append("Depth of the node US is " + us.getDepth()); 41 jtaMessage.append("\nLevel of the node US is " + us.getLevel()); 42 jtaMessage.append("\nFirst child of the root is " + root.getFirstChild()); 43 jtaMessage.append("\nFirst leaf of the root is " + root.getFirstLeaf()); 44 jtaMessage.append("\nNumber of the children of the root is " + root.getChildCount()); 45 jtaMessage.append("\nNumber of leaves in the tree is " + root.getLeafCount()); 46 String breadthFirstSearchResult = ""; 47 48 // Breadth-first traversal 49 Enumeration bf = root.breadthFirstEnumeration(); 50 while (bf.hasMoreElements()) { 51 breadthFirstSearchResult += bf.nextElement().toString() + " "; 52 } 53 jtaMessage.append("\nBreath-first traversal from the root is " + breadthFirstSearchResult); 54 55 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 56 setSize(500, 200); 57 setLocationRelativeTo(null); 58 setVisible(true); 59 } 60 61 public static void main(String args[]) { 62 Test t = new Test(); 63 } 64 }

8 More Tree Details ▪JTree can fire TreeSelectionEvent and TreeExpansionEvent ›When a new node is selected, a TreeSelectionEvent is fired To handle the TreeSelectionEvent, a listener must implement the TreeSelectionListener interface ›When a node is expanded or collapsed, a TreeExpansionEvent is fired To handle the TreeExpansionEvent, a listener must implement the TreeExpansionListener interface (or extend TreeExpansionAdapter) USC CSCI 201L8/25 Trees

9 Outline USC CSCI 201L9/10 ▪T▪Trees ▪P▪Program

10 Program ▪Create the following GUI. USC CSCI 201L10/10 Program


Download ppt "Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L."

Similar presentations


Ads by Google