-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
184 lines (156 loc) · 5.85 KB
/
BinaryTree.java
File metadata and controls
184 lines (156 loc) · 5.85 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
public class BinaryTree<T> implements BinaryTreeInterface<T>
{
private BinaryNode<T> root;
public BinaryTree()
{
root = null;
} // end default constructor
public BinaryTree(T rootData)
{
root = new BinaryNode<>(rootData);
} // end constructor
public BinaryTree(T rootData, BinaryTree<T> leftTree, BinaryTree<T> rightTree)
{
initializeTree(rootData, leftTree, rightTree);
} // end constructor
public void setTree(T rootData, BinaryTreeInterface<T> leftTree,
BinaryTreeInterface<T> rightTree)
{
initializeTree(rootData, (BinaryTree<T>)leftTree,
(BinaryTree<T>)rightTree);
} // end setTree
public void setRootData(T rootData)
{
root.setData(rootData);
} // end setRootData
public T getRootData()
{
if (isEmpty())
throw new EmptyTreeException();
else
return root.getData();
} // end getRootData
public boolean isEmpty()
{
return root == null;
} // end isEmpty
public void clear()
{
root = null;
} // end clear
protected void setRootNode(BinaryNode<T> rootNode)
{
root = rootNode;
} // end setRootNode
protected BinaryNode<T> getRootNode()
{
return root;
} // end getRootNode
private void initializeTree(T rootData, BinaryTree<T> leftTree, BinaryTree<T> rightTree)
{
root = new BinaryNode<>(rootData);
if ((leftTree != null) && !leftTree.isEmpty())
root.setLeftChild(leftTree.root);
if ((rightTree != null) && !rightTree.isEmpty())
{
if (rightTree != leftTree)
root.setRightChild(rightTree.root);
else
root.setRightChild(rightTree.root.copy());
} // end if
if ((leftTree != null) && (leftTree != this))
leftTree.clear();
if ((rightTree != null) && (rightTree != this))
rightTree.clear();
} // end initializeTree
/** -------------------------------------------------------------------- */
/** Task 1: Implement the 4 methods
* . In BinaryTree.java
* 1. public void postorderTraverse();
* 2. private void postorderTraverse(BinaryNode<T> node)
* 3. public void postorderTraverse_callBinaryNodeMethod()
* . In BinaryNode.java
* 4. public void postorderTraverse_binaryNodeMethod() */
/** calls postorderTraverse(BinaryNode<T> node)
* prints (using post-order traversal) all nodes in the "whole" tree */
public void postorderTraverse()
{
postorderTraverse(this.root);
}
/** A Recursive Method in the BinaryTree Class
* prints (using post-order traversal) all nodes in the subtree rooted at this node.*/
public void postorderTraverse(BinaryNode<T> node)
{
if(node != null){
postorderTraverse(node.getLeftChild());
postorderTraverse(node.getRightChild());
System.out.println(node.getData());
}
}
/** The following calls postorderTraverse_binaryNodeMethod(), which is a recursive binaryNode class method
* prints (using post-order traversal) all nodes in the "whole" tree */
public void postorderTraverse_callBinaryNodeMethod()
{
root.postorderTraverse_binaryNodeMethod();
}
/** -------------------------------------------------------------------- */
/** Task 2: Implement the 2 methods
* . In BinaryTree.java
* 1. public int getHeight_callBinaryNodeMethod()
* . In BinaryNode.java
* 2. public int getHeight_binaryNodeMethod()*/
/** calls getHeight(BinaryNode<T> node)
@return The height of the "whole" tree */
public int getHeight()
{
return getHeight(root);
} // end getHeight
/** A Recursive Method in the BinaryTree Class
* Computes the height of the subtree rooted at this node.
@return The height of the subtree rooted at this node. */
private int getHeight(BinaryNode<T> node)
{
int height = 0;
if (node != null)
height = 1 + Math.max(getHeight(node.getLeftChild()),
getHeight(node.getRightChild()));
return height;
} // end getHeight
/** The following calls getHeight_binaryNodeMethod() which is a recursive binaryNode class method
* Computes the height of the "whole" tree.
@return The height of the "whole" tree. */
public int getHeight_callBinaryNodeMethod()
{
return root.getHeight_binaryNodeMethod();
} // end getHeight_callBinaryNodeMethod
/** -------------------------------------------------------------------- */
/** Task 3: Implement the 2 methods
* . In BinaryTree.java
* 1. public int getNumberOfNodes()
* 2. private int getNumberOfNodes(BinaryNode<T> node)*/
/** calls getNumberOfNodes(BinaryNode<T> node)
@return The number of nodes in the "whole" tree */
public int getNumberOfNodes()
{
return getNumberOfNodes(root);
} // end getNumberOfNodes
/** A Recursive Method in the BinaryTree Class
* Counts the nodes in the subtree rooted at this node.
@return The number of nodes in the subtree rooted at this node. */
private int getNumberOfNodes(BinaryNode<T> node)
{
int numOfNodes = 0;
if (node != null)
{
numOfNodes = 1 + getNumberOfNodes(node.getLeftChild()) + getNumberOfNodes(node.getRightChild());
}
return numOfNodes;
} // end getNumberOfNodes
/** The following calls getNumberOfNodes_binaryNodeMethod() which is a recursive binaryNode class method
* Counts the nodes in the "whole" tree
@return The number of nodes in the "whole" tree. */
public int getNumberOfNodes_callBinaryNodeMethod()
{
return root.getNumberOfNodes_binaryNodeMethod();
} // end getNumberOfNodes_callBinaryNodeMethod
} // end BinaryTree