All Packages Class Hierarchy This Package Previous Next Index
Class com.roguewave.tools.v2-0.BinaryTree
java.lang.Object
|
+----com.roguewave.tools.v2-0.CollectionBase
|
+----com.roguewave.tools.v2-0.BinaryTree
- public class BinaryTree
- extends CollectionBase
- implements Cloneable, Serializable
This class implements a binary search tree. The collection
maintains elements in sorted order according to a Comparator
object given at construction. Duplicates are allowed.
- See Also:
- Comparator, NumericCompare, StringCompare
-
comp_
- The Comparator used to order elements of this tree
-
root_
- The root node of this tree
-
BinaryTree()
- Creates an empty BinaryTree with a null comparator.
-
BinaryTree(Comparator)
- Creates an empty BinaryTree.
-
BinaryTree(Node, Comparator)
- Constructs a BinaryTree with the given root and comparator.
-
balance()
- Balances this BinaryTree.
-
balanceNodes(Vector, int, int)
- Creates a balanced sorted binary tree from a vector of Nodes.
-
clear()
- Removes all items from the collection.
-
clone()
- Returns a clone of this BinaryTree.
-
contains(Object)
- Returns true if the tree contains an element that
compares equal to the given item.
-
elements()
- Returns an enumeration of the elements in the BinaryTree.
-
find(Object)
- Returns the first element in the tree that compares equal to
the given item, or null if no such element.
-
height()
- Returns the height of this BinaryTree by calculating
the length of the longest path from the root to a leaf.
-
insert(Object)
- Inserts the given item into the collection.
-
nodes()
- Returns an Enumeration for an in-order traversal
of the nodes of this tree
-
occurrencesOf(Object)
- Returns the number of elements in the BinaryTree
that compare equal to the given item.
-
remove(Object)
- Removes and returns the first element in the collection that
compares equal to the given item, or returns null if no such element.
-
removeAll(Object)
- Removes all elements of the tree that compare equal
to the given item.
-
setComparator(Comparator)
- Set the Comparator.
-
toString()
- Coverts this BinaryTree to a string.
root_
protected Node root_
- The root node of this tree
comp_
protected Comparator comp_
- The Comparator used to order elements of this tree
BinaryTree
public BinaryTree(Comparator comp)
- Creates an empty BinaryTree.
- Parameters:
- comp - The object used to order the elements
BinaryTree
public BinaryTree()
- Creates an empty BinaryTree with a null comparator.
The comparator must be set before adding any elements.
- See Also:
- setComparator
BinaryTree
protected BinaryTree(Node aNode,
Comparator comp)
- Constructs a BinaryTree with the given root and comparator.
clone
public synchronized Object clone()
- Returns a clone of this BinaryTree. Note that neither
the Comparator object nor the elements of the tree
themselves are cloned. The cloned tree will have the
same node structure as the original.
- Overrides:
- clone in class Object
balance
public synchronized void balance()
- Balances this BinaryTree. Note that because this collection
accepts duplicate entries, invoking this method cannot
always result in a perfectly balanced tree.
clear
public synchronized void clear()
- Removes all items from the collection.
- Overrides:
- clear in class CollectionBase
contains
public final boolean contains(Object item)
- Returns true if the tree contains an element that
compares equal to the given item.
- Overrides:
- contains in class CollectionBase
find
public synchronized Object find(Object item)
- Returns the first element in the tree that compares equal to
the given item, or null if no such element.
- Overrides:
- find in class CollectionBase
height
public synchronized int height()
- Returns the height of this BinaryTree by calculating
the length of the longest path from the root to a leaf.
Note that an empty tree has height 0, while a tree with
only a root with no children has height 1.
insert
public synchronized Object insert(Object item)
- Inserts the given item into the collection.
- Returns:
- item
- Overrides:
- insert in class CollectionBase
elements
public synchronized Enumeration elements()
- Returns an enumeration of the elements in the BinaryTree.
Use the returned Enumeration object to visit each of
the elements in turn.
- Overrides:
- elements in class CollectionBase
occurrencesOf
public synchronized int occurrencesOf(Object item)
- Returns the number of elements in the BinaryTree
that compare equal to the given item.
- Overrides:
- occurrencesOf in class CollectionBase
remove
public synchronized Object remove(Object item)
- Removes and returns the first element in the collection that
compares equal to the given item, or returns null if no such element.
- Overrides:
- remove in class CollectionBase
removeAll
public synchronized int removeAll(Object item)
- Removes all elements of the tree that compare equal
to the given item.
- Returns:
- the number of items removed
- Overrides:
- removeAll in class CollectionBase
setComparator
public boolean setComparator(Comparator comp)
- Set the Comparator. The collection must be empty or this
method will fail.
- Returns:
- true if the method is successfull, false otherwise.
toString
public synchronized String toString()
- Coverts this BinaryTree to a string. The string will have
the form
CompClass:[root [left, right]]
where CompClass is the string value of the Comparator
and left and right recursively show the values
of the children of the root. Thus,
the returned string will show the node structure of the tree
and will not show the elements in sorted sequence.
- Overrides:
- toString in class Object
balanceNodes
protected Node balanceNodes(Vector nodes,
int left,
int right)
- Creates a balanced sorted binary tree from a vector of Nodes.
- Parameters:
- nodes - A vector of Nodes in sorted order
- left - Left index of Nodes to use
- right - Right index of Nodes to use
- Returns:
- The root of the new tree
nodes
protected Enumeration nodes()
- Returns an Enumeration for an in-order traversal
of the nodes of this tree
All Packages Class Hierarchy This Package Previous Next Index