diff --git a/algorithms-searching/src/main/java/com/baeldung/algorithms/dfs/BinaryTree.java b/algorithms-searching/src/main/java/com/baeldung/algorithms/dfs/BinaryTree.java index a6019ea9f9..a3cb2b396e 100644 --- a/algorithms-searching/src/main/java/com/baeldung/algorithms/dfs/BinaryTree.java +++ b/algorithms-searching/src/main/java/com/baeldung/algorithms/dfs/BinaryTree.java @@ -1,7 +1,5 @@ package com.baeldung.algorithms.dfs; -import java.util.LinkedList; -import java.util.Queue; import java.util.Stack; public class BinaryTree { @@ -124,69 +122,43 @@ public class BinaryTree { } } - public void traverseLevelOrder() { - if (root == null) { - return; - } - Queue 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() { - Stack stack = new Stack(); + Stack stack = new Stack<>(); Node current = root; - stack.push(root); - while(! stack.isEmpty()) { - while(current.left != null) { - current = current.left; - stack.push(current); - } - current = stack.pop(); - visit(current.value); - if(current.right != null) { - current = current.right; + + while (current != null || !stack.isEmpty()) { + while (current != null) { stack.push(current); + current = current.left; } + + Node top = stack.pop(); + visit(top.value); + current = top.right; } } - + public void traversePreOrderWithoutRecursion() { - Stack stack = new Stack(); - Node current = root; + Stack stack = new Stack<>(); + Node current; stack.push(root); while(! stack.isEmpty()) { current = stack.pop(); visit(current.value); - + if(current.right != null) stack.push(current.right); - + if(current.left != null) stack.push(current.left); - } + } } - + public void traversePostOrderWithoutRecursion() { - Stack stack = new Stack(); + Stack stack = new Stack<>(); Node prev = root; - Node current = root; + Node current; stack.push(root); while (!stack.isEmpty()) { @@ -206,14 +178,14 @@ public class BinaryTree { 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; Node left; Node right; diff --git a/algorithms-searching/src/test/java/com/baeldung/algorithms/dfs/BinaryTreeUnitTest.java b/algorithms-searching/src/test/java/com/baeldung/algorithms/dfs/BinaryTreeUnitTest.java index 076da14f81..f98b4377ed 100644 --- a/algorithms-searching/src/test/java/com/baeldung/algorithms/dfs/BinaryTreeUnitTest.java +++ b/algorithms-searching/src/test/java/com/baeldung/algorithms/dfs/BinaryTreeUnitTest.java @@ -1,11 +1,11 @@ package com.baeldung.algorithms.dfs; +import org.junit.Test; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.junit.Test; - public class BinaryTreeUnitTest { @Test @@ -13,7 +13,7 @@ public class BinaryTreeUnitTest { BinaryTree bt = createBinaryTree(); - assertTrue(!bt.isEmpty()); + assertFalse(bt.isEmpty()); } @Test @@ -111,14 +111,6 @@ public class BinaryTreeUnitTest { bt.traversePostOrderWithoutRecursion(); } - @Test - public void givenABinaryTree_WhenTraversingLevelOrder_ThenPrintValues() { - - BinaryTree bt = createBinaryTree(); - - bt.traverseLevelOrder(); - } - private BinaryTree createBinaryTree() { BinaryTree bt = new BinaryTree();