T
- public class DenseSearchTree<T extends java.lang.Comparable<T>>
extends java.lang.Object
implements java.lang.Iterable<T>
Constructor and Description |
---|
DenseSearchTree() |
Modifier and Type | Method and Description |
---|---|
void |
add(T element)
Note: our trees follow a slightly different convention regarding
both our ordering relation and the placement of duplicates, viz:
|
java.util.Set<T> |
asSet()
Returns the set representation of this Tree.
|
boolean |
contains(T target)
Returns true if at least instance of
target
is found in tree. |
int |
count(T target)
Returns an int greater than or equal to 0, indicating how many occurrences
of
target reside in the tree. |
T |
getMax()
Returns the max value (of type T), or throws an
IllegalStateException if this function
is called on an empty tree. |
T |
getMin()
Returns a value of type T or throws an
IllegalStateException
if this function is called on an empty tree. |
java.util.Iterator<T> |
iterator()
Returns an iterator over the DenseSearchTree.
|
void |
remove(T target)
Somewhat streamlined or simplified version of the classic
BST remove algorithm.
|
int |
size()
Returns "inflated" size of tree, meaning a count of
all keys in the tree.
|
java.lang.String |
toString()
For sanity's sake ...
|
public void add(T element)
Rather than place duplicate elements on the right branch, each Tree Node maintains a count of copies. For example, if we had a tree of Integers containing two instances of the number 1, our Tree Node would logically appear as
[ left-branch 1:2 right-branch ],
where 1:2 means 2 copies of the integer 1.Thus, your add logic will either make a new node and insert it in the correct position in the tree, or it will find a node with the value (key) equal to the value you are adding, and increment its count.
This simplifies your remove logic: if the node's count is greater than 0, then decrement by 1, otherwise invoke the remove logic to physically remove the node and replace it with a Binary Search Tree.
Finally, the "price you pay" for this simplification is that your Iterator (which presents an "in-order" view of the tree), needs to "inflate" or "expand" each node. That is, if you have a node
[ left 3:4 right ]
then 4 instances of the integer 3 need to be created and put into the iteration.
element
- public boolean contains(T target)
target
is found in tree.target
- public int count(T target)
target
reside in the tree. Note, this
function returns 0 when the item is not found in tree.target
- public int size()
public java.util.Set<T> asSet()
Returns the set representation of this Tree. In this case, the set will contain unique elements (i.e., it should omit multiple instances) that comprise the tree.
Note: sets are unordered collections, but trees are partial orderings. This means, among other things, that your set may reflect an internal ordering, such as pre-, in- or post-ordering, but it need not.
public void remove(T target)
target
and decrements
the counter by 1. If that would result in the counter going to
0, however, then the remove logic finds the greatest in order
successor and replaces the node to be removed with that, and
then continues to remove the in order successor whose value
was used to re-label the node to be removed.
IllegalStateException
if the target
is not found in the tree.target
- public T getMin()
IllegalStateException
if this function is called on an empty tree.public T getMax()
IllegalStateException
if this function
is called on an empty tree.public java.lang.String toString()
toString
in class java.lang.Object