[JAVA-9909] Fix code for DFS article

This commit is contained in:
Haroon Khan 2022-02-23 17:04:52 +00:00
parent d988efb89f
commit 87c9fde8d7
2 changed files with 28 additions and 64 deletions

View File

@ -1,7 +1,5 @@
package com.baeldung.algorithms.dfs; package com.baeldung.algorithms.dfs;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack; import java.util.Stack;
public class BinaryTree { public class BinaryTree {
@ -124,69 +122,43 @@ public class BinaryTree {
} }
} }
public void traverseLevelOrder() {
if (root == null) {
return;
}
Queue<Node> nodes = new LinkedList<>();
nodes.add(root);
while (!nodes.isEmpty()) {
Node node = nodes.remove();
System.out.print(" " + node.value);
if (node.left != null) {
nodes.add(node.left);
}
if (node.left != null) {
nodes.add(node.right);
}
}
}
public void traverseInOrderWithoutRecursion() { public void traverseInOrderWithoutRecursion() {
Stack<Node> stack = new Stack<Node>(); Stack<Node> stack = new Stack<>();
Node current = root; Node current = root;
stack.push(root);
while(! stack.isEmpty()) { while (current != null || !stack.isEmpty()) {
while(current.left != null) { while (current != null) {
current = current.left;
stack.push(current);
}
current = stack.pop();
visit(current.value);
if(current.right != null) {
current = current.right;
stack.push(current); stack.push(current);
current = current.left;
} }
Node top = stack.pop();
visit(top.value);
current = top.right;
} }
} }
public void traversePreOrderWithoutRecursion() { public void traversePreOrderWithoutRecursion() {
Stack<Node> stack = new Stack<Node>(); Stack<Node> stack = new Stack<>();
Node current = root; Node current;
stack.push(root); stack.push(root);
while(! stack.isEmpty()) { while(! stack.isEmpty()) {
current = stack.pop(); current = stack.pop();
visit(current.value); visit(current.value);
if(current.right != null) if(current.right != null)
stack.push(current.right); stack.push(current.right);
if(current.left != null) if(current.left != null)
stack.push(current.left); stack.push(current.left);
} }
} }
public void traversePostOrderWithoutRecursion() { public void traversePostOrderWithoutRecursion() {
Stack<Node> stack = new Stack<Node>(); Stack<Node> stack = new Stack<>();
Node prev = root; Node prev = root;
Node current = root; Node current;
stack.push(root); stack.push(root);
while (!stack.isEmpty()) { while (!stack.isEmpty()) {
@ -206,14 +178,14 @@ public class BinaryTree {
stack.push(current.left); stack.push(current.left);
} }
} }
} }
}
private void visit(int value) {
System.out.print(" " + value);
} }
class Node { private void visit(int value) {
System.out.print(" " + value);
}
static class Node {
int value; int value;
Node left; Node left;
Node right; Node right;

View File

@ -1,11 +1,11 @@
package com.baeldung.algorithms.dfs; package com.baeldung.algorithms.dfs;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class BinaryTreeUnitTest { public class BinaryTreeUnitTest {
@Test @Test
@ -13,7 +13,7 @@ public class BinaryTreeUnitTest {
BinaryTree bt = createBinaryTree(); BinaryTree bt = createBinaryTree();
assertTrue(!bt.isEmpty()); assertFalse(bt.isEmpty());
} }
@Test @Test
@ -111,14 +111,6 @@ public class BinaryTreeUnitTest {
bt.traversePostOrderWithoutRecursion(); bt.traversePostOrderWithoutRecursion();
} }
@Test
public void givenABinaryTree_WhenTraversingLevelOrder_ThenPrintValues() {
BinaryTree bt = createBinaryTree();
bt.traverseLevelOrder();
}
private BinaryTree createBinaryTree() { private BinaryTree createBinaryTree() {
BinaryTree bt = new BinaryTree(); BinaryTree bt = new BinaryTree();