|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnz.ac.waikato.jdsl.core.ref.AbstractPositionalContainer
nz.ac.waikato.jdsl.core.ref.NodeBinaryTree
public class 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,
java.lang.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,
java.lang.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 |
java.lang.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 |
java.lang.Object |
replaceElement(Accessor p,
java.lang.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 |
java.lang.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 nz.ac.waikato.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 nz.ac.waikato.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, java.lang.Object eltOfParent, BinaryTree newSibling)
graftOnLeft
in interface BinaryTree
subtree
- 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 left child of the new nodepublic void graftOnRight(Position subtree, java.lang.Object eltOfParent, BinaryTree newSibling)
graftOnRight
in interface BinaryTree
subtree
- 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 nodepublic void expandExternal(Position mustBeExternal) throws InvalidAccessorException
expandExternal
in interface BinaryTree
mustBeExternal
- 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 BinaryTree
mustBeExternal
- 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 BinaryTree
rootOfSubtree
- 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 java.lang.Object link(Position mustBeExternal, BinaryTree newSubtree) throws InvalidAccessorException, InvalidContainerException
link
in interface BinaryTree
mustBeExternal
- the external node to replace with the new subtreenewSubtree
- the subtree to link in
InvalidAccessorException
- if node
is not
external, is null, or does not belong to this binary tree.
InvalidContainerException
- if newSubtree
is null, incompatible with, or equal to this binary tree.public BinaryTree replaceSubtree(Position subtreeRoot, BinaryTree newSubtree) throws InvalidAccessorException, InvalidContainerException
replaceSubtree
in interface BinaryTree
subtreeRoot
- 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 InspectableBinaryTree
p
- Any internal node of the tree
node
InvalidAccessorException
- if node
is null
or does not belong to this binary tree.
BoundaryViolationException
- if node
is
externalpublic Position rightChild(Position p) throws InvalidAccessorException, BoundaryViolationException
rightChild
in interface InspectableBinaryTree
p
- Any internal node of the tree
node
InvalidAccessorException
- if node
is null
or does not belong to this binary tree.
BoundaryViolationException
- if node
is
externalpublic Position sibling(Position p) throws InvalidAccessorException, BoundaryViolationException
sibling
in interface InspectableBinaryTree
p
- 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
rootpublic boolean isRoot(Position p) throws InvalidAccessorException
isRoot
in interface InspectableTree
p
- a node
true
if node
is the root of this tree
InvalidAccessorException
- if node
is null or
does not belong to this treepublic boolean isInternal(Position p) throws InvalidAccessorException
isInternal
in interface InspectableTree
p
- a node
true
if node
has at least one child
InvalidAccessorException
- if node
is null or
does not belong to this treepublic boolean isExternal(Position p) throws InvalidAccessorException
isExternal
in interface InspectableTree
p
- 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 InspectableTree
p
- a node
InvalidAccessorException
- if node
is null or
does not belong to this tree
BoundaryViolationException
- if node
is the
root of the treepublic PositionIterator children(Position p) throws InvalidAccessorException
children
in interface InspectableTree
p
- a node of the tree
InvalidAccessorException
- if node
is null or
does not belong to this treepublic PositionIterator siblings(Position p) throws InvalidAccessorException
siblings
in interface InspectableTree
p
- a node of the tree
InvalidAccessorException
- if node
is null or
does not belong to this treepublic int numChildren(Position node) throws InvalidAccessorException
numChildren
in interface InspectableTree
node
- a node of the tree
node
InvalidAccessorException
- if node
is null or
does not belong to this treepublic Position siblingAfter(Position node) throws BoundaryViolationException, InvalidAccessorException
siblingAfter
in interface InspectableTree
node
- a node
node
BoundaryViolationException
- if node
is
either the last child of a node or the root
InvalidAccessorException
- if node
is null
or does not belong to this treepublic Position siblingBefore(Position node) throws BoundaryViolationException, InvalidAccessorException
siblingBefore
in interface InspectableTree
node
- 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 treepublic Position firstChild(Position node) throws BoundaryViolationException, InvalidAccessorException
firstChild
in interface InspectableTree
node
- a node
node
BoundaryViolationException
- if node
is
a leaf
InvalidAccessorException
- if node
is null
or does not belong to this treepublic Position lastChild(Position node) throws BoundaryViolationException, InvalidAccessorException
lastChild
in interface InspectableTree
node
- a node
node
BoundaryViolationException
- if node
is
a leaf
InvalidAccessorException
- if node
is null
or does not belong to this treepublic int rankOfChild(Position child) throws BoundaryViolationException, InvalidAccessorException
rankOfChild
in interface InspectableTree
child
- a node
child
BoundaryViolationException
- if child
is
the root
InvalidAccessorException
- if child
is null
or does not belong to this treepublic Position childAtRank(Position node, int rank) throws BoundaryViolationException, InvalidAccessorException
childAtRank
in interface InspectableTree
node
- 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 InspectablePositionalContainer
public ObjectIterator elements()
elements
in interface InspectableContainer
public java.lang.Object replaceElement(Accessor p, java.lang.Object newElement) throws InvalidAccessorException
replaceElement
in interface Container
p
- 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 Container
public int size()
size
in interface InspectableContainer
public boolean isEmpty()
isEmpty
in interface InspectableContainer
isEmpty
in class AbstractPositionalContainer
true
if and only if the container is empty (holds
no elements)InspectableBinaryTree
public boolean contains(Accessor a)
contains
in interface InspectableContainer
public java.lang.String toString()
toString
in class java.lang.Object
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 |