Skip to content

Commit cb5e76d

Browse files
committed
Refactor traversals to return a list instead of printing into console
1 parent 2bcf602 commit cb5e76d

File tree

3 files changed

+80
-48
lines changed

3 files changed

+80
-48
lines changed

src/main/java/dataStructures/binarySearchTree/BinarySearchTree.java

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* successor(T key)
1515
* searchMin()
1616
* searchMax()
17-
* printInorder()
18-
* printPreorder()
19-
* printPostorder()
20-
* printLevelorder()
17+
* getInorder()
18+
* getPreorder()
19+
* getPostorder()
20+
* getLevelorder()
2121
*/
2222
public class BinarySearchTree<T extends Comparable<T>, V> {
2323

@@ -170,83 +170,89 @@ private T successor(Node<T, V> node) {
170170
}
171171

172172
/**
173-
* Prints out in-order traversal of tree rooted at node
173+
* Stores in-order traversal of tree rooted at node into a list
174+
*
174175
* @param node node which the tree is rooted at
175176
*/
176-
private void printInorder(Node<T, V> node) {
177+
private void getInorder(Node<T, V> node, List<String> result) {
177178
if (node == null) {
178179
return;
179180
}
180181

181182
if (node.left != null) {
182-
printInorder(node.left);
183+
getInorder(node.left, result);
183184
}
184185

185-
System.out.print(node.toString() + " ");
186+
result.add(node.toString());
186187

187188
if (node.right != null) {
188-
printInorder(node.right);
189+
getInorder(node.right, result);
189190
}
190191
}
191192

192193
/**
193-
* Prints out pre-order traversal of tree rooted at node
194+
* Stores in-order traversal of tree rooted at node into a list
195+
*
194196
* @param node node which the tree is rooted at
195197
*/
196-
private void printPreorder(Node<T, V> node) {
198+
private void getPreorder(Node<T, V> node, List<String> result) {
197199
if (node == null) {
198200
return;
199201
}
200202

201-
System.out.print(node.toString() + " ");
203+
result.add(node.toString());
202204

203205
if (node.left != null) {
204-
printPreorder(node.left);
206+
getPreorder(node.left, result);
205207
}
206208

207209
if (node.right != null) {
208-
printPreorder(node.right);
210+
getPreorder(node.right, result);
209211
}
210212
}
211213

212214
/**
213-
* Prints out post-order traversal of tree rooted at node
215+
* Stores post-order traversal of tree rooted at node into a list
216+
*
214217
* @param node node which the tree is rooted at
215218
*/
216-
private void printPostorder(Node<T, V> node) {
219+
private void getPostorder(Node<T, V> node, List<String> result) {
217220
if (node == null) {
218221
return;
219222
}
220223

221224
if (node.left != null) {
222-
printPostorder(node.left);
225+
getPostorder(node.left, result);
223226
}
224227

225228
if (node.right != null) {
226-
printPostorder(node.right);
229+
getPostorder(node.right, result);
227230
}
228231

229-
System.out.print(node.toString() + " ");
232+
result.add(node.toString());
230233
}
231234

232235
/**
233-
* Prints out level-order traversal of tree rooted at node
236+
* Stores level-order traversal of tree rooted at node into a list
237+
*
234238
* @param node node which the tree is rooted at
235239
*/
236-
private void printLevelorder(Node<T, V> node) {
240+
private void getLevelorder(Node<T, V> node, List<String> result) {
237241
if (node == null) {
238242
return;
239243
}
240-
Queue<Node<T, V>> q = new LinkedList<>();
241-
q.add(node);
242-
while (!q.isEmpty()) {
243-
Node<T, V> curr = q.poll();
244-
System.out.print(curr.toString() + " ");
245-
if (curr.left != null) {
246-
q.add(curr.left);
244+
245+
Queue<Node<T, V>> queue = new LinkedList<>();
246+
queue.add(node);
247+
while (!queue.isEmpty()) {
248+
Node<T, V> current = queue.poll();
249+
result.add(current.toString());
250+
251+
if (current.left != null) {
252+
queue.add(current.left);
247253
}
248-
if (curr.right != null) {
249-
q.add(curr.right);
254+
if (current.right != null) {
255+
queue.add(current.right);
250256
}
251257
}
252258
}
@@ -364,27 +370,27 @@ public Node<T, V> searchMax() {
364370
}
365371
}
366372

367-
public void printInorder() {
368-
System.out.print("In-order: ");
369-
printInorder(root);
370-
System.out.println();
373+
public List<String> getInorder() {
374+
List<String> result = new ArrayList<>();
375+
getInorder(root, result);
376+
return result;
371377
}
372378

373-
public void printPreorder() {
374-
System.out.print("Pre-order: ");
375-
printPreorder(root);
376-
System.out.println();
379+
public List<String> getPreorder() {
380+
List<String> result = new ArrayList<>();
381+
getPreorder(root, result);
382+
return result;
377383
}
378384

379-
public void printPostorder() {
380-
System.out.print("Post-order: ");
381-
printPostorder(root);
382-
System.out.println();
385+
public List<String> getPostorder() {
386+
List<String> result = new ArrayList<>();
387+
getPostorder(root, result);
388+
return result;
383389
}
384390

385-
public void printLevelorder() {
386-
System.out.print("Level-order: ");
387-
printLevelorder(root);
388-
System.out.println();
391+
public List<String> getLevelorder() {
392+
List<String> result = new ArrayList<>();
393+
getLevelorder(root, result);
394+
return result;
389395
}
390396
}

src/test/java/algorithms/binarySearch/BinarySearchTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import org.junit.Test;
44

5-
import java.util.Arrays;
6-
75
import static org.junit.Assert.assertEquals;
86

97
public class BinarySearchTest {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dataStructures.binarySearchTree;
2+
3+
import org.junit.Test;
4+
5+
public class BinarySearchTreeTest {
6+
@Test
7+
public void insert_shouldCorrectlyInsertNodes() {
8+
// Create BST instance
9+
BinarySearchTree<Integer, String> bst = new BinarySearchTree<>();
10+
11+
// Insert elements
12+
bst.insert(5, "Five");
13+
bst.insert(3, "Three");
14+
bst.insert(7, "Seven");
15+
bst.insert(2, "Two");
16+
bst.insert(4, "Four");
17+
18+
// Expected in-order traversal result
19+
List<Integer> expected = Arrays.asList(2, 3, 4, 5, 7);
20+
21+
// Perform in-order traversal and get result
22+
List<Integer> result = bst.inOrderTraversal();
23+
24+
// Assert that the result matches the expected list
25+
assert result.equals(expected);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)