Skip to content

Commit c6c9058

Browse files
kwoykeKrzysztof Woyke
andauthored
BAEL-4864: Fix traverseInOrderWithoutRecursion method (eugenp#10588)
Co-authored-by: Krzysztof Woyke <[email protected]>
1 parent 506e82c commit c6c9058

2 files changed

Lines changed: 46 additions & 23 deletions

File tree

data-structures/src/main/java/com/baeldung/tree/BinaryTree.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,48 +148,46 @@ public void traverseLevelOrder() {
148148
}
149149
}
150150

151-
152151
public void traverseInOrderWithoutRecursion() {
153-
Stack<Node> stack = new Stack<Node>();
152+
Stack<Node> stack = new Stack<>();
154153
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;
154+
155+
while (current != null || !stack.isEmpty()) {
156+
while (current != null) {
165157
stack.push(current);
158+
current = current.left;
166159
}
160+
161+
Node top = stack.pop();
162+
visit(top.value);
163+
current = top.right;
167164
}
168165
}
169-
166+
170167
public void traversePreOrderWithoutRecursion() {
171-
Stack<Node> stack = new Stack<Node>();
168+
Stack<Node> stack = new Stack<>();
172169
Node current = root;
173170
stack.push(root);
174-
while(! stack.isEmpty()) {
171+
172+
while (current != null && !stack.isEmpty()) {
175173
current = stack.pop();
176174
visit(current.value);
177-
178-
if(current.right != null)
175+
176+
if (current.right != null)
179177
stack.push(current.right);
180-
181-
if(current.left != null)
178+
179+
if (current.left != null)
182180
stack.push(current.left);
183-
}
181+
}
184182
}
185183

186184
public void traversePostOrderWithoutRecursion() {
187-
Stack<Node> stack = new Stack<Node>();
185+
Stack<Node> stack = new Stack<>();
188186
Node prev = root;
189187
Node current = root;
190188
stack.push(root);
191189

192-
while (!stack.isEmpty()) {
190+
while (current != null && !stack.isEmpty()) {
193191
current = stack.peek();
194192
boolean hasChild = (current.left != null || current.right != null);
195193
boolean isPrevLastChild = (prev == current.right || (prev == current.left && current.right == null));

data-structures/src/test/java/com/baeldung/tree/BinaryTreeUnitTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void givenABinaryTree_WhenAddingElements_ThenTreeNotEmpty() {
1313

1414
BinaryTree bt = createBinaryTree();
1515

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

1919
@Test
@@ -72,6 +72,7 @@ public void givenABinaryTree_WhenDeletingNonExistingElement_ThenTreeDoesNotDelet
7272

7373
@Test
7474
public void it_deletes_the_root() {
75+
7576
int value = 12;
7677
BinaryTree bt = new BinaryTree();
7778
bt.add(value);
@@ -91,6 +92,14 @@ public void givenABinaryTree_WhenTraversingInOrder_ThenPrintValues() {
9192
bt.traverseInOrderWithoutRecursion();
9293
}
9394

95+
@Test
96+
public void givenAnEmptyBinaryTree_WhenTraversingInOrderWithoutRecursion_ThenNoException() {
97+
98+
BinaryTree empty = new BinaryTree();
99+
100+
empty.traverseInOrderWithoutRecursion();
101+
}
102+
94103
@Test
95104
public void givenABinaryTree_WhenTraversingPreOrder_ThenPrintValues() {
96105

@@ -101,6 +110,14 @@ public void givenABinaryTree_WhenTraversingPreOrder_ThenPrintValues() {
101110
bt.traversePreOrderWithoutRecursion();
102111
}
103112

113+
@Test
114+
public void givenAnEmptyBinaryTree_WhenTraversingPreOrderWithoutRecursion_ThenNoException() {
115+
116+
BinaryTree empty = new BinaryTree();
117+
118+
empty.traversePreOrderWithoutRecursion();
119+
}
120+
104121
@Test
105122
public void givenABinaryTree_WhenTraversingPostOrder_ThenPrintValues() {
106123

@@ -111,6 +128,14 @@ public void givenABinaryTree_WhenTraversingPostOrder_ThenPrintValues() {
111128
bt.traversePostOrderWithoutRecursion();
112129
}
113130

131+
@Test
132+
public void givenAnEmptyBinaryTree_WhenTraversingPostOrderWithoutRecursion_ThenNoException() {
133+
134+
BinaryTree empty = new BinaryTree();
135+
136+
empty.traversePostOrderWithoutRecursion();
137+
}
138+
114139
@Test
115140
public void givenABinaryTree_WhenTraversingLevelOrder_ThenPrintValues() {
116141

0 commit comments

Comments
 (0)