public class DoubleBTNode
extends java.lang.Object
DoubleBTNode
provides a node for a binary tree. Each node
contains a piece of data (which is a reference to an object) and references
to a left and right child. The references to children may be null to indicate
that there is no child. The reference stored in a node can also be null.
Limitations:
Beyond Int.MAX_VALUE
elements, treeSize
, is
wrong.
Java Source Code for this class:
http://www.cs.colorado.edu/~main/edu/colorado/nodes/DoubleBTNode.java BTNode
,
BooleanBTNode
,
ByteBTNode
,
CharBTNode
,
FloatBTNode
,
IntBTNode
,
LongBTNode
,
ShortBTNode
Constructor and Description |
---|
DoubleBTNode(double initialData,
DoubleBTNode initialLeft,
DoubleBTNode initialRight)
Initialize a
DoubleBTNode with a specified initial data and links
children. |
Modifier and Type | Method and Description |
---|---|
double |
getData()
Accessor method to get the data from this node.
|
DoubleBTNode |
getLeft()
Accessor method to get a reference to the left child of this node.
|
double |
getLeftmostData()
Accessor method to get the data from the leftmost node of the tree below
this node.
|
DoubleBTNode |
getRight()
Accessor method to get a reference to the right child of this node.
|
double |
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.
|
DoubleBTNode |
removeLeftmost()
Remove the leftmost most node of the tree below this node.
|
DoubleBTNode |
removeRightmost()
Remove the rightmost most node of the tree below this node.
|
void |
setData(double newData)
Modification method to set the data in this node.
|
void |
setLeft(DoubleBTNode newLeft)
Modification method to set the link to the left child of this node.
|
void |
setRight(DoubleBTNode newRight)
Modification method to set the link to the right child of this node.
|
static DoubleBTNode |
treeCopy(DoubleBTNode source)
Copy a binary tree.
|
static int |
treeSize(DoubleBTNode root)
Count the number of nodes in a binary tree.
|
public DoubleBTNode(double initialData, DoubleBTNode initialLeft, DoubleBTNode initialRight)
DoubleBTNode
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.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.public double getData()
public DoubleBTNode getLeft()
public double getLeftmostData()
public double getRightmostData()
public DoubleBTNode getRight()
public void inorderPrint()
System.out.println( )
using an inorder traversal.public boolean isLeaf()
true
(if this node is a leaf) or
false
(if this node is not a leaf.public void preorderPrint()
System.out.println( )
using a preorder traversal.public void postorderPrint()
System.out.println( )
using a postorder traversal.public void print(int depth)
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.public DoubleBTNode removeLeftmost()
public DoubleBTNode removeRightmost()
public void setData(double newData)
newData
- the new data to place in this node
Postcondition:
The data of this node has been set to newData
.public void setLeft(DoubleBTNode newLeft)
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.public void setRight(DoubleBTNode newRight)
newRight
- 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.public static DoubleBTNode treeCopy(DoubleBTNode source)
source
- a reference to the root of a binary tree that will be copied (which may be
an empty tree where source
is null)source
. The return value is a reference to the root of the copy.java.lang.OutOfMemoryError
- Indicates that there is insufficient memory for the new tree.public static int treeSize(DoubleBTNode root)
root
- a reference to the root of a binary tree (which may be
an empty tree where source
is null)INT.MAX_VALUE
.