Trees and nodes
Tree structures
Next week we will look at a utility that requires a tree structure. Therefore we will
look at this classic data structure this week and make some trees.
|
If you are unfamiliar with this data structure, you should look up some book in
classical data structures. A www search like "tree structures in Java" would also give
you something to look at.
|
|
Secondly, you would have to look at the Java classes of the data structure. They are located
in the javax.swing.tree package, and some of the important classes and interfaces to study include
javax.swing.tree.TreeNode,
javax.swing.tree.DefaultMutableTreeNode,
javax.swing.tree.TreeModel and
javax.swing.tree.DefaultTreeModel.
|
|
Now, try to use these classes. It should not be many lines of codes, just instantiate three or four DefaultMutableTreeNode
objects with any string values and construct some tree structure. Finally, you may add the root node of your tree
to a DefaultTreeModel object.
TestTree.java
|
|
It would now be nice if you could visualize your tree, and in fact there is convenient Swing class to do so called
JTree in the javax.swing package. You simply need to add your tree model object to
the JTree class, and layout object on a JFrame. In no time you'll have a "directory like" visualizations of your
tree structure.
TestJTree.java
|
|
The JTree class and the javaw.swing.tree offers lots of customization through many action and change events
and GUI setup. Do play with some of this possibilities to get more acquainted to the tree structure and the classes.
|
|
|