class BinaryNode
1: package TreePackage;
2: /**
3: A class that represents nodes in a binary tree.
4:
5: @author Frank M. Carrano
6: @author Timothy M. Henry
7: @version 5.0
8: */
9: class BinaryNode<T>
10: {
11: private T data;
12: private BinaryNode<T> leftChild; // Reference to left child
13: private BinaryNode<T> rightChild; // Reference to right child
15: public BinaryNode()
16: {
17: this(null); // Call next constructor
18: } // end default constructor
20: public BinaryNode(T dataPortion)
21: {
22: this(dataPortion, null, null); // Call next constructor
23: } // end constructor
25: public BinaryNode(T dataPortion, BinaryNode<T> newLeftChild,
26: BinaryNode<T> newRightChild)
27: {
28: data = dataPortion;
29: leftChild = newLeftChild;
30: rightChild = newRightChild;
31: } // end constructor
33: /** Retrieves the data portion of this node.
34: @return The object in the data portion of the node. */
35: public T getData()
36: {
37: return data;
38: } // end getData
40: /** Sets the data portion of this node.
41: @param newData The data object. */
42: public void setData(T newData)
43: {
44: data = newData;
45: } // end setData
47: /** Retrieves the left child of this node.
48: @return A reference to this node's left child. */
49: public BinaryNode<T> getLeftChild()
50: {
51: return leftChild;
52: } // end getLeftChild
54: /** Sets this node’s left child to a given node.
55: @param newLeftChild A node that will be the left child. */
56: public void setLeftChild(BinaryNode<T> newLeftChild)
57: {
58: leftChild = newLeftChild;
59: } // end setLeftChild
61: /** Detects whether this node has a left child.
62: @return True if the node has a left child. */
63: public boolean hasLeftChild()
64: {
65: return leftChild != null;
66: } // end hasLeftChild
68: /* Implementations of getRightChild, setRightChild, and hasRightChild
69: are here and are analogous to their left-child counterparts. */
70:
71: /** Detects whether this node is a leaf.
72: @return True if the node is a leaf. */
73: public boolean isLeaf()
74: {
75: return (leftChild == null) && (rightChild == null);
76: } // end isLeaf
77:
78: /** Counts the nodes in the subtree rooted at this node.
79: @return The number of nodes in the subtree rooted at this node. */
80: public int getNumberOfNodes()
81: {
82: // < See Segment 25.10. >
83: } // end getNumberOfNodes
84:
85: /** Computes the height of the subtree rooted at this node.
86: @return The height of the subtree rooted at this node. */
87: public int getHeight()
88: {
89: // < See Segment 25.10. >
90: } // end getHeight
92: /** Copies the subtree rooted at this node.
93: @return The root of a copy of the subtree rooted at this node. */
94: public BinaryNode<T> copy()
95: {
96: // < See Segment 25.5. >
97: } // end copy
98: } // end BinaryNode