Visualization helper methods
I have created few helper methods to visualize a data structure during an algorithm.
1. Two pointers
- int[] : Integer Array
// Helper method to visualize two pointers for int[]
private void tpi(int[] s, int l, int r, String... prefix) {
if (prefix.length == 0) {
prefix = new String[] { "" };
}
System.out.print(prefix[0]);
for (int i = 0; i < s.length; i++) {
if (i == l && i == r) {
System.out.printf("[(%s)]", s[i]);
} else if (i == l) {
System.out.printf("(%s)", s[i]);
} else if (i == r) {
System.out.printf("[%s]", s[i]);
} else {
System.out.print(s[i]);
}
System.out.print(" ");
}
System.out.printf("| l=%s, r=%s %n", l, r);
}
- char[] : Character Array
// Helper method to visualize two pointers for char[]
private void tpc(char[] s, int l, int r, String... prefix) {
if (prefix.length == 0) {
prefix = new String[] { "" };
}
System.out.print(prefix[0]);
for (int i = 0; i < s.length; i++) {
if (i == l && i == r) {
System.out.printf("[(%s)]", s[i]);
} else if (i == l) {
System.out.printf("(%s)", s[i]);
} else if (i == r) {
System.out.printf("[%s]", s[i]);
} else {
System.out.print(s[i]);
}
System.out.print(" ");
}
System.out.printf("| l=%s, r=%s %n", l, r);
}
- String : String
// Helper method to visualize two pointers for String
private void tps(String s, int l, int r, String... prefix) {
if (prefix.length == 0) {
prefix = new String[] { "" };
}
System.out.print(prefix[0]);
for (int i = 0; i < s.length(); i++) {
if (i == l && i == r) {
System.out.printf("[(%s)]", s.charAt(i));
} else if (i == l) {
System.out.printf("(%s)", s.charAt(i));
} else if (i == r) {
System.out.printf("[%s]", s.charAt(i));
} else {
System.out.print(s.charAt(i));
}
System.out.print(" ");
}
System.out.printf("| l=%s, r=%s %n", l, r);
}
2. Sliding Window
- int[] : Integer Array
// Helper method to visualize sliding window for int[]
private void swi(int[] nums, int l, int r, String... prefix) {
if (prefix.length == 0) {
prefix = new String[] { "" };
}
System.out.print(prefix[0]);
for (int i = 0; i < nums.length; i++) {
if (i == l) {
System.out.print("[");
}
System.out.print(nums[i]);
if (i == r) {
System.out.print("]");
}
System.out.print(" ");
}
System.out.printf("| l=%s, r=%s %n", l, r);
}
- char[] : Character Array
// Helper method to visualize sliding window for char[]
private void swi(char[] chars, int l, int r, String... prefix) {
if (prefix.length == 0) {
prefix = new String[] { "" };
}
System.out.print(prefix[0]);
for (int i = 0; i < chars.length; i++) {
if (i == l) {
System.out.print("[");
}
System.out.print(chars[i]);
if (i == r) {
System.out.print("]");
}
System.out.print(" ");
}
System.out.printf("| l=%s, r=%s %n", l, r);
}
- String : String
// Helper method to visualize sliding window for String
private void sws(String s, int l, int r, String... prefix) {
if (prefix.length == 0) {
prefix = new String[] { "" };
}
System.out.print(prefix[0]);
for (int i = 0; i < s.length(); i++) {
if (i == l) {
System.out.print("[");
}
System.out.print(s.charAt(i));
if (i == r) {
System.out.print("]");
}
System.out.print(" ");
}
System.out.printf("| l=%s, r=%s %n", l, r);
}
3. Binary Search
// Helper method to visualize binary search (how pointers are moving)
private void pbs(int[] a, int m, int l, int r, String... prefix) {
if (prefix.length == 0) {
prefix = new String[] { "" };
}
System.out.print(prefix[0]);
System.out.printf("L: %s, M: %s, R: %s | ", l, m, r);
for (int i = 0; i < a.length; i++) {
if (i == m) {
if (m == l && m == r) {
System.out.printf("[({%s})]", a[i]);
} else if (m == l) {
System.out.printf("({%s})", a[i]);
} else if (m == r) {
System.out.printf("[{%s}]", a[i]);
} else {
System.out.printf("{%s}", a[i]);
}
} else if (i == l && i == r) {
System.out.printf("[(%s)]", a[i]);
} else if (i == l) {
System.out.printf("(%s)", a[i]);
} else if (i == r) {
System.out.printf("[%s]", a[i]);
} else {
System.out.print(a[i]);
}
System.out.print(" ");
}
System.out.println();
}
4. Two dimension Array
- int[][] : Integer Array
// Helper method to visualize two dimesional int array int[][]
private void tdi(int[][] a) {
for (int[] row : a) {
System.out.println(Arrays.toString(row));
}
System.out.println("---");
}
- char[][] : Character Array
// Helper method to visualize two dimesional char array char[][]
private void tdc(char[][] a) {
for (char[] row : a) {
System.out.println(Arrays.toString(row));
}
System.out.println("---");
}
- String[][] : String
// Helper method to visualize two dimesional String array String[][]
private void tds(String[][] a) {
for (String[] row : a) {
System.out.println(Arrays.toString(row));
}
System.out.println("---");
}
5. Print Linked list
// Helper method to visualize LinkedList
private void pll(ListNode head, String... prefix) {
if (prefix.length == 0) {
prefix = new String[] { "" };
}
System.out.print(prefix[0]);
int size = 0;
ListNode curr = head;
while (curr != null) {
size++;
curr = curr.next;
}
curr = head;
int i = 0;
while (curr != null) {
if (i == size / 2) {
System.out.printf("[%s] -> ", curr.val);
} else {
System.out.printf("%s -> ", curr.val);
}
curr = curr.next;
i++;
}
System.out.print("null\n");
}
6. Print Binary Tree
// Helper method to visualize bianry tree
private void pbt(final TreeNode node) {
String result = ppt(node, new StringBuilder(), true, new StringBuilder());
System.out.println(result);
}
private String pbtUtil(TreeNode node, StringBuilder prefix, boolean isTail, StringBuilder sb) {
if (node.right != null) {
pbtUtil(node.right, new StringBuilder().append(prefix).append(isTail ? "│ " : " "), false,
sb);
}
sb.append(prefix).append(isTail ? "└── " : "┌── ").append(node.val).append("\n");
if (node.left != null) {
pbtUtil(node.left, new StringBuilder().append(prefix).append(isTail ? " " : "│ "), true, sb);
}
return sb.toString();
}
Top comments (0)