|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjdsl.core.ref.RedBlackTree
A Dictionary implemented as a red-black tree. (A red-black tree is a balanced search tree that maintains its balance through coloring its nodes. See "Data Structures and Algorithms in Java", Goodrich, Tamassia, 2nd Ed., Ch.9.) The red-black tree, in turn, is implemented on top of a subclass of BinaryTree that has the ability to rotate (restructure). This RB tree begins empty, and its internal structure can be accessed with the inspectableBinaryTree method. Once the comparator is set, it can never be changed. Leaf nodes contain locators with a null key; this separates them from data nodes, which are internal and have locators with legitimate, user's keys. A position's color is represented in the locator it holds rather than through a decoration, for faster access. The iterator-returning methods are not cached. This OrderedDictionary is a multi-map, meaning that it is possible for two elements to have the same key. Currently has RestructurableNodeBinaryTree as a nested class while waiting for a test for RNBT.
| Nested Class Summary |
| Nested classes inherited from class jdsl.core.api.InspectableDictionary |
InspectableDictionary.InvalidLocator |
| Field Summary | |
static int |
BLACK
|
static int |
DOUBLEBLACK
|
static int |
RED
|
| Fields inherited from interface jdsl.core.api.InspectableOrderedDictionary |
BOUNDARY_VIOLATION |
| Fields inherited from interface jdsl.core.api.InspectableDictionary |
NO_SUCH_KEY |
| Constructor Summary | |
RedBlackTree(Comparator comparator)
Takes O(1) time This constructor creates the tree with a single no-element-stored-here locator. |
|
| Method Summary | |
Locator |
after(Locator locator)
Takes O(logN) time -- may need to traverse the height of the tree to find the next note that we do not return color-locators in the leaves -- we only return key locators. |
Locator |
before(Locator locator)
Takes O(logN) time -- may need to traverse the height of the tree to find the next note that we do not return color-locators in the leaves -- we only return key locators. |
protected void |
case1(Position y,
Position z)
Takes O(1) time Implements case 1, the Restructuring case Protected for purposes of allowing snapshots during visualization |
protected void |
case2(Position y)
Takes O(1) time Implements case 2, the Recoloring case Protected for purposes of allowing snapshots during visualization |
protected void |
case3(Position y)
Takes O(1) time Implements case 3, the Adjustment case Protected for purposes of allowing snapshots during visualization |
protected void |
checkDoubleRed(Position p)
Takes O(logN) time -- where N = the number of locators in the tree and O(logN) = the height of the tree Check for double reds, then rotate or promote if necessary Protected for purposes of allowing snapshots during visualization |
Locator |
closestAfter(Object key)
Takes O(logN) time -- traverses the height of the tree once. |
Locator |
closestBefore(Object key)
Takes O(logN) time -- traverses the height of the tree once. |
protected void |
colorPromotion(Position parent,
Position uncle)
Takes O(logN) time -- where N = the number of locators in the tree and O(logN) = the height of the tree Do a color promotion and then check if colors are now wrong higher up Protected for purposes of allowing snapshots during visualization |
boolean |
contains(Accessor a)
Takes O(1) time |
ObjectIterator |
elements()
Takes O(N) time from the need to iterate through the tree during snapshot -- where N= the number of locators in the tree Could very easily be cached; not sure that would be useful |
Locator |
find(Object key)
Takes O(logN) time -- where N = the number of locators in the tree and O(logN) = the height of the tree Takes the time to traverse the tree's height, which is O(logN) |
LocatorIterator |
findAll(Object key)
Takes O(logN+R) time -- where N = the number of locators in the tree and O(logN) = the height of the tree, and R = the number of instances of key in the tree O(log N) for one element; in theory, for each element, taking its inorder prev or next may take up to O(log N). |
Locator |
first()
Takes O(log N) time to traverse the height of the tree where N is the number of locators in the tree and O(logN) is the height of the tree |
Locator |
insert(Object key,
Object element)
Takes O(logN) time -- where N = the number of locators in the tree and O(logN) = the height of the tree The worst-case insertion would restructure once each step up the tree. |
InspectableBinaryTree |
inspectableBinaryTree()
Used for visualization and testers |
boolean |
isBlack(Position p)
Returns whether or not the given position of the underlying tre is black; for visualization/testing purposes. |
boolean |
isDoubleBlack(Position p)
Returns whether or not the given position of the underlying tre is double-black; for visualization/testing purposes. |
boolean |
isEmpty()
Takes O(1) time |
boolean |
isRed(Position p)
Returns whether or not the given position of the underlying tre is red; for visualization/testing purposes. |
ObjectIterator |
keys()
Takes O(N) time from the need to iterate through the tree during snapshot -- where N= the number of locators in the tree Could very easily be cached; not sure that would be useful |
Locator |
last()
Takes O(log N) time to traverse the height of the tree where N is the number of locators in the tree and O(logN) is the height of the tree |
LocatorIterator |
locators()
Takes O(N) time from the need to iterate through the tree during snapshot -- where N= the number of locators in the tree Could very easily be cached; not sure that would be useful |
Container |
newContainer()
Takes O(1) time |
protected void |
recolorAfterRemove(Position p)
Takes O(logN) time -- where N = the number of locators in the tree and O(logN) = the height of the tree. |
void |
remove(Locator locator)
Takes O(logN) time -- where N = the number of locators in the tree and O(logN) = the height of the tree The worst-case removal would restructure once each step up the tree. |
LocatorIterator |
removeAll(Object key)
Takes O(RlogN) time where R = the number of objects with key key and log N = the height of the tree (N locators in the tree) (one removal case per each object) |
Locator |
removeKey(Object key)
Takes O(logN) time -- where N = the number of locators in the tree and O(logN) = the height of the tree The worst-case removal would restructure once each step up the tree. |
Object |
replaceElement(Accessor loc,
Object newElement)
Takes O(1) time |
Object |
replaceKey(Locator locator,
Object key)
Takes O(logN) time -- where N = the number of locators in the tree and O(logN) = the height of the tree Takes the running time it would take to execute both remove and insert. |
int |
size()
Takes O(1) time |
String |
toString()
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
public static final int RED
public static final int BLACK
public static final int DOUBLEBLACK
| Constructor Detail |
public RedBlackTree(Comparator comparator)
| Method Detail |
public Container newContainer()
newContainer in interface Containerpublic int size()
size in interface InspectableContainerpublic boolean isEmpty()
isEmpty in interface InspectableContainertrue if and only if the container is empty (holds
no elements)InspectableBinaryTree
public boolean contains(Accessor a)
throws InvalidAccessorException
contains in interface InspectableContainerInvalidAccessorException - if a is null
public Object replaceElement(Accessor loc,
Object newElement)
throws InvalidAccessorException
replaceElement in interface Containerloc - Accessor in this containernewElement - to be stored at a
InvalidAccessorException - if a is null or does not
belong to this containerpublic LocatorIterator locators()
locators in interface InspectableKeyBasedContainerpublic ObjectIterator elements()
elements in interface InspectableContainerpublic ObjectIterator keys()
keys in interface InspectableKeyBasedContainer
public Object replaceKey(Locator locator,
Object key)
throws InvalidAccessorException,
InvalidKeyException
replaceKey in interface KeyBasedContainerlocator - the locator in the container whose key should be replacedkey - the new key to associate with loc.
InvalidAccessorException - If the locator is not valid
or is not contained by this container
InvalidKeyException - If key cannot be used
by this container
public Locator find(Object key)
throws InvalidKeyException
find in interface InspectableDictionarykey - The key mapped to search for.
Locator referring to the key-element pair
that was found, or NO_SUCH_KEY if it could not be found.
InvalidKeyException - if the specified key is not a valid
key in this container.InspectableDictionary.NO_SUCH_KEY
public LocatorIterator findAll(Object key)
throws InvalidKeyException
findAll in interface InspectableDictionarykey - The key to search for.
InvalidKeyException - if the specified key is not valid in
this container
public Locator closestBefore(Object key)
throws InvalidKeyException
closestBefore in interface InspectableOrderedDictionarykey - A valid key.
key. Note: If no such locator exists, the
returned locator will be BOUNDARY_VIOLATION.
InvalidKeyException - If key is not of a
type accepted by this Container (For example: the key is not
comparable).
public Locator closestAfter(Object key)
throws InvalidKeyException
closestAfter in interface InspectableOrderedDictionarykey - A valid key.
key. Note: If no such Locator exists, the
returned locator will be BOUNDARY_VIOLATION.
InvalidKeyException - If key is not of a
type accepted by this Container (For example: the key is not
comparable).
public Locator after(Locator locator)
throws InvalidAccessorException
after in interface InspectableOrderedDictionarylocator - An abstract position in this Container.
locator. Note: Will return the invalid
BOUNDARY_VIOLATION Locator if no such locator exists.
InvalidAccessorException - If locator is
invalid (For example: It does not actually reference an element
within this Container).
public Locator before(Locator locator)
throws InvalidAccessorException
before in interface InspectableOrderedDictionarylocator - An abstract position in this Container.
locator. Note: Will return the invalid
BOUNDARY_VIOLATION Locator if no such locator exists.
InvalidAccessorException - If locator is
invalid (For example: It does not actually reference an element
within this Container).
public Locator insert(Object key,
Object element)
throws InvalidKeyException
insert in interface KeyBasedContainerkey - the key associated with the specified element.element - the element to insert into the container.
InvalidKeyException - if key cannot be used
by this containerprotected void checkDoubleRed(Position p)
p - The position that would be the child of the two double reds
protected void colorPromotion(Position parent,
Position uncle)
parent - The position that would be the parent of the two double redsuncle - parent's sibling
public LocatorIterator removeAll(Object key)
throws InvalidKeyException
removeAll in interface Dictionarykey - the key to search for
InvalidKeyException - if the specified key is not a
valid key in this container
public Locator removeKey(Object key)
throws InvalidKeyException
InvalidKeyException
public void remove(Locator locator)
throws InvalidAccessorException
remove in interface KeyBasedContainerlocator - a locator in the container to remove
InvalidAccessorException - if the locator is not valid or
is not contained by this containerprotected void recolorAfterRemove(Position p)
p - The node to recolor above
protected void case1(Position y,
Position z)
y - -- the sibling of the double-black nodez - -- the red child of yprotected void case2(Position y)
y - -- the sibling of the double-black nodeprotected void case3(Position y)
y - -- the sibling of the double-black nodepublic Locator first()
first in interface InspectableOrderedDictionarypublic Locator last()
last in interface InspectableOrderedDictionarypublic final boolean isRed(Position p)
p - The position to checkpublic final boolean isBlack(Position p)
p - The position to checkpublic final boolean isDoubleBlack(Position p)
p - The position to checkpublic InspectableBinaryTree inspectableBinaryTree()
public String toString()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||