|
| 1 | +package com.thealgorithms.datastructures.trees; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Deque; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +/** |
| 9 | + * BoundaryTraversal |
| 10 | + * <p> |
| 11 | + * Start with the Root: |
| 12 | + * Add the root node to the boundary list. |
| 13 | + * Traverse the Left Boundary (Excluding Leaf Nodes): |
| 14 | + * Move down the left side of the tree, adding each non-leaf node to the boundary list. |
| 15 | + * If a node has a left child, go left; otherwise, go right. |
| 16 | + * Visit All Leaf Nodes: |
| 17 | + * Traverse the tree and add all leaf nodes to the boundary list, from left to right. |
| 18 | + * Traverse the Right Boundary (Excluding Leaf Nodes) in Reverse Order: |
| 19 | + * Move up the right side of the tree, adding each non-leaf node to a temporary list. |
| 20 | + * If a node has a right child, go right; otherwise, go left. |
| 21 | + * Reverse the temporary list and add it to the boundary list. |
| 22 | + * Combine and Output: |
| 23 | + * The final boundary list contains the root, left boundary, leaf nodes, and reversed right boundary in that order. |
| 24 | + */ |
| 25 | +public final class BoundaryTraversal { |
| 26 | + private BoundaryTraversal() { |
| 27 | + } |
| 28 | + |
| 29 | + // Main function for boundary traversal, returns a list of boundary nodes in order |
| 30 | + public static List<Integer> boundaryTraversal(BinaryTree.Node root) { |
| 31 | + List<Integer> result = new ArrayList<>(); |
| 32 | + if (root == null) { |
| 33 | + return result; |
| 34 | + } |
| 35 | + |
| 36 | + // Add root node if it's not a leaf node |
| 37 | + if (!isLeaf(root)) { |
| 38 | + result.add(root.data); |
| 39 | + } |
| 40 | + |
| 41 | + // Add left boundary |
| 42 | + addLeftBoundary(root, result); |
| 43 | + |
| 44 | + // Add leaf nodes |
| 45 | + addLeaves(root, result); |
| 46 | + |
| 47 | + // Add right boundary |
| 48 | + addRightBoundary(root, result); |
| 49 | + |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + // Adds the left boundary, including nodes that have no left child but have a right child |
| 54 | + private static void addLeftBoundary(BinaryTree.Node node, List<Integer> result) { |
| 55 | + BinaryTree.Node cur = node.left; |
| 56 | + |
| 57 | + // If there is no left child but there is a right child, treat the right child as part of the left boundary |
| 58 | + if (cur == null && node.right != null) { |
| 59 | + cur = node.right; |
| 60 | + } |
| 61 | + |
| 62 | + while (cur != null) { |
| 63 | + if (!isLeaf(cur)) { |
| 64 | + result.add(cur.data); // Add non-leaf nodes to result |
| 65 | + } |
| 66 | + if (cur.left != null) { |
| 67 | + cur = cur.left; // Move to the left child |
| 68 | + } else if (cur.right != null) { |
| 69 | + cur = cur.right; // If left child is null, move to the right child |
| 70 | + } else { |
| 71 | + break; // Stop if there are no children |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Adds leaf nodes (nodes without children) |
| 77 | + private static void addLeaves(BinaryTree.Node node, List<Integer> result) { |
| 78 | + if (node == null) { |
| 79 | + return; |
| 80 | + } |
| 81 | + if (isLeaf(node)) { |
| 82 | + result.add(node.data); // Add leaf node |
| 83 | + } else { |
| 84 | + addLeaves(node.left, result); // Recur for left subtree |
| 85 | + addLeaves(node.right, result); // Recur for right subtree |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Adds the right boundary, excluding leaf nodes |
| 90 | + private static void addRightBoundary(BinaryTree.Node node, List<Integer> result) { |
| 91 | + BinaryTree.Node cur = node.right; |
| 92 | + List<Integer> temp = new ArrayList<>(); |
| 93 | + |
| 94 | + // If no right boundary is present and there is no left subtree, skip |
| 95 | + if (cur != null && node.left == null) { |
| 96 | + return; |
| 97 | + } |
| 98 | + while (cur != null) { |
| 99 | + if (!isLeaf(cur)) { |
| 100 | + temp.add(cur.data); // Store non-leaf nodes temporarily |
| 101 | + } |
| 102 | + if (cur.right != null) { |
| 103 | + cur = cur.right; // Move to the right child |
| 104 | + } else if (cur.left != null) { |
| 105 | + cur = cur.left; // If right child is null, move to the left child |
| 106 | + } else { |
| 107 | + break; // Stop if there are no children |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Add the right boundary nodes in reverse order |
| 112 | + for (int i = temp.size() - 1; i >= 0; i--) { |
| 113 | + result.add(temp.get(i)); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Checks if a node is a leaf node |
| 118 | + private static boolean isLeaf(BinaryTree.Node node) { |
| 119 | + return node.left == null && node.right == null; |
| 120 | + } |
| 121 | + |
| 122 | + // Iterative boundary traversal |
| 123 | + public static List<Integer> iterativeBoundaryTraversal(BinaryTree.Node root) { |
| 124 | + List<Integer> result = new ArrayList<>(); |
| 125 | + if (root == null) { |
| 126 | + return result; |
| 127 | + } |
| 128 | + |
| 129 | + // Add root node if it's not a leaf node |
| 130 | + if (!isLeaf(root)) { |
| 131 | + result.add(root.data); |
| 132 | + } |
| 133 | + |
| 134 | + // Handle the left boundary |
| 135 | + BinaryTree.Node cur = root.left; |
| 136 | + if (cur == null && root.right != null) { |
| 137 | + cur = root.right; |
| 138 | + } |
| 139 | + while (cur != null) { |
| 140 | + if (!isLeaf(cur)) { |
| 141 | + result.add(cur.data); // Add non-leaf nodes to result |
| 142 | + } |
| 143 | + cur = (cur.left != null) ? cur.left : cur.right; // Prioritize left child, move to right if left is null |
| 144 | + } |
| 145 | + |
| 146 | + // Add leaf nodes |
| 147 | + addLeaves(root, result); |
| 148 | + |
| 149 | + // Handle the right boundary using a stack (reverse order) |
| 150 | + cur = root.right; |
| 151 | + Deque<Integer> stack = new LinkedList<>(); |
| 152 | + if (cur != null && root.left == null) { |
| 153 | + return result; |
| 154 | + } |
| 155 | + while (cur != null) { |
| 156 | + if (!isLeaf(cur)) { |
| 157 | + stack.push(cur.data); // Temporarily store right boundary nodes in a stack |
| 158 | + } |
| 159 | + cur = (cur.right != null) ? cur.right : cur.left; // Prioritize right child, move to left if right is null |
| 160 | + } |
| 161 | + |
| 162 | + // Add the right boundary nodes from the stack to maintain the correct order |
| 163 | + while (!stack.isEmpty()) { |
| 164 | + result.add(stack.pop()); |
| 165 | + } |
| 166 | + return result; |
| 167 | + } |
| 168 | +} |
0 commit comments