Skip to content

Commit 7d5b77a

Browse files
committed
[JAVA-9909] Fix code for DFS article
1 parent c9411a6 commit 7d5b77a

File tree

2 files changed

+27
-63
lines changed

2 files changed

+27
-63
lines changed

Diff for: algorithms-searching/src/main/java/com/baeldung/algorithms/dfs/BinaryTree.java

+24-52
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.baeldung.algorithms.dfs;
22

3-
import java.util.LinkedList;
4-
import java.util.Queue;
53
import java.util.Stack;
64

75
public class BinaryTree {
@@ -124,69 +122,43 @@ public void traversePostOrder(Node node) {
124122
}
125123
}
126124

127-
public void traverseLevelOrder() {
128-
if (root == null) {
129-
return;
130-
}
131-
132-
Queue<Node> nodes = new LinkedList<>();
133-
nodes.add(root);
134-
135-
while (!nodes.isEmpty()) {
136-
137-
Node node = nodes.remove();
138125

139-
System.out.print(" " + node.value);
140-
141-
if (node.left != null) {
142-
nodes.add(node.left);
143-
}
144-
145-
if (node.left != null) {
146-
nodes.add(node.right);
147-
}
148-
}
149-
}
150-
151-
152126
public void traverseInOrderWithoutRecursion() {
153-
Stack<Node> stack = new Stack<Node>();
127+
Stack<Node> stack = new Stack<>();
154128
Node current = root;
155-
stack.push(root);
156-
while(! stack.isEmpty()) {
157-
while(current.left != null) {
158-
current = current.left;
159-
stack.push(current);
160-
}
161-
current = stack.pop();
162-
visit(current.value);
163-
if(current.right != null) {
164-
current = current.right;
129+
130+
while (current != null || !stack.isEmpty()) {
131+
while (current != null) {
165132
stack.push(current);
133+
current = current.left;
166134
}
135+
136+
Node top = stack.pop();
137+
visit(top.value);
138+
current = top.right;
167139
}
168140
}
169-
141+
170142
public void traversePreOrderWithoutRecursion() {
171-
Stack<Node> stack = new Stack<Node>();
172-
Node current = root;
143+
Stack<Node> stack = new Stack<>();
144+
Node current;
173145
stack.push(root);
174146
while(! stack.isEmpty()) {
175147
current = stack.pop();
176148
visit(current.value);
177-
149+
178150
if(current.right != null)
179151
stack.push(current.right);
180-
152+
181153
if(current.left != null)
182154
stack.push(current.left);
183-
}
155+
}
184156
}
185-
157+
186158
public void traversePostOrderWithoutRecursion() {
187-
Stack<Node> stack = new Stack<Node>();
159+
Stack<Node> stack = new Stack<>();
188160
Node prev = root;
189-
Node current = root;
161+
Node current;
190162
stack.push(root);
191163

192164
while (!stack.isEmpty()) {
@@ -206,14 +178,14 @@ public void traversePostOrderWithoutRecursion() {
206178
stack.push(current.left);
207179
}
208180
}
209-
}
210-
}
211-
181+
}
182+
}
183+
212184
private void visit(int value) {
213-
System.out.print(" " + value);
185+
System.out.print(" " + value);
214186
}
215-
216-
class Node {
187+
188+
static class Node {
217189
int value;
218190
Node left;
219191
Node right;

Diff for: algorithms-searching/src/test/java/com/baeldung/algorithms/dfs/BinaryTreeUnitTest.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.baeldung.algorithms.dfs;
22

3+
import org.junit.Test;
4+
35
import static org.junit.Assert.assertEquals;
46
import static org.junit.Assert.assertFalse;
57
import static org.junit.Assert.assertTrue;
68

7-
import org.junit.Test;
8-
99
public class BinaryTreeUnitTest {
1010

1111
@Test
1212
public void givenABinaryTree_WhenAddingElements_ThenTreeNotEmpty() {
1313

1414
BinaryTree bt = createBinaryTree();
1515

16-
assertTrue(!bt.isEmpty());
16+
assertFalse(bt.isEmpty());
1717
}
1818

1919
@Test
@@ -111,14 +111,6 @@ public void givenABinaryTree_WhenTraversingPostOrder_ThenPrintValues() {
111111
bt.traversePostOrderWithoutRecursion();
112112
}
113113

114-
@Test
115-
public void givenABinaryTree_WhenTraversingLevelOrder_ThenPrintValues() {
116-
117-
BinaryTree bt = createBinaryTree();
118-
119-
bt.traverseLevelOrder();
120-
}
121-
122114
private BinaryTree createBinaryTree() {
123115
BinaryTree bt = new BinaryTree();
124116

0 commit comments

Comments
 (0)