|
APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectap.TreeNode
The class for nodes in a binary tree that will be
used and tested on the
Advanced Placement Computer Science exam.
For example, the question below, similar to
Question 14 on the 1992 released AB exam, shows
many of the TreeNode
methods in use.
Consider methods m
and f
below. If the precondition for f
is satisfied,
which of the following is the best postcondition?
f
f
.
public class T { public static Object m(Object a, Object b) { if ( ( (Comparable) a).compareTo(b) >= 0) { return a; } else { return b; } } // precondition: t != null, that is t is not an empty tree // postcondition: public static Object f(TreeNode t) { if (t.getLeft() == null && t.getRight() == null) { return t.getValue(); } else if (t.getLeft() == null) { return m(t.getValue(), f(t.getRight())); } else if (t.getRight() == null) { return m(t.getValue(), f(t.getLeft())); } else { return m(m(t.getValue(), f(t.getLeft())), f(t.getRight())); } } }
Note: this class has nothing to do
with the standard Java interface javax.swing.tree.TreeNode
.
This implementation is provided at apcentral.
Constructor Summary | |
TreeNode(java.lang.Object initValue)
Construct a tree node with specified value, with null left and right subtrees. |
|
TreeNode(java.lang.Object initValue,
TreeNode initLeft,
TreeNode initRight)
Construct a tree node with specified value, left, and right subtrees. |
Method Summary | |
TreeNode |
getLeft()
Returns the left subtree of this node. |
TreeNode |
getRight()
Returns the right subtree of this node. |
java.lang.Object |
getValue()
Returns the value stored in this tree node. |
void |
setLeft(TreeNode theNewLeft)
Sets the value of the left subtree of this node. |
void |
setRight(TreeNode theNewRight)
Sets the value of the right subtree of this node. |
void |
setValue(java.lang.Object theNewValue)
Sets the value of this tree node. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public TreeNode(java.lang.Object initValue)
initValue
- is the value stored in the nodepublic TreeNode(java.lang.Object initValue, TreeNode initLeft, TreeNode initRight)
initValue
- is the value stored in the nodeinitLeft
- is the left subtree of the nodeinitRight
- is the right subtree of the nodeMethod Detail |
public java.lang.Object getValue()
public TreeNode getLeft()
public TreeNode getRight()
public void setValue(java.lang.Object theNewValue)
theNewValue
- is the (new) value stored in this nodepublic void setLeft(TreeNode theNewLeft)
theNewLeft
- is the (new) left subtree of this nodepublic void setRight(TreeNode theNewRight)
theNewRight
- is the (new) right subtree of this node
|
unofficial documentation for the APCS Java Subset | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |