Class BinaryTreePlus

java.lang.Object
  |
  +--BinaryTreePlus
All Implemented Interfaces:
javax.swing.tree.TreeModel

public class BinaryTreePlus
extends java.lang.Object
implements javax.swing.tree.TreeModel

Class BinaryTreePlus is an extended version of the plain BinaryTree class. BinaryTree plus adds some additional traversal methods, as discussed in CSC 103 Lecture Notes week 4.


Field Summary
protected  BinaryTreePlusNode root
          The root of this tree
protected  int size
          The size of this tree, i.e., the number of nodes it contains
protected  BinaryTreePlusNode[] stack
          Traversal stack
protected  int STACK_SIZE
          Size of traversal stack, good for trees depths up to 100
protected  int stackIndex
          Current stack index
 
Constructor Summary
BinaryTreePlus(BinaryTreePlusNode root)
          Construct this with the given node as the root.
 
Method Summary
 void addTreeModelListener(javax.swing.event.TreeModelListener l)
          This is a TreeModel method we don't care about.
protected  boolean emptyStack()
           
 BinaryTreePlusNode find(java.lang.Object value)
          Return a pointer to the first node containing the given value, using a preorder traversal.
protected  BinaryTreePlusNode findPreorder(java.lang.Object value, BinaryTreePlusNode t)
          Recursive work doer for find.
 java.lang.Object getChild(java.lang.Object parent, int index)
          If the given index = 0, return the left child of the given parent, otherwise return the right child.
 int getChildCount(java.lang.Object parent)
          Return 0 if the given parent object is a leaf node, return 2 otherwise.
 int getIndexOfChild(java.lang.Object parent, java.lang.Object child)
          Return 0 or 1 based on whether the given child is the right or left child of the given parent.
 java.lang.Object getRoot()
          Return the root of this tree.
 boolean isLeaf(java.lang.Object node)
          Return true if the given node is a leaf, false if not.
protected  BinaryTreePlusNode pop()
          Push from a simple array-based stack.
protected  void push(BinaryTreePlusNode t)
          Push onto a simple array-based stack.
 void removeTreeModelListener(javax.swing.event.TreeModelListener l)
          This is a TreeModel method we don't care about.
 java.lang.String toString()
          Convert this to a preorder string of the form: root left subtree ...
protected static java.lang.String toString(BinaryTreePlusNode t, int indent)
          Recursive work doer for toString(), converting the given node value to a string, preceded by indent spacing, followed by the recursive toString of the left and right subtrees.
 java.lang.String traversePreorder(BinaryTreePlusNode t)
          Perform a very basic recursive preorder traversal, returning a space-delimited string of the node values.
 java.lang.String traverseWithNestedLoop()
          Attempt to perform an iterative preorder tree traversal using a doubly nested loop.
 java.lang.String traverseWithStack()
          Perform an iterative preorder tree traversal using an explicit stack.
 void valueForPathChanged(javax.swing.tree.TreePath path, java.lang.Object newValue)
          This is a TreeModel method we don't care about.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

root

protected BinaryTreePlusNode root
The root of this tree

size

protected int size
The size of this tree, i.e., the number of nodes it contains

stack

protected BinaryTreePlusNode[] stack
Traversal stack

stackIndex

protected int stackIndex
Current stack index

STACK_SIZE

protected final int STACK_SIZE
Size of traversal stack, good for trees depths up to 100
Constructor Detail

BinaryTreePlus

public BinaryTreePlus(BinaryTreePlusNode root)
Construct this with the given node as the root.
Method Detail

find

public BinaryTreePlusNode find(java.lang.Object value)
Return a pointer to the first node containing the given value, using a preorder traversal.

findPreorder

protected BinaryTreePlusNode findPreorder(java.lang.Object value,
                                          BinaryTreePlusNode t)
Recursive work doer for find.

toString

public java.lang.String toString()
Convert this to a preorder string of the form: root left subtree ... right subtree ... I.e., the root is on the first line, subtrees are on subsequent lines, each indented two spaces for each level of the tree.
Overrides:
toString in class java.lang.Object

toString

protected static java.lang.String toString(BinaryTreePlusNode t,
                                           int indent)
Recursive work doer for toString(), converting the given node value to a string, preceded by indent spacing, followed by the recursive toString of the left and right subtrees.

traversePreorder

public java.lang.String traversePreorder(BinaryTreePlusNode t)
Perform a very basic recursive preorder traversal, returning a space-delimited string of the node values.

traverseWithNestedLoop

public java.lang.String traverseWithNestedLoop()
Attempt to perform an iterative preorder tree traversal using a doubly nested loop.

traverseWithStack

public java.lang.String traverseWithStack()
Perform an iterative preorder tree traversal using an explicit stack.

push

protected void push(BinaryTreePlusNode t)
Push onto a simple array-based stack.

pop

protected BinaryTreePlusNode pop()
Push from a simple array-based stack.

emptyStack

protected boolean emptyStack()

getChild

public java.lang.Object getChild(java.lang.Object parent,
                                 int index)
If the given index = 0, return the left child of the given parent, otherwise return the right child. If the child is null, return a new node with a value field equal to the string "null". WARNING: Do not return a null value from this method. If null is returned, the tree viewer will die with a runtime error.
Specified by:
getChild in interface javax.swing.tree.TreeModel

getChildCount

public int getChildCount(java.lang.Object parent)
Return 0 if the given parent object is a leaf node, return 2 otherwise. Note that a value of 1 is never returned from this method, even if there is only one child. The getChild method deals with null children by returing a dummy node with "null" as its value.
Specified by:
getChildCount in interface javax.swing.tree.TreeModel

getIndexOfChild

public int getIndexOfChild(java.lang.Object parent,
                           java.lang.Object child)
Return 0 or 1 based on whether the given child is the right or left child of the given parent.
Specified by:
getIndexOfChild in interface javax.swing.tree.TreeModel

getRoot

public java.lang.Object getRoot()
Return the root of this tree.
Specified by:
getRoot in interface javax.swing.tree.TreeModel

isLeaf

public boolean isLeaf(java.lang.Object node)
Return true if the given node is a leaf, false if not.
Specified by:
isLeaf in interface javax.swing.tree.TreeModel

addTreeModelListener

public void addTreeModelListener(javax.swing.event.TreeModelListener l)
This is a TreeModel method we don't care about. Do nothing here, i.e., leave the method body blank as it is.
Specified by:
addTreeModelListener in interface javax.swing.tree.TreeModel

removeTreeModelListener

public void removeTreeModelListener(javax.swing.event.TreeModelListener l)
This is a TreeModel method we don't care about. Do nothing here, i.e., leave the method body blank as it is.
Specified by:
removeTreeModelListener in interface javax.swing.tree.TreeModel

valueForPathChanged

public void valueForPathChanged(javax.swing.tree.TreePath path,
                                java.lang.Object newValue)
This is a TreeModel method we don't care about. Do nothing here, i.e., leave the method body blank as it is.
Specified by:
valueForPathChanged in interface javax.swing.tree.TreeModel