|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjdsl.core.ref.AbstractPositionalContainer
jdsl.core.ref.NodeBinaryTree
A node-based Binary Tree. The Positions of the tree are the nodes.
contains() is O(1) time; so replaceSubtree, cut, and link are O(S), where S is the number of positions in that subtree. Positions() and elements() are O(N) -- where N is the number of positions in the entire tree. All other methods are O(1) time.
Elements can be stored at both internal and external (leaf) nodes.
The data structure stores a supernode which in turn stores the root. (this supernode is invisible to the end user) You are only allowed to link in and replaceSubtree with other instances of NodeBinaryTree (or subclasses thereof).
AbstractPositionalContainer,
BinaryTree| Nested Class Summary | |
protected static class |
NodeBinaryTree.NBTNode
This is the class for all user-visible nodes It contains links for its parent, children, and element. |
protected static class |
NodeBinaryTree.NBTSuperNode
This is the supernode. |
| Field Summary | |
protected int |
_size
The size of the tree protected so that RestructurableNodeBinaryTree can access it |
| Constructor Summary | |
|
NodeBinaryTree()
Create a tree. |
protected |
NodeBinaryTree(NodeBinaryTree.NBTNode root)
Create a tree from a set of nodes. |
| Method Summary | |
protected NodeBinaryTree.NBTNode |
checkPosition(Accessor a)
Casts the accessor passed in to the appropriate node class for this container; also checks if it is null. |
Position |
childAtRank(Position node,
int rank)
O(1) time |
PositionIterator |
children(Position p)
O(1) time |
boolean |
contains(Accessor a)
O(1) time |
BinaryTree |
cut(Position rootOfSubtree)
Takes O(S) time, where S is the number of positions in the subtree to cut |
ObjectIterator |
elements()
Takes O(N) time from the need to iterate through the tree during snapshot, where N is the number of elements in the tree |
void |
expandExternal(Position mustBeExternal)
O(1) time |
Position |
firstChild(Position node)
O(1) time |
void |
graftOnLeft(Position subtree,
Object eltOfParent,
BinaryTree newSibling)
Takes O(S) time, where S is the number of positions in the new subtree to graft on You are only allowed to link in and replaceSubtree with other instances of NodeBinaryTree (or subclasses thereof) |
void |
graftOnRight(Position subtree,
Object eltOfParent,
BinaryTree newSibling)
Takes O(S) time, where S is the number of positions in the new subtree to graft on You are only allowed to link in and replaceSubtree with other instances of NodeBinaryTree (or subclasses thereof) |
boolean |
isEmpty()
Overridden from AbstractPositionalContainer to be O(1) time. |
boolean |
isExternal(Position p)
O(1) time |
boolean |
isInternal(Position p)
O(1) time |
boolean |
isRoot(Position p)
O(1) time |
Position |
lastChild(Position node)
O(1) time |
Position |
leftChild(Position p)
O(1) time |
Object |
link(Position mustBeExternal,
BinaryTree newSubtree)
Takes O(S) time, where S is the number of positions in this subtree You are only allowed to link in and replaceSubtree with other instances of NodeBinaryTree (or subclasses thereof) |
Container |
newContainer()
O(1) time |
int |
numChildren(Position node)
O(1) time Can be determined by the inherent nature of the type of node rather than by counting |
Position |
parent(Position p)
O(1) time |
PositionIterator |
positions()
Takes O(N) time from the need to iterate through the tree during snapshot, where N is the number of positions in the tree |
int |
rankOfChild(Position child)
O(1) time |
void |
removeAboveExternal(Position mustBeExternal)
O(1) time |
Object |
replaceElement(Accessor p,
Object newElement)
O(1) time |
BinaryTree |
replaceSubtree(Position subtreeRoot,
BinaryTree newSubtree)
Takes O(S1+S2) time, where S1 and S2 are the number of positions in each subtree You are only allowed to link in and replaceSubtree with other instances of NodeBinaryTree (or subclasses thereof) |
protected void |
resetToEmpty()
Used for resetting the tree to an empty tree after a link or replaceSubtree operation. |
Position |
rightChild(Position p)
O(1) time |
Position |
root()
O(1) time |
Position |
sibling(Position p)
O(1) time |
Position |
siblingAfter(Position node)
O(1) time |
Position |
siblingBefore(Position node)
O(1) time |
PositionIterator |
siblings(Position p)
O(1) time |
int |
size()
O(1) time |
String |
toString()
|
protected void |
updateContainer(Accessor root)
Recursively changes the container of all nodes in the subtree anchored at root to this container, adding to _size for each node whose container we change Takes O(S) time -- where S is the number of elements in the subtree |
| Methods inherited from class jdsl.core.ref.AbstractPositionalContainer |
swapElements |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface jdsl.core.api.PositionalContainer |
swapElements |
| Field Detail |
protected int _size
| Constructor Detail |
public NodeBinaryTree()
null.
protected NodeBinaryTree(NodeBinaryTree.NBTNode root)
throws InvalidAccessorException
root - Root of a tree of existing nodes.
InvalidAccessorException - when the given root has a parent.| Method Detail |
public void graftOnLeft(Position subtree,
Object eltOfParent,
BinaryTree newSibling)
graftOnLeft in interface BinaryTreenewSibling - the binary tree whose root will be linked in as
the left child of the new nodeeltOfParent - the object to be stored in the new nodesubtree - any position in this binary tree
public void graftOnRight(Position subtree,
Object eltOfParent,
BinaryTree newSibling)
graftOnRight in interface BinaryTreesubtree - any position in this binary treeeltOfParent - the object to be stored in the new nodenewSibling - the binary tree whose root will be linked in as
the right child of the new node
public void expandExternal(Position mustBeExternal)
throws InvalidAccessorException
expandExternal in interface BinaryTreemustBeExternal - any external position in this binary tree
InvalidAccessorException - if node is not
external, is null, or does not belong to this binary tree.
public void removeAboveExternal(Position mustBeExternal)
throws InvalidAccessorException,
BoundaryViolationException
removeAboveExternal in interface BinaryTreemustBeExternal - any external position in this binary tree
InvalidAccessorException - if node is not
external, is null, or does not belong to this binary tree.
BoundaryViolationException - if node is
an external position, as required, but also the root of this binary
tree.
public BinaryTree cut(Position rootOfSubtree)
throws InvalidAccessorException
cut in interface BinaryTreerootOfSubtree - the position above which to make the cut
node as its root
InvalidAccessorException - if node is null
or does not belong to this binary tree.
public Object link(Position mustBeExternal,
BinaryTree newSubtree)
throws InvalidAccessorException,
InvalidContainerException
link in interface BinaryTreemustBeExternal - the external node to replace with the new subtreenewSubtree - the subtree to link in
InvalidContainerException - if newSubtree
is null, incompatible with, or equal to this binary tree.
InvalidAccessorException - if node is not
external, is null, or does not belong to this binary tree.
public BinaryTree replaceSubtree(Position subtreeRoot,
BinaryTree newSubtree)
throws InvalidAccessorException,
InvalidContainerException
replaceSubtree in interface BinaryTreesubtreeRoot - any position in this binary treenewSubtree - the binary tree that will replace the binary
tree rooted at subtreeRoot
subtreeRoot as its
root
InvalidAccessorException - if subtreeRoot
is null or does not belong to this binary tree.
InvalidContainerException - if newSubtree
is null, incompatible with, or equal to this binary tree.
protected void updateContainer(Accessor root)
throws InvalidAccessorException
root - The node to begin with
InvalidAccessorException
public Position leftChild(Position p)
throws InvalidAccessorException,
BoundaryViolationException
leftChild in interface InspectableBinaryTreep - Any internal node of the tree
node
BoundaryViolationException - if node is
external
InvalidAccessorException - if node is null
or does not belong to this binary tree.
public Position rightChild(Position p)
throws InvalidAccessorException,
BoundaryViolationException
rightChild in interface InspectableBinaryTreep - Any internal node of the tree
node
InvalidAccessorException - if node is null
or does not belong to this binary tree.
BoundaryViolationException - if node is
external
public Position sibling(Position p)
throws InvalidAccessorException,
BoundaryViolationException
sibling in interface InspectableBinaryTreep - a node of the binary tree.
node.
InvalidAccessorException - if node is null
or does not belong to this binary tree.
BoundaryViolationException - if node is the
root
public boolean isRoot(Position p)
throws InvalidAccessorException
isRoot in interface InspectableTreep - a node
true if node is the root of this tree
InvalidAccessorException - if node is null or
does not belong to this tree
public boolean isInternal(Position p)
throws InvalidAccessorException
isInternal in interface InspectableTreep - a node
true if node has at least one child
InvalidAccessorException - if node is null or
does not belong to this tree
public boolean isExternal(Position p)
throws InvalidAccessorException
isExternal in interface InspectableTreep - Any node of the tree
true if node has no children
InvalidAccessorException - if node is null or
does not belong to this treepublic Position root()
root in interface InspectableTree
public Position parent(Position p)
throws InvalidAccessorException,
BoundaryViolationException
parent in interface InspectableTreep - a node
BoundaryViolationException - if node is the
root of the tree
InvalidAccessorException - if node is null or
does not belong to this tree
public PositionIterator children(Position p)
throws InvalidAccessorException
children in interface InspectableTreep - a node of the tree
InvalidAccessorException - if node is null or
does not belong to this tree
public PositionIterator siblings(Position p)
throws InvalidAccessorException
siblings in interface InspectableTreep - a node of the tree
InvalidAccessorException - if node is null or
does not belong to this tree
public int numChildren(Position node)
throws InvalidAccessorException
numChildren in interface InspectableTreenode - a node of the tree
node
InvalidAccessorException - if node is null or
does not belong to this tree
public Position siblingAfter(Position node)
throws BoundaryViolationException,
InvalidAccessorException
siblingAfter in interface InspectableTreenode - a node
node
InvalidAccessorException - if node is null
or does not belong to this tree
BoundaryViolationException - if node is
either the last child of a node or the root
public Position siblingBefore(Position node)
throws BoundaryViolationException,
InvalidAccessorException
siblingBefore in interface InspectableTreenode - a node
node
BoundaryViolationException - if node is
either the first child of a node or the root
InvalidAccessorException - if node is null
or does not belong to this tree
public Position firstChild(Position node)
throws BoundaryViolationException,
InvalidAccessorException
firstChild in interface InspectableTreenode - a node
node
InvalidAccessorException - if node is null
or does not belong to this tree
BoundaryViolationException - if node is
a leaf
public Position lastChild(Position node)
throws BoundaryViolationException,
InvalidAccessorException
lastChild in interface InspectableTreenode - a node
node
BoundaryViolationException - if node is
a leaf
InvalidAccessorException - if node is null
or does not belong to this tree
public int rankOfChild(Position child)
throws BoundaryViolationException,
InvalidAccessorException
rankOfChild in interface InspectableTreechild - a node
child
BoundaryViolationException - if child is
the root
InvalidAccessorException - if child is null
or does not belong to this tree
public Position childAtRank(Position node,
int rank)
throws BoundaryViolationException,
InvalidAccessorException
childAtRank in interface InspectableTreenode - a noderank - an integer index of the children of
node; childAtRank(0) is the first child,
childAtRank(numChildren(node)-1) is the last child
node at the specified rank
BoundaryViolationException - if rank < 0 or rank >
numChildren(node)-1 or node is a leaf
InvalidAccessorException - if node is null
or does not belong to this treepublic PositionIterator positions()
positions in interface InspectablePositionalContainerpublic ObjectIterator elements()
elements in interface InspectableContainer
public Object replaceElement(Accessor p,
Object newElement)
throws InvalidAccessorException
replaceElement in interface Containerp - Accessor in this containernewElement - to be stored at a
InvalidAccessorException - if a is null or does not
belong to this containerpublic Container newContainer()
newContainer in interface Containerpublic int size()
size in interface InspectableContainerpublic boolean isEmpty()
isEmpty in interface InspectableContainerisEmpty in class AbstractPositionalContainerpublic boolean contains(Accessor a)
contains in interface InspectableContainerpublic String toString()
protected void resetToEmpty()
protected NodeBinaryTree.NBTNode checkPosition(Accessor a)
throws InvalidAccessorException
a - The accessor to cast
InvalidAccessorException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||