|
datastructures | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.datastructures.LinkedBinaryTree
An implementation of the BinaryTree interface by means of a linked structure.
This class serves as a superclass for the BinarySearchTree
implementation. This design decision was made to emphasize the
conceptual relationship that a BinarySearchTree is a specialized
LinkedBinaryTree. An unwanted side-effect of this is that the
size
method returns the number of total nodes
whereas the size
method in the
BinarySearchTree
class returns the number
of internal nodes only. For this reason, the the size
variable instead of the size
method is used
within this class.
BinaryTree
Field Summary | |
protected Position |
root
|
protected int |
size
|
Constructor Summary | |
LinkedBinaryTree()
Creates an empty binary tree. |
Method Summary | |
Position |
addRoot(Object e)
Adds a root node to an empty tree |
void |
attach(Position v,
BinaryTree T1,
BinaryTree T2)
Attaches two trees to be subtrees of an external node. |
protected BTPosition |
checkPosition(Position v)
If v is a good binary tree node, cast to BTPosition, else throw exception |
Iterator |
children(Position v)
Returns an iterator of the children of a node. |
protected BTPosition |
createNode(Object element,
BTPosition parent,
BTPosition left,
BTPosition right)
Creates a new binary tree node |
Iterator |
elements()
Returns an iterator of the elements stored at the nodes |
void |
expandExternal(Position v,
Object l,
Object r)
Expand an external node into an internal node with two external node children |
boolean |
hasLeft(Position v)
Returns whether a node has a left child. |
boolean |
hasRight(Position v)
Returns whether a node has a right child. |
protected void |
inorderPositions(Position v,
List pos)
Creates a list storing the the nodes in the subtree of a node, ordered according to the inorder traversal of the subtree. |
Position |
insertLeft(Position v,
Object e)
Inserts a left child at a given node. |
Position |
insertRight(Position v,
Object e)
Inserts a right child at a given node. |
boolean |
isEmpty()
Returns whether the tree is empty. |
boolean |
isExternal(Position v)
Returns whether a node is external. |
boolean |
isInternal(Position v)
Returns whether a node is internal. |
boolean |
isRoot(Position v)
Returns whether a node is the root. |
Position |
left(Position v)
Returns the left child of a node. |
Position |
parent(Position v)
Returns the parent of a node. |
Iterator |
positions()
Returns an iterator of the tree nodes. |
Object |
remove(Position v)
Removes a node with zero or one child. |
void |
removeAboveExternal(Position v)
Remove an external node v and replace its parent with v's sibling |
Object |
replace(Position v,
Object o)
Replaces the element at a node. |
Position |
right(Position v)
Returns the right child of a node. |
Position |
root()
Returns the root of the tree. |
Position |
sibling(Position v)
Return the sibling of a node |
int |
size()
Returns the number of nodes in the tree. |
void |
swapElements(Position v,
Position w)
Swap the elements at two nodes |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected Position root
protected int size
Constructor Detail |
public LinkedBinaryTree()
Method Detail |
public int size()
size
in interface Tree
public boolean isEmpty()
isEmpty
in interface Tree
public boolean isInternal(Position v) throws InvalidPositionException
isInternal
in interface Tree
InvalidPositionException
public boolean isExternal(Position v) throws InvalidPositionException
isExternal
in interface Tree
InvalidPositionException
public boolean isRoot(Position v) throws InvalidPositionException
isRoot
in interface Tree
InvalidPositionException
public boolean hasLeft(Position v) throws InvalidPositionException
hasLeft
in interface BinaryTree
InvalidPositionException
public boolean hasRight(Position v) throws InvalidPositionException
hasRight
in interface BinaryTree
InvalidPositionException
public Position root() throws EmptyTreeException
root
in interface Tree
EmptyTreeException
public Position left(Position v) throws InvalidPositionException, BoundaryViolationException
left
in interface BinaryTree
InvalidPositionException
BoundaryViolationException
public Position right(Position v) throws InvalidPositionException, BoundaryViolationException
right
in interface BinaryTree
InvalidPositionException
BoundaryViolationException
public Position parent(Position v) throws InvalidPositionException, BoundaryViolationException
parent
in interface Tree
InvalidPositionException
BoundaryViolationException
public Iterator children(Position v) throws InvalidPositionException
children
in interface Tree
InvalidPositionException
public Iterator positions()
positions
in interface Tree
public Iterator elements()
elements
in interface Tree
public Object replace(Position v, Object o) throws InvalidPositionException
replace
in interface Tree
InvalidPositionException
public Position sibling(Position v) throws InvalidPositionException, BoundaryViolationException
InvalidPositionException
BoundaryViolationException
public Position insertLeft(Position v, Object e) throws InvalidPositionException
InvalidPositionException
public Position insertRight(Position v, Object e) throws InvalidPositionException
InvalidPositionException
public Object remove(Position v) throws InvalidPositionException
InvalidPositionException
public Position addRoot(Object e) throws NonEmptyTreeException
NonEmptyTreeException
public void attach(Position v, BinaryTree T1, BinaryTree T2) throws InvalidPositionException
InvalidPositionException
public void swapElements(Position v, Position w) throws InvalidPositionException
InvalidPositionException
public void expandExternal(Position v, Object l, Object r) throws InvalidPositionException
InvalidPositionException
public void removeAboveExternal(Position v) throws InvalidPositionException
InvalidPositionException
protected BTPosition checkPosition(Position v) throws InvalidPositionException
InvalidPositionException
protected BTPosition createNode(Object element, BTPosition parent, BTPosition left, BTPosition right)
protected void inorderPositions(Position v, List pos) throws InvalidPositionException
InvalidPositionException
|
datastructures | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |