|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectgeneseo.cs.sc.OrderedTree
Represents ordered binary trees containing arbitrary objects. These trees conform to a recursive definition of "tree" as a structure that is either
These trees are "ordered" in the sense that the objects
in the trees are required to support a "less than" relation (Java
interface Comparable), and objects are placed in a tree
in such a way that all object's in the tree's left subtree are less
than the root object, and all objects in the right subtree are
greater than or equal to the root object.
The messages these trees handle are generally standard ones for any ordered tree class. Some are destructive, and therefore do not require assignments within client code when modifying trees.
This class was created as a support tool for the text Algorithms & Data Structures: The Science of Computing by Doug Baldwin and Greg Scragg. All references herein to "the text" refer to that book. Trees are described in Chapter 13 of the text. Some of the methods of this class are ones "left to an exercise" in the text.
Comparable,
Serialized Form| Constructor Summary | |
OrderedTree()
Initialize a new ordered tree to be empty. |
|
| Method Summary | |
void |
addNode(java.lang.Comparable newValue)
Adds a new value to the tree in a leaf position that maintains the tree's order. |
protected void |
cutAndRaise()
Removes a tree's root node. |
void |
delete(java.lang.Comparable target)
Removes one occurrence of a value from a tree. |
boolean |
find(java.lang.Comparable target)
Determines whether a tree contains a value. |
OrderedTree |
getLeft()
Extracts the left subtree of a tree. |
OrderedTree |
getRight()
Extracts the right subtree of a tree. |
java.lang.Comparable |
getRoot()
Extracts the root of a tree. |
void |
insertValue(java.lang.Comparable newValue)
Replaces a tree with a leaf -- i.e., replaces any value at the root of the tree, and makes both subtrees empty. |
boolean |
isEmpty()
Determines whether a tree is empty. |
OrderedTree |
makeNewTree()
Create a new tree that is an instance of the same class as this tree. |
void |
printTree()
Prints the contents of a tree in order. |
void |
printTreePostOrder()
Prints the contents of a tree in postorder (i.e., subtrees before roots). |
void |
printTreePreOrder()
Prints the contents of a tree in preorder (i.e., roots before the contents of their subtrees). |
void |
restore(java.lang.String fileName)
Restores a binary tree from a file. |
void |
save(java.lang.String fileName)
Writes a tree to a file, replacing any previous data in that file. |
protected void |
setLeft(OrderedTree newLeft)
Replaces the left subtree of a tree. |
protected void |
setRight(OrderedTree newRight)
Replaces the right subtree of a tree. |
protected void |
setRoot(java.lang.Comparable newValue)
Replaces the root value in a tree. |
java.lang.String |
toString()
Generates a string representation of a tree. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public OrderedTree()
OrderedTree tree = new OrderedTree();
| Method Detail |
public OrderedTree makeNewTree()
OrderedTree. Every subclass of
OrderedTree must therefore provide a method for handling this
message that returns a new instance of that subclass. Clients are unlikely
to ever invoke this method directly, but it is essential to the correct
functioning of other methods defined for trees.
public java.lang.Comparable getRoot()
Comparable r = someTree.getRoot();
public OrderedTree getLeft()
OrderedTree subtree = someTree.getLeft();
public OrderedTree getRight()
OrderedTree subtree = someTree.getRight();
protected void setRoot(java.lang.Comparable newValue)
someTree.setRoot( "new" );
Most clients should not use this message, but methods in subclasses that need to construct trees in unusual ways may find it helpful.
newValue - The new root value.protected void setLeft(OrderedTree newLeft)
someTree.setLeft( otherTree );
Most clients should not use this message, but methods in subclasses that need to construct trees in unusual ways may find it helpful.
newLeft - The tree that should replace the left subtree.protected void setRight(OrderedTree newRight)
someTree.setRight( otherTree );
Most clients should not use this message, but methods in subclasses that need to construct trees in unusual ways may find it helpful.
newRight - The tree that should replace the right subtree.public boolean isEmpty()
if ( someTree.isEmpty() ) ...
public boolean find(java.lang.Comparable target)
equals message. For example
if ( someTree.find("Hello") ) ...
target - The value to seek.
public void delete(java.lang.Comparable target)
equals
message to determine whether the target value matches the node's
value, and removes that occurrence. This message alters the tree.
For example
someTree.delete( "Goodbye" );
target - The value to remove from the tree.public void insertValue(java.lang.Comparable newValue)
someTree.insertValue( "replacement" );
newValue - The new root value.public void addNode(java.lang.Comparable newValue)
someTree.addValue( "new" );
newValue - The value to insert into the tree.protected void cutAndRaise()
someTree.cutAndRaise();
public void printTree()
someTree.printTree();
public void printTreePreOrder()
someTree.printTreePreOrder();
public void printTreePostOrder()
someTree.printTreePostOrder();
public void save(java.lang.String fileName)
someTree.save( "TreeFile" );
fileName - The name of the file to write the tree to.public void restore(java.lang.String fileName)
someTree.restore( "TreeFile" );
After restoring a tree, the contents of the tree are replaced by the contents of the file, if possible. If for some reason the file couldn't be read, the contents of the tree are unchanged.
fileName - The name of the file to restore the tree from.public java.lang.String toString()
String text = someTree.toString();
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||