All Packages  Class Hierarchy  This Package  Previous  Next  Index  

Class structure.BinaryTree

java.lang.Object
    |
    +----structure.BinaryTree

public class BinaryTree
extends Object
A binary tree. Every node refers to as many as two other nodes in the tree. Each node, in addition, as at most one parent. The root node, is the unique node with no parent.

Associated with the binary tree is a cursor. This reference points to a current node within the tree, or it is null. When the tree is first created it is empty, with a null cursor (there is nothing to refer to). When a node is added, it is added at the location indicated by the cursor. The cursor may be moved about with the "move" methods. When the cursor "falls off" the tree at a current leaf, a new value may be added at that location.

Standard pre-, post-, in-, and level-order traversals are provided.


Constructor Index

 o BinaryTree()
Construct an empty binary tree.

Method Index

 o clear()
Remove values from tree.
 o depth()
Returns depth of node in tree.
 o elements()
Returns an iterator that traverses tree in-order.
 o hasLeft()
Check for left child.
 o hasParent()
Checks for parent.
 o hasRight()
Check for right child.
 o height()
Returns height of node in tree.
 o inorderElements()
Returns an iterator that traverses tree in-order.
 o insert(Object)
Add a value to the tree.
 o isComplete()
Detect a complete tree rooted at cursor.
 o isEmpty()
Returns true iff tree has no data.
 o isFull()
Detects a full tree rooted at cursor.
 o isLeftChild()
Checks if cursor is a left child.
 o isRightChild()
Checks if cursor is a right child.
 o moveLeft()
Move cursor to left.
 o moveRight()
Move cursor to right.
 o moveUp()
Moves up the tree.
 o postorderElements()
Returns post-order iterator.
 o preorderElements()
Returns pre-order iterator.
 o remove()
Remove value at cursor.
 o reset()
Sets cursor to root of tree.
 o rotateLeft()
Perform a left rotation about cursor.
 o rotateRight()
Perform a right rotation about cursor.
 o setValue(Object)
Change value of the node referred to by cursor.
 o size()
The number of nodes in the tree.
 o toString()
Constructs string representation of tree.
 o valid()
Checks validity of cursor.
 o value()
Return cursor's value.

Constructors

 o BinaryTree
public BinaryTree()
Construct an empty binary tree. The tree is empty, no root exists, and the cursor is null.

Postcondition:
Creates an empty binary tree

Methods

 o clear
public void clear()
Remove values from tree. The tree is made empty; all memory associated with nodes is freed. Cursor is made null.

Postcondition:
Removes all nodes from tree

 o insert
public void insert(Object value)
Add a value to the tree. The value is added to the location suggested by the cursor. If the cursor and root are both null value is added to the root of the currently empty tree. If root is non-null, the value is added as the left child of the prior node if the cursor became invalid by moving left from the prior node. Otherwise it is made the right child of the prior node.

Precondition:
Cursor is null (invalid)
Postcondition:
If tree is empty, value is inserted at root, otherwise value is inserted where cursor last moved off tree

Parameters:
value - The value to be added to the tree.
 o remove
public Object remove()
Remove value at cursor. Node referenced by cursor should be a leaf. The value at the cursor is removed and the cursor is moved upward to parent, or becomes null if cursor points to root.

Precondition:
Cursor is valid and has no children
Postcondition:
Leaf is removed, cursor is moved to parent, if any

Returns:
The value removed from tree.
 o value
public Object value()
Return cursor's value. Provided the cursor is non-null, this method returns the value stored in the referenced node.

Precondition:
Cursor valid
Postcondition:
Returns value of object at cursor

Returns:
The value associated with the cursor.
 o setValue
public void setValue(Object value)
Change value of the node referred to by cursor.

Precondition:
Cursor valid
Postcondition:
Sets value found at cursor

Parameters:
value - The new value of the cursor-referenced node.
 o reset
public void reset()
Sets cursor to root of tree. If no root exists, cursor remains null.

Postcondition:
Moves the cursor to the root, if any

 o valid
public boolean valid()
Checks validity of cursor. Cursor is valid if it is pointing to an existing node of the tree.

Postcondition:
Returns true if the cursor points to a valid node.

Returns:
True iff the cursor refers to a node of the tree.
 o rotateLeft
public void rotateLeft()
Perform a left rotation about cursor. The relation between the cursor node and its right child are reversed. Left child of right child becomes right child of cursor. Cursor becomes left child of right child.

