Skip to content

Commit d806c35

Browse files
authored
refactor: reorganize structure and add JavaDoc
- Move private methods after public methods for better readability - Add class-level JavaDoc documentation with algorithm description and links to references
1 parent 5c52b36 commit d806c35

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

src/main/java/com/thealgorithms/others/IterativeFloodFill.java

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,31 @@
33
import java.util.LinkedList;
44
import java.util.Queue;
55

6+
/**
7+
* Implementation of the Flood Fill algorithm using an iterative BFS (Breadth-First Search) approach.
8+
*
9+
* <p>The Flood Fill algorithm is used to fill connected areas in an image with a new color, starting from a specified point.
10+
* This implementation uses an iterative BFS approach with a queue
11+
* instead of recursion to avoid stack overflow issues with large images.</p>
12+
*
13+
* <p><b>Implementation Features:</b></p>
14+
* <ul>
15+
* <li>Supports 8-connected filling (horizontal, vertical, and diagonal directions)</li>
16+
* <li>Uses BFS traversal through {@link java.util.Queue}</li>
17+
* <li>Includes nested {@code Point} class to represent pixel coordinates</li>
18+
* <li>Iterative approach avoids stack overflow for large images</li>
19+
* </ul>
20+
*
21+
* <p><b>Time Complexity:</b> O(M × N) where M and N are the dimensions of the image</p>
22+
* <p><b>Space Complexity:</b> O(M × N) in the worst case the queue stores every pixel</p>
23+
*
24+
* @see <a href="https://www.geeksforgeeks.org/dsa/flood-fill-algorithm">Flood Fill Algorithm - GeeksforGeeks</a>
25+
* @see <a href="https://en.wikipedia.org/wiki/Flood_fill">Flood Fill Algorithm - Wikipedia</a>
26+
*/
627
public final class IterativeFloodFill {
728
private IterativeFloodFill() {
829
}
930

10-
/**
11-
* Represents a point in 2D space with integer coordinates.
12-
*/
13-
private static class Point {
14-
final int x;
15-
final int y;
16-
17-
Point(final int x, final int y) {
18-
this.x = x;
19-
this.y = y;
20-
}
21-
}
22-
23-
/**
24-
* Checks if a pixel should be skipped during flood fill operation.
25-
*
26-
* @param image The image to get boundaries
27-
* @param x The x co-ordinate of pixel to check
28-
* @param y The y co-ordinate of pixel to check
29-
* @param oldColor The old color which is to be replaced in the image
30-
* @return {@code true} if pixel should be skipped, else {@code false}
31-
*/
32-
private static boolean shouldSkipPixel(final int[][] image, final int x, final int y, final int oldColor) {
33-
34-
if (x < 0 || x >= image.length || y < 0 || y >= image[0].length || image[x][y] != oldColor) {
35-
return true;
36-
}
37-
38-
return false;
39-
}
40-
4131
/**
4232
* Iteratively fill the 2D image with new color
4333
*
@@ -78,4 +68,35 @@ public static void floodFill(final int[][] image, final int x, final int y, fina
7868
}
7969
}
8070
}
71+
72+
/**
73+
* Represents a point in 2D space with integer coordinates.
74+
*/
75+
private static class Point {
76+
final int x;
77+
final int y;
78+
79+
Point(final int x, final int y) {
80+
this.x = x;
81+
this.y = y;
82+
}
83+
}
84+
85+
/**
86+
* Checks if a pixel should be skipped during flood fill operation.
87+
*
88+
* @param image The image to get boundaries
89+
* @param x The x co-ordinate of pixel to check
90+
* @param y The y co-ordinate of pixel to check
91+
* @param oldColor The old color which is to be replaced in the image
92+
* @return {@code true} if pixel should be skipped, else {@code false}
93+
*/
94+
private static boolean shouldSkipPixel(final int[][] image, final int x, final int y, final int oldColor) {
95+
96+
if (x < 0 || x >= image.length || y < 0 || y >= image[0].length || image[x][y] != oldColor) {
97+
return true;
98+
}
99+
100+
return false;
101+
}
81102
}

0 commit comments

Comments
 (0)