Simple TreeModel Example: Infinite Binary Tree

by Daniel Green

This article illustrates the concept of TreeModel with a very simple Java example representing an infinite binary tree.

Programmers are often uncertain when to build trees and tables explicitly, and when to define tree and table models. It's usually easier to understand the explicit construction by creating DefaultMutableTreeNode and DefaultTableModel objects to hold or represent their data items. For that reason, programmers often miss the opportunity to create a much simpler, flexible, and descriptive implicit implementation by deriving TreeModel and TableModel classes.

TreeModels can be used to present hierarchial structures for which it may be difficult or impossible to build an in-memory representation. Even when you have all the data in memory, it can be ineffecient and wasteful to create a display representation of all your data items when most of them will never be seen.

The TreeModel implementation here represents a binary tree where each node displays a unique integer. All positive integers can be found in the tree. It would therefore be impossible to build this in memory using DefaultMutableTreeNode unless you added listeners that build and add new nodes on demand. Using a TreeModel instead, Java's Swing does all that for you. You only need to provide displayable objects for the nodes Swing asks your model for.

 * InfiniteBinaryTree
 * @author Daniel Green
 * Superliminal Software
 * A little example program to show the power of the TreeModel interface.
 * Running it displays a binary tree with numbers on each node.
 * Every positive number can be found somewhere in the tree.
 * See if you can find the node labeled "1000".
 * Hint: The base 2 representation of any node value can be read out from
 * the path leading to that node where opening the first child of a node
 * represents a 0 and opening the second child represents a 1.
 * Enjoy!

import javax.swing.*;
import javax.swing.tree.*; 

public class InfiniteBinaryTree implements TreeModel {

    public Object getRoot()  {
        return new Integer(1); // start at node number 1

    public int getChildCount(Object parent)  { 
        return 2; // because its a binary tree

    public Object getChild(Object parent, int index) {
        return new Integer(2 * valueOf(parent) + index); // the magic formula

    public int getIndexOfChild(Object parent, Object child) {
        return valueOf(child) % 2; // odd number == first child, even == second

    public boolean isLeaf(Object node) {
        return false; // an infinite number of internal nodes and no leaves!

    // stubbed out methods not needed for display though for any real
    // application at least the listener methods should be implemented.
    public void addTreeModelListener(javax.swing.event.TreeModelListener l) {}
    public void removeTreeModelListener(javax.swing.event.TreeModelListener l) {}
    public void valueForPathChanged(TreePath path, Object newValue) {}

    // helper function
    private int valueOf(Object obj)  {
        return ((Integer)obj).intValue();

     * Creates a JTree from an InfiniteBinaryTree model and displays it.
    public static void main(String args[])  {
        JTree binTree = new JTree(new InfiniteBinaryTree());
        JFrame frame = new JFrame("Infinite Binary Tree");
        frame.getContentPane().add(new JScrollPane(binTree));
        frame.setSize(new java.awt.Dimension(400, 400));

TreeModel and other model interfaces are easy to use and can simplify the display and management of large data structures while also making the design more powerful. As with any tool, this design is not always the best choice. Simple trees and tables are better expressed explicitly, but for representing large or complicated structures, Swing model subclasses are often the better choice.

About the Author

Daniel Green is a member of a UI team designing and implementing the next-generation version of GeneExpress, Gene Logic's flagship product. He is also a resident visualization expert, creating embedded and standalone tools for visualizing data mined from the company's enormous database of genetic expression data which scientists probe in order to better understand disease processes and to find potential treatments.

This article was originally published on Wednesday Dec 12th 2001
Mobile Site | Full Site