Precondition:
Cursor is valid, has right child
Postcondition:
Rotates the tree to left about cursor. cursor points to new root of subtree

 o rotateRight
public void rotateRight()
Perform a right rotation about cursor. The relation between the cursor node and its left child are reversed. Right child of left child becomes left child of cursor. Cursor becomes right child of left child.

Precondition:
Cursor is valid, has left child
Postcondition:
Rotates the tree to left about cursor. cursor points to new root of subtree

 o hasLeft
public boolean hasLeft()
Check for left child. If cursor has left child, returns true.

Postcondition:
Returns true iff cursor has left child

Returns:
True iff cursor has left child.
 o hasRight
public boolean hasRight()
Check for right child. If cursor has right child, returns true.

Postcondition:
Returns true iff cursor has right child

Returns:
True iff cursor has right child.
 o hasParent
public boolean hasParent()
Checks for parent. Returns true if cursor has a parent node.

Precondition:
Cursor is valid
Postcondition:
Returns true iff cursor has parent

Returns:
True iff cursor has parent.
 o isLeftChild
public boolean isLeftChild()
Checks if cursor is a left child. If cursor is left child of its parent, returns true.

Postcondition:
Returns true if cursor has parent and is left child

Returns:
True iff cursor is left child of its parent.
 o isRightChild
public boolean isRightChild()
Checks if cursor is a right child. If cursor is a right child of its parent, returns true.

Postcondition:
Returns true if cursor has parent and is right child

Returns:
True iff cursor is a right child of its parent.
 o moveLeft
public void moveLeft()
Move cursor to left. If cursor is valid, cursor refers to left child of current node, or null, if none.

Precondition:
Cursor is valid
Postcondition:
Cursor moves to left child of pre-cursor, or off tree

 o moveRight
public void moveRight()
Move cursor to right. If cursor is valid, cursor refers to right child of current node, or null, if none.

Precondition:
Cursor is valid
Postcondition:
Cursor moves to right child of pre-cursor, or off tree

 o moveUp
public void moveUp()
Moves up the tree. If cursor has parent, cursor points to parent. Otherwise cursor becomes null.

Precondition:
Cursor is valid
Postcondition:
Cursor moves up to parent of pre-cursor.

 o height
public int height()
Returns height of node in tree. Height is the maximum number of edges between current node and any descendant.

Postcondition:
Returns height of cursor in tree or -1 if tree is empty

Returns:
Height of node in tree.
 o depth
public int depth()
Returns depth of node in tree. Depth is the number of edges traversed in unique path to root.

Postcondition:
Returns depth of cursor in tree or -1 if tree is empty

Returns:
Depth of cursor node in tree.
 o isFull
public boolean isFull()
Detects a full tree rooted at cursor. A tree is full if adding a node to the tree would increase its height.

Postcondition:
Returns true iff subtree rooted at cursor is full

Returns:
True iff adding a node to subtree requires increasing height.
 o isComplete
public boolean isComplete()
Detect a complete tree rooted at cursor. A tree is complete if it is minimum height and any "holes" in the tree appear only to the right in last level.

Postcondition:
Returns true iff subtree rooted at cursor is complete

Returns:
True iff tree is complete.
 o isEmpty
public boolean isEmpty()
Returns true iff tree has no data.

Postcondition:
Returns true iff tree is empty

Returns:
True iff tree has no data.
 o size
public int size()
The number of nodes in the tree.

Postcondition:
Returns number of nodes in tree

Returns:
The number of nodes in the tree.
 o elements
public Iterator elements()
Returns an iterator that traverses tree in-order.

Postcondition:
Returns inorder traversal of tree

Returns:
An Iterator for traversing tree in order.
 o inorderElements
public Iterator inorderElements()
Returns an iterator that traverses tree in-order. The root of a subtree is visited after all the nodes of the left subtree, and before all the nodes of the right subtree.

Postcondition:
Returns inorder traversal of tree

Returns:
An Iterator for traversing tree in order.
 o preorderElements
public Iterator preorderElements()
Returns pre-order iterator. The iterator considers the root of a subtree before any of its values.

Postcondition:
Returns preorder traversal of tree

Returns:
Iterator traversing tree in pre-order.
 o postorderElements
public Iterator postorderElements()
Returns post-order iterator. The iterator considers the root of a subtree after any of its values.

Postcondition:
Returns postorder traversal of tree

Returns:
Iterator traversing tree in post-order.
 o toString
public String toString()
Constructs string representation of tree.

Postcondition:
Returns string representation of tree

Returns:
String describing tree.
Overrides:
toString in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index