-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSc2720_binaryTree.java
More file actions
226 lines (179 loc) · 4.94 KB
/
CSc2720_binaryTree.java
File metadata and controls
226 lines (179 loc) · 4.94 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Program Name: Lab 11/binaryTree
* Name: Stephanie L. Wyckoff
* Date: 4/1/2017
* Course: Data Structures Lab
* Professor: Duan
* TA: Pelin Icer, Shubin Wu
*
* Description: Constructor binaryTree class which sorts through binary trees using either preorder, inorder or postorder and also calculates the height of the tree
*
* Input: n/a
*
* Output: n/a
*/
public class binaryTree {
protected btNode root;
/* Constructor */
public binaryTree()
{
root = null;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return root == null;
}
/* Functions to insert data */
public void insert(int data)
{
root = insert(root, data);
}
/* Function to insert data recursively */
private btNode insert(btNode node, int data)
{
if (node == null)
node = new btNode(data);
else
{
if (data <= node.getData())
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
}
return node;
}
/* Function for preorder traversal */
public void preorder()
{
preorder(root); //calls private preorder(btNode r)
}
private void preorder(btNode r)
{
if (r != null)
{
System.out.print(r.getData() +" "); //visits root
preorder(r.getLeft()); //visit left
preorder(r.getRight()); //visti right
}
}
public void postorder()
{
postorder(root); //calls private postorder(btNode)
}
public void inorder()
{
inorder(root); //calls private inorder(btNode root)
}
private void inorder(btNode root)
{
//TO DO by students
if(root != null){
inorder(root.getLeft()); //visit left
System.out.print(root.getData() + ", "); //visit root
inorder(root.getRight()); //visit right
}
}
public int findSmallest()
{
return findSmallest(root);
}
private int findSmallest(btNode root)
{
btNode curr = root;
while(curr.left != null){
curr = curr.left;
}
return curr.data;
}
private void postorder(btNode root)
{
//TO DO by students
if(root != null){
postorder(root.getRight()); //visit right
postorder(root.getLeft()); //visit left
System.out.println(root.getData() + " "); //visit root
}
}
public int findHeight(btNode root) {
// TO DO by students
if(root == null){
return 0;
}
int lh = findHeight(root.getLeft()); //height of left side
int rh = findHeight(root.getRight()); //height of right side
return Math.max(lh,rh)+1; //height of tree
}
public boolean remove(int num) {
if (root == null)
return false;
else {
if (root.getData() == num) {
btNode root2 = new btNode();
root2.setLeft(root);
boolean result = root.remove(num, root2);
root = root2.getLeft();
return result;
} else {
return root.remove(num, null);
}
}
}
public void delete(int m){
root = delete(root, m);
}
private btNode delete(btNode b, int m){
if(b == null){
return b;
}
//if b does not exist in tree
if(search(b,m) == null){
return null;
}
else{
//if theres no left child but is a right child, set root = right
if(b.getLeft() == null && b.getRight() != null){
b = b.getRight();
}
//if theres no right child but is a left child, set root = left
else if(b.getRight() == null && b.getLeft() != null){
b = b.getLeft();
}
//if theres 2 children, b = max of left/right, delete max of left/right
else if(b.getLeft() != null && b.getRight() != null){
m=Math.max(b.getLeft().getData(), b.getRight().getData());
b.setData(m);
delete(b, m);
}
}
return null;
}
public btNode search(int m){
return search(root, m);
}
private btNode search(btNode r, int m){
btNode curr = r;
//if found
if(curr.getData() == m){
return curr;
}
else if(curr.getData() >= m){
return search(curr.getLeft(), m);//left subtree result
}
else if(curr.getData() <= m){
return search(curr.getRight(), m);//right subtree result
}
return null; //if tree is empty
}
public int countNodes(){
return countNodes(root);
}
private int countNodes(btNode r){
//if empty
if(r == null){
return 0;
}
//else, return recursive count of left and right + 1
return 1+countNodes(r.getLeft())+countNodes(r.getRight());
}
}