1
1
/**
2
- * This entire class is used to build a Binary Tree data structure.
3
- * There is the Node Class and the Tree Class, both explained below.
4
- *
5
- * @author Unknown
6
- *
7
- */
2
+ * This entire class is used to build a Binary Tree data structure.
3
+ * There is the Node Class and the Tree Class, both explained below.
4
+ *
5
+ * @author Unknown
6
+ *
7
+ */
8
8
9
9
10
10
/**
11
- * This class implements the nodes that will go on the Binary Tree.
12
- * They consist of the data in them, the node to the left, the node
13
- * to the right, and the parent from which they came from.
14
- *
15
- * @author Unknown
16
- *
17
- */
11
+ * This class implements the nodes that will go on the Binary Tree.
12
+ * They consist of the data in them, the node to the left, the node
13
+ * to the right, and the parent from which they came from.
14
+ *
15
+ * @author Unknown
16
+ *
17
+ */
18
18
class Node {
19
19
/** Data for the node */
20
20
public int data ;
@@ -26,10 +26,10 @@ class Node{
26
26
public Node parent ;
27
27
28
28
/**
29
- * Constructor of Node
30
- *
31
- * @param value Value to put in the node
32
- */
29
+ * Constructor of Node
30
+ *
31
+ * @param value Value to put in the node
32
+ */
33
33
public Node (int value ){
34
34
data = value ;
35
35
left = null ;
@@ -40,56 +40,54 @@ public Node(int value){
40
40
41
41
42
42
/**
43
- * A binary tree is a data structure in which an element
44
- * has two successors(children). The left child is usually
45
- * smaller than the parent, and the right child is usually
46
- * bigger.
47
- *
48
- * @author Unknown
49
- *
50
- */
43
+ * A binary tree is a data structure in which an element
44
+ * has two successors(children). The left child is usually
45
+ * smaller than the parent, and the right child is usually
46
+ * bigger.
47
+ *
48
+ * @author Unknown
49
+ *
50
+ */
51
51
class Tree {
52
52
/** The root of the Binary Tree */
53
53
private Node root ;
54
54
55
55
/**
56
- * Constructor
57
- */
56
+ * Constructor
57
+ */
58
58
public Tree (){
59
59
root = null ;
60
60
}
61
-
61
+
62
62
/**
63
- * Method to find a Node with a certain value
64
- *
65
- * @param key Value being looked for
66
- * @return The node if it finds it, otherwise returns the parent
67
- */
68
- public Node find (int key ){
63
+ * Method to find a Node with a certain value
64
+ *
65
+ * @param key Value being looked for
66
+ * @return The node if it finds it, otherwise returns the parent
67
+ */
68
+ public Node find (int key ) {
69
69
Node current = root ;
70
- Node last = root ;
71
- while (current != null ){
72
- last = current ;
73
- if (key < current .data )
70
+ while (current != null ) {
71
+ if (key < current .data ) {
74
72
current = current .left ;
75
- else if (key > current .data )
73
+ } else if (key > current .data ) {
76
74
current = current .right ;
77
- //If you find the value return it
78
- else
75
+ } else { // If you find the value return it
79
76
return current ;
77
+ }
80
78
}
81
- return last ;
79
+ return null ;
82
80
}
83
81
84
82
/**
85
- * Inserts certain value into the Binary Tree
86
- *
87
- * @param value Value to be inserted
88
- */
83
+ * Inserts certain value into the Binary Tree
84
+ *
85
+ * @param value Value to be inserted
86
+ */
89
87
public void put (int value ){
90
88
Node newNode = new Node (value );
91
89
if (root == null )
92
- root = newNode ;
90
+ root = newNode ;
93
91
else {
94
92
//This will return the soon to be parent of the value you're inserting
95
93
Node parent = find (value );
@@ -109,29 +107,29 @@ public void put(int value){
109
107
}
110
108
111
109
/**
112
- * Deletes a given value from the Binary Tree
113
- *
114
- * @param value Value to be deleted
115
- * @return If the value was deleted
116
- */
110
+ * Deletes a given value from the Binary Tree
111
+ *
112
+ * @param value Value to be deleted
113
+ * @return If the value was deleted
114
+ */
117
115
public boolean remove (int value ){
118
116
//temp is the node to be deleted
119
117
Node temp = find (value );
120
118
121
119
//If the value doesn't exist
122
120
if (temp .data != value )
123
- return false ;
121
+ return false ;
124
122
125
123
//No children
126
124
if (temp .right == null && temp .left == null ){
127
125
if (temp == root )
128
- root = null ;
126
+ root = null ;
129
127
130
128
//This if/else assigns the new node to be either the left or right child of the parent
131
129
else if (temp .parent .data < temp .data )
132
- temp .parent .right = null ;
130
+ temp .parent .right = null ;
133
131
else
134
- temp .parent .left = null ;
132
+ temp .parent .left = null ;
135
133
return true ;
136
134
}
137
135
@@ -162,9 +160,9 @@ else if(temp.left != null && temp.right != null){
162
160
163
161
//This if/else assigns the new node to be either the left or right child of the parent
164
162
if (temp .parent .data < temp .data )
165
- temp .parent .right = successor ;
163
+ temp .parent .right = successor ;
166
164
else
167
- temp .parent .left = successor ;
165
+ temp .parent .left = successor ;
168
166
return true ;
169
167
}
170
168
}
@@ -175,96 +173,96 @@ else if(temp.left != null && temp.right != null){
175
173
if (temp == root ){
176
174
root = temp .right ; return true ;}
177
175
178
- temp .right .parent = temp .parent ;
176
+ temp .right .parent = temp .parent ;
179
177
180
- //Assigns temp to left or right child
181
- if (temp .data < temp .parent .data )
178
+ //Assigns temp to left or right child
179
+ if (temp .data < temp .parent .data )
182
180
temp .parent .left = temp .right ;
183
- else
181
+ else
184
182
temp .parent .right = temp .right ;
185
- return true ;
183
+ return true ;
184
+ }
185
+ //If it has a left child
186
+ else {
187
+ if (temp == root ){
188
+ root = temp .left ; return true ;}
189
+
190
+ temp .left .parent = temp .parent ;
191
+
192
+ //Assigns temp to left or right side
193
+ if (temp .data < temp .parent .data )
194
+ temp .parent .left = temp .left ;
195
+ else
196
+ temp .parent .right = temp .left ;
197
+ return true ;
198
+ }
199
+ }
186
200
}
187
- //If it has a left child
188
- else {
189
- if (temp == root ){
190
- root = temp .left ; return true ;}
191
201
192
- temp .left .parent = temp .parent ;
202
+ /**
203
+ * This method finds the Successor to the Node given.
204
+ * Move right once and go left down the tree as far as you can
205
+ *
206
+ * @param n Node that you want to find the Successor of
207
+ * @return The Successor of the node
208
+ */
209
+ public Node findSuccessor (Node n ){
210
+ if (n .right == null )
211
+ return n ;
212
+ Node current = n .right ;
213
+ Node parent = n .right ;
214
+ while (current != null ){
215
+ parent = current ;
216
+ current = current .left ;
217
+ }
218
+ return parent ;
219
+ }
193
220
194
- //Assigns temp to left or right side
195
- if (temp .data < temp .parent .data )
196
- temp .parent .left = temp .left ;
197
- else
198
- temp .parent .right = temp .left ;
199
- return true ;
221
+ /**
222
+ * Returns the root of the Binary Tree
223
+ *
224
+ * @return the root of the Binary Tree
225
+ */
226
+ public Node getRoot (){
227
+ return root ;
200
228
}
201
- }
202
- }
203
229
204
- /**
205
- * This method finds the Successor to the Node given.
206
- * Move right once and go left down the tree as far as you can
207
- *
208
- * @param n Node that you want to find the Successor of
209
- * @return The Successor of the node
210
- */
211
- public Node findSuccessor (Node n ){
212
- if (n .right == null )
213
- return n ;
214
- Node current = n .right ;
215
- Node parent = n .right ;
216
- while (current != null ){
217
- parent = current ;
218
- current = current .left ;
219
- }
220
- return parent ;
221
- }
230
+ /**
231
+ * Prints leftChild - root - rightChild
232
+ *
233
+ * @param localRoot The local root of the binary tree
234
+ */
235
+ public void inOrder (Node localRoot ){
236
+ if (localRoot != null ){
237
+ inOrder (localRoot .left );
238
+ System .out .print (localRoot .data + " " );
239
+ inOrder (localRoot .right );
240
+ }
241
+ }
222
242
223
- /**
224
- * Returns the root of the Binary Tree
225
- *
226
- * @return the root of the Binary Tree
227
- */
228
- public Node getRoot (){
229
- return root ;
230
- }
243
+ /**
244
+ * Prints root - leftChild - rightChild
245
+ *
246
+ * @param localRoot The local root of the binary tree
247
+ */
248
+ public void preOrder (Node localRoot ){
249
+ if (localRoot != null ){
250
+ System .out .print (localRoot .data + " " );
251
+ preOrder (localRoot .left );
252
+ preOrder (localRoot .right );
253
+ }
254
+ }
231
255
232
- /**
233
- * Prints leftChild - root - rightChild
234
- *
235
- * @param localRoot The local root of the binary tree
236
- */
237
- public void inOrder (Node localRoot ){
238
- if (localRoot != null ){
239
- inOrder (localRoot .left );
240
- System .out .print (localRoot .data + " " );
241
- inOrder (localRoot .right );
242
- }
243
- }
244
-
245
- /**
246
- * Prints root - leftChild - rightChild
247
- *
248
- * @param localRoot The local root of the binary tree
249
- */
250
- public void preOrder (Node localRoot ){
251
- if (localRoot != null ){
252
- System .out .print (localRoot .data + " " );
253
- preOrder (localRoot .left );
254
- preOrder (localRoot .right );
255
- }
256
- }
257
-
258
- /**
259
- * Prints rightChild - leftChild - root
260
- *
261
- * @param localRoot The local root of the binary tree
262
- */
263
- public void postOrder (Node localRoot ){
264
- if (localRoot != null ){
265
- postOrder (localRoot .left );
266
- postOrder (localRoot .right );
267
- System .out .print (localRoot .data + " " );
256
+ /**
257
+ * Prints rightChild - leftChild - root
258
+ *
259
+ * @param localRoot The local root of the binary tree
260
+ */
261
+ public void postOrder (Node localRoot ){
262
+ if (localRoot != null ){
263
+ postOrder (localRoot .left );
264
+ postOrder (localRoot .right );
265
+ System .out .print (localRoot .data + " " );
266
+ }
267
+ }
268
268
}
269
- }
270
- }
0 commit comments