|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--BinaryTree
This class supports insertion and deletion of elements in a simple binary tree (e.g., the tree is not a balanced tree like an AVL tree or a red-black tree). Relative to a particular node, nodes on the left are less than the node and nodes on the right are greater.
The Java class library includes the java.util.TreeMap class which is a balanced Red-Black tree which is recommended over this code. As with this class, the TreeMap class supports both insertion and deletion.
This class is not intended as a replacement for the TreeMap class. It was written to some degree as an excercise and demonstration. It uses a classic binary tree algorthm for both insert and delete. In contrast to the TreeMap algorithm, which uses left, right and parent references, this class uses only left and right references. As a result, this algorithm mirrors similar binary tree algorithms that might be implemented in C, C++, Pascal, or Modula-X.
This code is an interesting example of the problems that can be encountered in Java, where only pass-by-value is supported. The classic tree delete algorithm, which depends on pass by reference becomes much more awkward. A reference object is used in this code, which supports indirection. Since all references are made through the reference object, the code is more difficult to write and to understand that it would be for a language like C++ which supports reference arguments.
This object is not synchronized, so it is not safe for multi-threaded use.
Nested Class Summary | |
private class |
BinaryTree.Node
A binary tree node, where the associated data item is a generic Object. |
private class |
BinaryTree.NodeReference
A container for a reference |
Field Summary | |
private java.util.Comparator |
mCompare
Object used to compare Objects in the tree |
private int |
mNodeCount
Number of nodes in the binary tree |
private BinaryTree.NodeReference |
root
root of the binary tree |
Constructor Summary | |
BinaryTree(java.util.Comparator compareObj)
The constructor is initialized with an instance of the Comparator interface, which supports the compare() method which is used to compare objects in the tree with a search key. |
Method Summary | |
private java.lang.Object |
delete(BinaryTree.NodeReference root,
java.lang.Object key)
This function searches a binary tree for "key" and deletes the item from the tree if it found. |
java.lang.Object |
delete(java.lang.Object key)
Search the tree for an object which compares equal to key. |
java.lang.Object |
insert(java.lang.Object newObj)
Insert an object in the tree if it is not already present. |
java.lang.Object |
search(java.lang.Object key)
Search for an object in the tree that compares equal to key. |
private java.lang.Object |
searchAndInsert(BinaryTree.NodeReference root,
java.lang.Object key,
boolean insert)
Recursive tree search and insert. |
int |
size()
Return the number of data items (Objects) in the tree |
private java.lang.String |
spaces(int count)
Return a string consisting of count spaces. |
java.lang.Object[] |
toArray()
Return an ordered array of tree objects, where Object values (as determined by the Comparator object used to initialize the BinaryTree constructor) increase with the array indices (e.g., the smallest value is at index 0, the largest value is at index n. |
private void |
toArray(BinaryTree.Node root,
java.util.Vector vec)
Recursively traverse the tree (using inorder tranversal) and insert the Objects from the tree into a Vector. |
java.lang.String |
toString()
Return a String which contains the tree represented in "landscape" form, running down the page. |
private void |
toString(BinaryTree.NodeReference root,
java.io.PrintStream ps,
int indent)
Recursively build up a "landscape" display for the tree (e.g., a tree running down the page). |
private BinaryTree.Node |
unlink(BinaryTree.NodeReference root)
When a value is removed from an ordered binary tree and the value has two children, it will be replaced by the next value in the right sub-branch which is greater. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private BinaryTree.NodeReference root
private int mNodeCount
private java.util.Comparator mCompare
Constructor Detail |
public BinaryTree(java.util.Comparator compareObj)
Method Detail |
private BinaryTree.Node unlink(BinaryTree.NodeReference root)
The unlink function walks down a sub-tree's left branch until it gets to the end (the leaf node). It removes the value at the end of the branch and returns it (so it can be inserted into the "hole" left by the deleted value). If the value at the end of the left branch has a right branch child, this value replaces the terminal left branch node.
private java.lang.Object delete(BinaryTree.NodeReference root, java.lang.Object key)
When the node is found in the tree there are three possible cases:
private java.lang.Object searchAndInsert(BinaryTree.NodeReference root, java.lang.Object key, boolean insert)
The tree is constructed of unique items. If an item is already present in the tree, key will not be inserted, even though insert may be true.
root
- the root of the current subtree.key
- this object contains the key value being searched for.insert
- if this flag is true, key will be inserted into the tree
if it is not already present.
private java.lang.String spaces(int count)
private void toString(BinaryTree.NodeReference root, java.io.PrintStream ps, int indent)
public java.lang.String toString()
toString
in class java.lang.Object
private void toArray(BinaryTree.Node root, java.util.Vector vec)
public java.lang.Object[] toArray()
public java.lang.Object insert(java.lang.Object newObj)
public java.lang.Object search(java.lang.Object key)
public java.lang.Object delete(java.lang.Object key)
public int size()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |