http://www.cs.colorado.edu/~main/edu/colorado/nodes/BTNode.java
|
Constructor Summary |
BTNode(E initialData,
BTNode<E> initialLeft,
BTNode<E> initialRight)
Initialize a BTNode with a specified initial data and links
children. |
|
Method Summary |
E |
getData()
Accessor method to get the data from this node. |
BTNode<E> |
getLeft()
Accessor method to get a reference to the left child of this node. |
E |
getLeftmostData()
Accessor method to get the data from the leftmost node of the tree below
this node. |
BTNode<E> |
getRight()
Accessor method to get a reference to the right child of this node. |
E |
getRightmostData()
Accessor method to get the data from the rightmost node of the tree below
this node. |
void |
inorderPrint()
Uses an inorder traversal to print the data from each node at or below
this node of the binary tree. |
boolean |
isLeaf()
Accessor method to determine whether a node is a leaf. |
void |
postorderPrint()
Uses a postorder traversal to print the data from each node at or below
this node of the binary tree. |
void |
preorderPrint()
Uses a preorder traversal to print the data from each node at or below
this node of the binary tree. |
void |
print(int depth)
Uses an inorder traversal to print the data from each node at or below
this node of the binary tree, with indentations to indicate the depth
of each node. |
BTNode<E> |
removeLeftmost()
Remove the leftmost most node of the tree with this node as its root. |
BTNode<E> |
removeRightmost()
Remove the rightmost most node of the tree with this node as its root. |
void |
setData(E newData)
Modification method to set the data in this node. |
void |
setLeft(BTNode<E> newLeft)
Modification method to set the link to the left child of this node. |
void |
setRight(BTNode<E> newRight)
Modification method to set the link to the right child of this node. |
static
|
treeCopy(BTNode<E> source)
Copy a binary tree. |
static
|
treeSize(BTNode<E> root)
Count the number of nodes in a binary tree. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
BTNode
public BTNode(E initialData,
BTNode<E> initialLeft,
BTNode<E> initialRight)
- Initialize a
BTNode with a specified initial data and links
children. Note that a child link may be the null reference,
which indicates that the new node does not have that child.
- Parameters:
initialData - the initial data of this new nodeinitialLeft - a reference to the left child of this new node--this reference may be null
to indicate that there is no node after this new node.initialRight - a reference to the right child of this new node--this reference may be null
to indicate that there is no node after this new node.
- Postcondition:
-
This node contains the specified data and links to its children.
getData
public E getData()
- Accessor method to get the data from this node.
- Parameters:
- - none
- Returns:
- the data from this node
getLeft
public BTNode<E> getLeft()
- Accessor method to get a reference to the left child of this node.
- Parameters:
- - none
- Returns:
- a reference to the left child of this node (or the null reference if there
is no left child)
getLeftmostData
public E getLeftmostData()
- Accessor method to get the data from the leftmost node of the tree below
this node.
- Parameters:
- - none
- Returns:
- the data from the deepest node that can be reached from this node by
following left links.
getRight
public BTNode<E> getRight()
- Accessor method to get a reference to the right child of this node.
- Parameters:
- - none
- Returns:
- a reference to the right child of this node (or the null reference if there
is no right child)
getRightmostData
public E getRightmostData()
- Accessor method to get the data from the rightmost node of the tree below
this node.
- Parameters:
- - none
- Returns:
- the data from the deepest node that can be reached from this node by
following right links.
inorderPrint
public void inorderPrint()
- Uses an inorder traversal to print the data from each node at or below
this node of the binary tree.
- Parameters:
- - none
- Postcondition:
-
The data of this node and all its descendants have been writeen by
System.out.println( ) using an inorder traversal.
isLeaf
public boolean isLeaf()
- Accessor method to determine whether a node is a leaf.
- Parameters:
- - none
- Returns:
true (if this node is a leaf) or
false (if this node is not a leaf.
preorderPrint
public void preorderPrint()
- Uses a preorder traversal to print the data from each node at or below
this node of the binary tree.
- Parameters:
- - none
- Postcondition:
-
The data of this node and all its descendants have been writeen by
System.out.println( ) using a preorder traversal.
postorderPrint
public void postorderPrint()
- Uses a postorder traversal to print the data from each node at or below
this node of the binary tree.
- Parameters:
- - none
- Postcondition:
-
The data of this node and all its descendants have been writeen by
System.out.println( ) using a postorder traversal.
print
public void print(int depth)
- Uses an inorder traversal to print the data from each node at or below
this node of the binary tree, with indentations to indicate the depth
of each node.
- Parameters:
depth - the depth of this node (with 0 for root, 1 for the root's
children, and so on)(
- Precondition:
-
depth is the depth of this node.
- Postcondition:
-
The data of this node and all its descendants have been writeen by
System.out.println( ) using an inorder traversal.
The indentation of each line of data is four times its depth in the
tree. A dash "--" is printed at any place where a child has no
sibling.
removeLeftmost
public BTNode<E> removeLeftmost()
- Remove the leftmost most node of the tree with this node as its root.
- Parameters:
- - none
- Postcondition:
-
The tree starting at this node has had its leftmost node removed (i.e.,
the deepest node that can be reached by following left links). The
return value is a reference to the root of the new (smaller) tree.
This return value could be null if the original tree had only one
node (since that one node has now been removed).
removeRightmost
public BTNode<E> removeRightmost()
- Remove the rightmost most node of the tree with this node as its root.
- Parameters:
- - none
- Postcondition:
-
The tree starting at this node has had its rightmost node removed (i.e.,
the deepest node that can be reached by following right links). The
return value is a reference to the root of the new (smaller) tree.
This return value could be null if the original tree had only one
node (since that one node has now been removed).
setData
public void setData(E newData)
- Modification method to set the data in this node.
- Parameters:
newData - the new data to place in this node
- Postcondition:
-
The data of this node has been set to
newData.
setLeft
public void setLeft(BTNode<E> newLeft)
- Modification method to set the link to the left child of this node.
- Parameters:
newLeft - a reference to the node that should appear as the left child of this node
(or the null reference if there is no left child for this node)
- Postcondition:
-
The link to the left child of this node has been set to
newLeft.
Any other node (that used to be the left child) is no longer connected to
this node.
setRight
public void setRight(BTNode<E> newRight)
- Modification method to set the link to the right child of this node.
- Parameters:
newLeft - a reference to the node that should appear as the right child of this node
(or the null reference if there is no right child for this node)
- Postcondition:
-
The link to the right child of this node has been set to
newRight.
Any other node (that used to be the right child) is no longer connected to
this node.
treeCopy
public static <E> BTNode<E> treeCopy(BTNode<E> source)
- Copy a binary tree.
- Parameters:
source - a reference to the root of a binary tree that will be copied (which may be
an empty tree where source is null)
- Returns:
- The method has made a copy of the binary tree starting at
source. The return value is a reference to the root of the copy.
- Throws:
java.lang.OutOfMemoryError - Indicates that there is insufficient memory for the new tree.
treeSize
public static <E> long treeSize(BTNode<E> root)
- Count the number of nodes in a binary tree.
- Parameters:
root - a reference to the root of a binary tree (which may be
an empty tree where source is null)
- Returns:
- the number of nodes in the binary tree
- Note:
-
A wrong answer occurs for trees larger than
INT.MAX_VALUE.