CITS2200
Class BinaryTree<E>

java.lang.Object
  extended by CITS2200.BinaryTree<E>

public abstract class BinaryTree<E>
extends Object

A class to represent binary tree where each node contains an item E The class is immutable, so after a tree is created, it cannot be changed.


Constructor Summary
BinaryTree()
          Constructs an empty Binary Tree
BinaryTree(E item, BinaryTree<E> left, BinaryTree<E> right)
          Constructs a binary tree containing the specified item at the root, and a left and right binary tree as children.
 
Method Summary
abstract  boolean equals(Object o)
          Tests whether the tree is equal to an Object, where two trees are equal if either both trees are empty, or both trees contain equal items at the root (tested using the equals method of E), and have equal left subtrees and equal right subtrees (recursively calling the equals method of BinaryTree.
 E getItem()
          Returns the item stored at the root
 BinaryTree<E> getLeft()
          Returns the left subtree
 BinaryTree<E> getRight()
          Returns the right subtree
 boolean isEmpty()
           
abstract  Iterator<E> iterator()
          Returns an iterator that will traverse through every element in the tree, exactly once.
 String toString()
          Returns a string representation of the tree, calling the toString method of E.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BinaryTree

public BinaryTree()
Constructs an empty Binary Tree


BinaryTree

public BinaryTree(E item,
                  BinaryTree<E> left,
                  BinaryTree<E> right)
Constructs a binary tree containing the specified item at the root, and a left and right binary tree as children.

Parameters:
item - the item to be stored in the root.
left - the left subtree.
right - the right subtree.
Method Detail

isEmpty

public boolean isEmpty()
Returns:
true if the tree is empty

getItem

public E getItem()
Returns the item stored at the root

Returns:
the item stored in the root of the tree
Throws:
Underflow - if the tree is empty

getLeft

public BinaryTree<E> getLeft()
Returns the left subtree

Returns:
the left subtree
Throws:
Underflow - if the tree is empty

getRight

public BinaryTree<E> getRight()
Returns the right subtree

Returns:
the right subtree
Throws:
Underflow - if the tree is empty

iterator

public abstract Iterator<E> iterator()
Returns an iterator that will traverse through every element in the tree, exactly once. The order isn't important. If the tree is empty, it will return an iterator for which hasNext() is false.

Returns:
an iterator to traverse every element once

toString

public String toString()
Returns a string representation of the tree, calling the toString method of E. The representation uses tabs to represent the tree structure, with each element on a single line (so the tree appears rotated anticlockwise by 90 degrees).

Overrides:
toString in class Object

equals

public abstract boolean equals(Object o)
Tests whether the tree is equal to an Object, where two trees are equal if either both trees are empty, or both trees contain equal items at the root (tested using the equals method of E), and have equal left subtrees and equal right subtrees (recursively calling the equals method of BinaryTree.

Overrides:
equals in class Object
Returns:
true if both trees are equal.