Skip to content

Commit 48c65e4

Browse files
authored
Add boundary traversal of binary tree (#5639)
1 parent f170078 commit 48c65e4

File tree

2 files changed

+276
-0
lines changed

2 files changed

+276
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
*
11+
*/
12+
public class BoundaryTraversalTest {
13+
14+
@Test
15+
public void testNullRoot() {
16+
assertEquals(Collections.emptyList(), BoundaryTraversal.boundaryTraversal(null));
17+
assertEquals(Collections.emptyList(), BoundaryTraversal.iterativeBoundaryTraversal(null));
18+
}
19+
20+
@Test
21+
public void testSingleNodeTree() {
22+
final BinaryTree.Node root = new BinaryTree.Node(1);
23+
24+
List<Integer> expected = List.of(1);
25+
26+
assertEquals(expected, BoundaryTraversal.boundaryTraversal(root));
27+
assertEquals(expected, BoundaryTraversal.iterativeBoundaryTraversal(root));
28+
}
29+
30+
/*
31+
1
32+
/ \
33+
2 3
34+
/ \ / \
35+
4 5 6 7
36+
37+
*/
38+
@Test
39+
public void testCompleteBinaryTree() {
40+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
41+
42+
List<Integer> expected = List.of(1, 2, 4, 5, 6, 7, 3);
43+
44+
assertEquals(expected, BoundaryTraversal.boundaryTraversal(root));
45+
assertEquals(expected, BoundaryTraversal.iterativeBoundaryTraversal(root));
46+
}
47+
48+
/*
49+
1
50+
/ \
51+
2 7
52+
/ \
53+
3 8
54+
\ /
55+
4 9
56+
/ \
57+
5 6
58+
/ \
59+
10 11
60+
*/
61+
@Test
62+
public void testBoundaryTraversal() {
63+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 7, 3, null, null, 8, null, 4, 9, null, 5, 6, 10, 11});
64+
65+
List<Integer> expected = List.of(1, 2, 3, 4, 5, 6, 10, 11, 9, 8, 7);
66+
67+
assertEquals(expected, BoundaryTraversal.boundaryTraversal(root));
68+
assertEquals(expected, BoundaryTraversal.iterativeBoundaryTraversal(root));
69+
}
70+
71+
/*
72+
1
73+
/
74+
2
75+
/
76+
3
77+
/
78+
4
79+
*/
80+
@Test
81+
public void testLeftSkewedTree() {
82+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, null, 3, null, 4, null});
83+
84+
List<Integer> expected = List.of(1, 2, 3, 4);
85+
86+
assertEquals(expected, BoundaryTraversal.boundaryTraversal(root));
87+
assertEquals(expected, BoundaryTraversal.iterativeBoundaryTraversal(root));
88+
}
89+
90+
/*
91+
5
92+
\
93+
6
94+
\
95+
7
96+
\
97+
8
98+
*/
99+
@Test
100+
public void testRightSkewedTree() {
101+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {5, null, 6, null, 7, null, 8});
102+
103+
List<Integer> expected = List.of(5, 6, 7, 8);
104+
105+
assertEquals(expected, BoundaryTraversal.boundaryTraversal(root));
106+
assertEquals(expected, BoundaryTraversal.iterativeBoundaryTraversal(root));
107+
}
108+
}

0 commit comments

Comments
 (0)