Repository: TheAlgorithms/Java
Pull request #3884 (2023-02-17 09:40:09)

Added recursive&iterative preorder binary tree traversal
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/trees/PreOrderTraversal.java b/src/main/java/com/thealgorithms/datastructures/trees/PreOrderTraversal.java
new file mode 100644
index 00000000..d0a5bc4a
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/trees/PreOrderTraversal.java
@@ -0,0 +1,59 @@
+package com.thealgorithms.datastructures.trees;
+
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Given tree is traversed in a 'pre-order' way: ROOT -> LEFT -> RIGHT.
+ * Below are given the recursive and iterative implementations.
+ *
+ * Complexities:
+ * Recursive: O(n) - time, O(n) - space, where 'n' is the number of nodes in a tree.
+ *
+ * Iterative: O(n) - time, O(h) - space, where 'n' is the number of nodes in a tree
+ * and 'h' is the height of a binary tree.
+ * In the worst case 'h' can be O(n) if tree is completely unbalanced, for instance:
+ * 5
+ *  \
+ *   6
+ *    \
+ *     7
+ *      \
+ *       8
+ *
+ * @author Albina Gimaletdinova on 17/02/2023
+ */
+public class PreOrderTraversal {
+    public static List<Integer> recursivePreOrder(BinaryTree.Node root) {
+        List<Integer> result = new ArrayList<>();
+        recursivePreOrder(root, result);
+        return result;
+    }
+
+    public static List<Integer> iterativePreOrder(BinaryTree.Node root) {
+        List<Integer> result = new ArrayList<>();
+        if (root == null) return result;
+
+        Deque<BinaryTree.Node> stack = new LinkedList<>();
+        stack.push(root);
+        while (!stack.isEmpty()) {
+            BinaryTree.Node node = stack.pop();
+            result.add(node.data);
+            if (node.right != null) stack.push(node.right);
+            if (node.left != null) stack.push(node.left);
+        }
+
+        return result;
+    }
+
+    private static void recursivePreOrder(BinaryTree.Node root, List<Integer> result) {
+        if (root == null) {
+            return;
+        }
+        result.add(root.data);
+        recursivePreOrder(root.left, result);
+        recursivePreOrder(root.right, result);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/trees/PreOrderTraversalTest.java b/src/test/java/com/thealgorithms/datastructures/trees/PreOrderTraversalTest.java
new file mode 100644
index 00000000..eeac2131
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/trees/PreOrderTraversalTest.java
@@ -0,0 +1,49 @@
+package com.thealgorithms.datastructures.trees;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * @author Albina Gimaletdinova on 17/02/2023
+ */
+public class PreOrderTraversalTest {
+    @Test
+    public void testNullRoot() {
+        assertEquals(Collections.emptyList(), PreOrderTraversal.recursivePreOrder(null));
+        assertEquals(Collections.emptyList(), PreOrderTraversal.iterativePreOrder(null));
+    }
+
+    /*
+         1
+        / \
+       2   3
+      /\   /\
+     4  5 6  7
+    */
+    @Test
+    public void testRecursivePreOrder() {
+        final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
+        assertEquals(List.of(1, 2, 4, 5, 3, 6, 7), PreOrderTraversal.recursivePreOrder(root));
+        assertEquals(List.of(1, 2, 4, 5, 3, 6, 7), PreOrderTraversal.iterativePreOrder(root));
+    }
+
+    /*
+         5
+          \
+           6
+            \
+             7
+              \
+               8
+    */
+    @Test
+    public void testRecursivePreOrderNonBalanced() {
+        final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
+        assertEquals(List.of(5, 6, 7, 8), PreOrderTraversal.recursivePreOrder(root));
+        assertEquals(List.of(5, 6, 7, 8), PreOrderTraversal.iterativePreOrder(root));
+    }
+}