public class LinkedBinaryTree<E> extends java.lang.Object implements BinaryTree<E>
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.//begin#fragment LinkedBinaryTree| Modifier and Type | Field and Description |
|---|---|
protected BTPosition<E> |
root |
protected int |
size |
| Constructor and Description |
|---|
LinkedBinaryTree()
Creates an empty binary tree.
|
| Modifier and Type | Method and Description |
|---|---|
Position<E> |
addRoot(E e)
Adds a root node to an empty tree
|
void |
attach(Position<E> v,
BinaryTree<E> T1,
BinaryTree<E> T2)
Attaches two trees to be subtrees of an external node.
|
protected BTPosition<E> |
checkPosition(Position<E> v)
If v is a good binary tree node, cast to BTPosition, else throw exception
|
java.lang.Iterable<Position<E>> |
children(Position<E> v)
Returns an iterable collection of the children of a node.
|
protected BTPosition<E> |
createNode(E element,
BTPosition<E> parent,
BTPosition<E> left,
BTPosition<E> right)
Creates a new binary tree node
|
void |
expandExternal(Position<E> v,
E l,
E r)
Expand an external node into an internal node with two external
node children
|
boolean |
hasLeft(Position<E> v)
Returns whether a node has a left child.
|
boolean |
hasRight(Position<E> v)
Returns whether a node has a right child.
|
protected void |
inorderPositions(Position<E> v,
PositionList<Position<E>> pos)
Creates a list storing the the nodes in the subtree of a node,
ordered according to the inorder traversal of the subtree.
|
Position<E> |
insertLeft(Position<E> v,
E e)
Inserts a left child at a given node.
|
Position<E> |
insertRight(Position<E> v,
E e)
Inserts a right child at a given node.
|
boolean |
isEmpty()
Returns whether the tree is empty.
|
boolean |
isExternal(Position<E> v)
Returns whether a node is external.
|
boolean |
isInternal(Position<E> v)
Returns whether a node is internal.
|
boolean |
isRoot(Position<E> v)
Returns whether a node is the root.
|
java.util.Iterator<E> |
iterator()
Returns an iterator of the elements stored at the nodes
|
Position<E> |
left(Position<E> v)
Returns the left child of a node.
|
Position<E> |
parent(Position<E> v)
Returns the parent of a node.
|
java.lang.Iterable<Position<E>> |
positions()
Returns an iterable collection of the tree nodes.
|
protected void |
preorderPositions(Position<E> v,
PositionList<Position<E>> pos)
Creates a list storing the the nodes in the subtree of a node,
ordered according to the preorder traversal of the subtree.
|
E |
remove(Position<E> v)
Removes a node with zero or one child.
|
void |
removeAboveExternal(Position<E> v)
Remove an external node v and replace its parent with v's
sibling
|
E |
replace(Position<E> v,
E o)
Replaces the element at a node.
|
Position<E> |
right(Position<E> v)
Returns the right child of a node.
|
Position<E> |
root()
Returns the root of the tree.
|
Position<E> |
sibling(Position<E> v)
Return the sibling of a node
|
int |
size()
Returns the number of nodes in the tree.
|
void |
swapElements(Position<E> v,
Position<E> w)
Swap the elements at two nodes
|
protected BTPosition<E> root
protected int size
public int size()
public boolean isEmpty()
public boolean isInternal(Position<E> v) throws InvalidPositionException
isInternal in interface Tree<E>InvalidPositionExceptionpublic boolean isExternal(Position<E> v) throws InvalidPositionException
isExternal in interface Tree<E>InvalidPositionExceptionpublic boolean isRoot(Position<E> v) throws InvalidPositionException
isRoot in interface Tree<E>InvalidPositionExceptionpublic boolean hasLeft(Position<E> v) throws InvalidPositionException
hasLeft in interface BinaryTree<E>InvalidPositionExceptionpublic boolean hasRight(Position<E> v) throws InvalidPositionException
hasRight in interface BinaryTree<E>InvalidPositionExceptionpublic Position<E> root() throws EmptyTreeException
root in interface Tree<E>EmptyTreeExceptionpublic Position<E> left(Position<E> v) throws InvalidPositionException, BoundaryViolationException
left in interface BinaryTree<E>InvalidPositionExceptionBoundaryViolationExceptionpublic Position<E> right(Position<E> v) throws InvalidPositionException, BoundaryViolationException
right in interface BinaryTree<E>InvalidPositionExceptionBoundaryViolationExceptionpublic Position<E> parent(Position<E> v) throws InvalidPositionException, BoundaryViolationException
parent in interface Tree<E>InvalidPositionExceptionBoundaryViolationExceptionpublic java.lang.Iterable<Position<E>> children(Position<E> v) throws InvalidPositionException
children in interface Tree<E>InvalidPositionExceptionpublic java.lang.Iterable<Position<E>> positions()
public java.util.Iterator<E> iterator()
public E replace(Position<E> v, E o) throws InvalidPositionException
replace in interface Tree<E>InvalidPositionExceptionpublic Position<E> sibling(Position<E> v) throws InvalidPositionException, BoundaryViolationException
public Position<E> addRoot(E e) throws NonEmptyTreeException
NonEmptyTreeExceptionpublic Position<E> insertLeft(Position<E> v, E e) throws InvalidPositionException
InvalidPositionExceptionpublic Position<E> insertRight(Position<E> v, E e) throws InvalidPositionException
InvalidPositionExceptionpublic E remove(Position<E> v) throws InvalidPositionException
InvalidPositionExceptionpublic void attach(Position<E> v, BinaryTree<E> T1, BinaryTree<E> T2) throws InvalidPositionException
InvalidPositionExceptionpublic void swapElements(Position<E> v, Position<E> w) throws InvalidPositionException
InvalidPositionExceptionpublic void expandExternal(Position<E> v, E l, E r) throws InvalidPositionException
InvalidPositionExceptionpublic void removeAboveExternal(Position<E> v) throws InvalidPositionException
InvalidPositionExceptionprotected BTPosition<E> checkPosition(Position<E> v) throws InvalidPositionException
InvalidPositionExceptionprotected BTPosition<E> createNode(E element, BTPosition<E> parent, BTPosition<E> left, BTPosition<E> right)
protected void preorderPositions(Position<E> v, PositionList<Position<E>> pos) throws InvalidPositionException
InvalidPositionExceptionprotected void inorderPositions(Position<E> v, PositionList<Position<E>> pos) throws InvalidPositionException
InvalidPositionException