-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryNode.java
More file actions
160 lines (140 loc) · 4.87 KB
/
BinaryNode.java
File metadata and controls
160 lines (140 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import javax.xml.crypto.Data;
class BinaryNode<T>
{
private T data;
private BinaryNode<T> leftChild; // Reference to left child
private BinaryNode<T> rightChild; // Reference to right child
public BinaryNode()
{
this(null); // Call next constructor
} // end default constructor
public BinaryNode(T dataPortion)
{
this(dataPortion, null, null); // Call next constructor
} // end constructor
public BinaryNode(T dataPortion, BinaryNode<T> newLeftChild,
BinaryNode<T> newRightChild)
{
data = dataPortion;
leftChild = newLeftChild;
rightChild = newRightChild;
} // end constructor
/** Retrieves the data portion of this node.
@return The object in the data portion of the node. */
public T getData()
{
return data;
} // end getData
/** Sets the data portion of this node.
@param newData The data object. */
public void setData(T newData)
{
data = newData;
} // end setData
/** Retrieves the left child of this node.
@return A reference to this node's left child. */
public BinaryNode<T> getLeftChild()
{
return leftChild;
} // end getLeftChild
/** Sets this node’s left child to a given node.
@param newLeftChild A node that will be the left child. */
public void setLeftChild(BinaryNode<T> newLeftChild)
{
leftChild = newLeftChild;
} // end setLeftChild
/** Detects whether this node has a left child.
@return True if the node has a left child. */
public boolean hasLeftChild()
{
return leftChild != null;
} // end hasLeftChild
/** Retrieves the right child of this node.
@return A reference to this node's right child. */
public BinaryNode<T> getRightChild()
{
return rightChild;
} // end getRightChild
/** Sets this node’s right child to a given node.
@param newRightChild A node that will be the right child. */
public void setRightChild(BinaryNode<T> newRightChild)
{
rightChild = newRightChild;
} // end setRightChild
/** Detects whether this node has a right child.
@return True if the node has a right child. */
public boolean hasRightChild()
{
return rightChild != null;
} // end hasRightChild
/** Detects whether this node is a leaf.
@return True if the node is a leaf. */
public boolean isLeaf()
{
return (leftChild == null) && (rightChild == null);
} // end isLeaf
/**A Recursion Example in the BinaryNode Class
* Copies the subtree rooted at this node.
@return The root of a copy of the subtree rooted at this node. */
public BinaryNode<T> copy()
{
BinaryNode<T> newRoot = new BinaryNode<>(data);
if (leftChild != null)
newRoot.setLeftChild(leftChild.copy());
if (rightChild != null)
newRoot.setRightChild(rightChild.copy());
return newRoot;
} // end copy
/** --------------------------------------------------------------------
* Part of Task 1 */
/** A Recursive Method in the BinaryNode Class
* prints (using post-order traversal) all nodes of the subtree rooted at "this" node */
public void postorderTraverse_binaryNodeMethod()
{
if(data != null){
if(leftChild != null)
leftChild.postorderTraverse_binaryNodeMethod();
if(rightChild != null)
rightChild.postorderTraverse_binaryNodeMethod();
System.out.println(data);
}
}
/**--------------------------------------------------------------------
* Part of Task 2*/
/** A Recursive Method in the BinaryNode Class
* Computes the height of the subtree rooted at "this" node.
@return The height of the subtree rooted at "this" node. */
public int getHeight_binaryNodeMethod()
{
int height = 0;
int heightLeft = 0;
int heightRight = 0;
if (data != null)
{
if (leftChild != null)
{
heightLeft = leftChild.getHeight_binaryNodeMethod();
}
if (rightChild != null)
{
heightRight= rightChild.getHeight_binaryNodeMethod();
}
height = 1 + Math.max(heightLeft,heightRight);
}
return height;
} // end getHeight
/** -------------------------------------------------------------------- */
/** A Recursive Method in the BinaryNode Class
* Counts the nodes in the subtree rooted at "this" node.
@return The number of nodes in the subtree rooted at "this" node. */
public int getNumberOfNodes_binaryNodeMethod()
{
int leftNumber = 0;
int rightNumber = 0;
if (leftChild != null)
leftNumber = leftChild.getNumberOfNodes_binaryNodeMethod();
if (rightChild != null)
rightNumber = rightChild.getNumberOfNodes_binaryNodeMethod();
return 1 + leftNumber + rightNumber;
} // end getNumberOfNodes
} // end BinaryNode