diff --git a/spotbugs-exclude.xml b/spotbugs-exclude.xml
index cbf8bf0..ae50214 100644
--- a/spotbugs-exclude.xml
+++ b/spotbugs-exclude.xml
@@ -2,7 +2,7 @@
-
+
diff --git a/src/main/java/org/nintynine/problems/BTreeP60.java b/src/main/java/org/nintynine/problems/BTreeP60.java
deleted file mode 100644
index 1852510..0000000
--- a/src/main/java/org/nintynine/problems/BTreeP60.java
+++ /dev/null
@@ -1,129 +0,0 @@
-package org.nintynine.problems;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-public class BTreeP60 {
- private final T value;
- protected BTreeP60 left;
- protected BTreeP60 right;
-
- public BTreeP60(T value) {
- this.value = value;
- this.left = null;
- this.right = null;
- }
-
- public static int minNodes(int h) {
- if (h <= 0) return 0;
- if (h == 1) return 1;
- return 1 + minNodes(h - 1) + minNodes(h - 2);
- }
-
- public static int maxHeight(int n) {
- if (n <= 0) return 0;
- int h = 0;
- while (minNodes(h) <= n) h++;
- return h - 1;
- }
-
- public static List> hbalTreeNodes(int n) {
- if (n <= 0) return Collections.emptyList();
- if (n == 1) {
- List> result = new ArrayList<>();
- result.add(new BTreeP60<>("x"));
- return result;
- }
-
- // Special case for 2 nodes
- if (n == 2) {
- List> result = new ArrayList<>();
- // Create left-child tree
- BTreeP60 leftTree = new BTreeP60<>("x");
- leftTree.left = new BTreeP60<>("x");
- result.add(leftTree);
-
- // Create right-child tree
- BTreeP60 rightTree = new BTreeP60<>("x");
- rightTree.right = new BTreeP60<>("x");
- result.add(rightTree);
-
- return result;
- }
-
- List> result = new ArrayList<>();
- int maxH = maxHeight(n);
- int minH = (int) Math.ceil(Math.log(n + (double) 1) / Math.log(2));
-
- for (int h = minH; h <= maxH; h++) {
- result.addAll(generateTreesWithHeight(h, n));
- }
-
- return result;
- }
-
- private static List> generateTreesWithHeight(int height, int n) {
- List> result = new ArrayList<>();
-
- if (n == 0) {
- return Collections.emptyList();
- }
- if (n == 1) {
- result.add(new BTreeP60<>("x"));
- return result;
- }
-
- for (int leftNodes = 0; leftNodes < n; leftNodes++) {
- int rightNodes = n - 1 - leftNodes;
-
- List> leftSubtrees = hbalTreeNodes(leftNodes);
- List> rightSubtrees = hbalTreeNodes(rightNodes);
-
- for (BTreeP60 left : leftSubtrees) {
- for (BTreeP60 right : rightSubtrees) {
- if (isHeightBalanced(left, right)
- && getHeight(left) <= height - 1
- && getHeight(right) <= height - 1) {
- BTreeP60 root = new BTreeP60<>("x");
- root.left = cloneTree(left);
- root.right = cloneTree(right);
- result.add(root);
- }
- }
- }
- }
-
- return result;
- }
-
- private static boolean isHeightBalanced(BTreeP60 left, BTreeP60 right) {
- return Math.abs(getHeight(left) - getHeight(right)) <= 1;
- }
-
- public static int getHeight(BTreeP60> node) {
- if (node == null) return 0;
- return 1 + Math.max(getHeight(node.left), getHeight(node.right));
- }
-
- private static BTreeP60 cloneTree(BTreeP60 node) {
- if (node == null) return null;
- BTreeP60 clone = new BTreeP60<>(node.value);
- clone.left = cloneTree(node.left);
- clone.right = cloneTree(node.right);
- return clone;
- }
-
- // Getters for testing
- public T getValue() {
- return value;
- }
-
- public BTreeP60 getLeft() {
- return left;
- }
-
- public BTreeP60 getRight() {
- return right;
- }
-}
diff --git a/src/main/java/org/nintynine/problems/BTreeP61.java b/src/main/java/org/nintynine/problems/BTreeP61.java
deleted file mode 100644
index 1ce4864..0000000
--- a/src/main/java/org/nintynine/problems/BTreeP61.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package org.nintynine.problems;
-
-public class BTreeP61 extends BTreeP60 {
-
- public BTreeP61(T value) {
- super(value);
- }
-
- public static int countLeaves(BTreeP61 tree) {
- if (tree == null) {
- return 0;
- }
-
- // A leaf is a node with no children
- if (tree.getLeft() == null && tree.getRight() == null) {
- return 1;
- }
-
- // Recursively count leaves in left and right subtrees
- return countLeaves((BTreeP61) tree.getLeft()) + countLeaves((BTreeP61) tree.getRight());
- }
-}
diff --git a/src/main/java/org/nintynine/problems/BTree54.java b/src/main/java/org/nintynine/problems/Btree54.java
similarity index 90%
rename from src/main/java/org/nintynine/problems/BTree54.java
rename to src/main/java/org/nintynine/problems/Btree54.java
index e5aa65c..982d1db 100644
--- a/src/main/java/org/nintynine/problems/BTree54.java
+++ b/src/main/java/org/nintynine/problems/Btree54.java
@@ -3,12 +3,12 @@
import java.util.Objects;
/** P54A: Check whether a given expression represents a binary tree */
-public class BTree54 {
+public class Btree54 {
/** Represents a node in the binary tree expression */
- public static class BTree54Node {
+ public static class Btree54Node {
private final String value;
- private final BTree54Node left;
- private final BTree54Node right;
+ private final Btree54Node left;
+ private final Btree54Node right;
/**
* Constructs a binary tree node
@@ -17,7 +17,7 @@ public static class BTree54Node {
* @param left Left child node or null
* @param right Right child node or null
*/
- public BTree54Node(String value, BTree54Node left, BTree54Node right) {
+ public Btree54Node(String value, Btree54Node left, Btree54Node right) {
this.value = Objects.requireNonNull(value, "Node value cannot be null");
this.left = left;
this.right = right;
@@ -28,14 +28,14 @@ public BTree54Node(String value, BTree54Node left, BTree54Node right) {
*
* @param value The value at this node
*/
- public BTree54Node(String value) {
+ public Btree54Node(String value) {
this(value, null, null);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
- if (!(o instanceof BTree54Node bTree54Node)) return false;
+ if (!(o instanceof Btree54Node bTree54Node)) return false;
return Objects.equals(value, bTree54Node.value)
&& Objects.equals(left, bTree54Node.left)
&& Objects.equals(right, bTree54Node.right);
@@ -60,7 +60,7 @@ public String toString() {
}
}
- private BTree54() {} // Prevent instantiation
+ private Btree54() {} // Prevent instantiation
private static boolean isValidValue(String value) {
// Check for any special characters or improper formatting
@@ -113,7 +113,7 @@ public static boolean isTree(String expression) {
* @return The root node of the parsed tree
* @throws IllegalArgumentException if the expression is invalid
*/
- public static BTree54Node parseTree(String expression) {
+ public static Btree54Node parseTree(String expression) {
if (expression == null) {
throw new IllegalArgumentException("Expression cannot be null");
}
@@ -123,7 +123,7 @@ public static BTree54Node parseTree(String expression) {
// Handle single value case
if (!expression.startsWith("(")) {
if (isValidValue(expression)) {
- return new BTree54Node(expression);
+ return new Btree54Node(expression);
}
throw new IllegalArgumentException("Invalid value: " + expression);
}
@@ -148,10 +148,10 @@ public static BTree54Node parseTree(String expression) {
throw new IllegalArgumentException("Invalid node value");
}
- BTree54Node left = "nil".equals(leftExpr) ? null : parseTree(leftExpr);
- BTree54Node right = "nil".equals(rightExpr) ? null : parseTree(rightExpr);
+ Btree54Node left = "nil".equals(leftExpr) ? null : parseTree(leftExpr);
+ Btree54Node right = "nil".equals(rightExpr) ? null : parseTree(rightExpr);
- return new BTree54Node(value, left, right);
+ return new Btree54Node(value, left, right);
}
@SuppressWarnings("java:S5852")
diff --git a/src/main/java/org/nintynine/problems/BTreeP56.java b/src/main/java/org/nintynine/problems/BtreeP56.java
similarity index 64%
rename from src/main/java/org/nintynine/problems/BTreeP56.java
rename to src/main/java/org/nintynine/problems/BtreeP56.java
index 6cfd7ca..d6a4735 100644
--- a/src/main/java/org/nintynine/problems/BTreeP56.java
+++ b/src/main/java/org/nintynine/problems/BtreeP56.java
@@ -1,6 +1,11 @@
package org.nintynine.problems;
-public class BTreeP56 {
+/**
+ * Simple binary tree used for symmetry checks in problem 56.
+ *
+ * @param node value type
+ */
+public class BtreeP56 {
private Node root;
private static class Node {
@@ -15,7 +20,7 @@ private static class Node {
}
}
- public BTreeP56() {
+ public BtreeP56() {
this.root = null;
}
@@ -24,27 +29,37 @@ public void setRoot(T value) {
}
public void addLeft(T value) {
- if (root == null) throw new IllegalStateException("Tree has no root");
+ if (root == null) {
+ throw new IllegalStateException("Tree has no root");
+ }
root.left = new Node<>(value);
}
public void addRight(T value) {
- if (root == null) throw new IllegalStateException("Tree has no root");
+ if (root == null) {
+ throw new IllegalStateException("Tree has no root");
+ }
root.right = new Node<>(value);
}
public boolean isSymmetric() {
- if (root == null) return true;
+ if (root == null) {
+ return true;
+ }
return isMirror(root.left, root.right);
}
@SuppressWarnings("java:S2234") // or just "S2234"
private boolean isMirror(Node left, Node right) {
// If both nodes are null, they are mirror images
- if (left == null && right == null) return true;
+ if (left == null && right == null) {
+ return true;
+ }
// If only one node is null, they are not mirror images
- if (left == null || right == null) return false;
+ if (left == null || right == null) {
+ return false;
+ }
// Check if the structure is mirrored
return isMirror(left.left, right.right) && isMirror(left.right, right.left);
diff --git a/src/main/java/org/nintynine/problems/BTreeP57.java b/src/main/java/org/nintynine/problems/BtreeP57.java
similarity index 77%
rename from src/main/java/org/nintynine/problems/BTreeP57.java
rename to src/main/java/org/nintynine/problems/BtreeP57.java
index 7fa2e90..3dafff5 100644
--- a/src/main/java/org/nintynine/problems/BTreeP57.java
+++ b/src/main/java/org/nintynine/problems/BtreeP57.java
@@ -1,6 +1,12 @@
package org.nintynine.problems;
-public class BTreeP57> {
+/**
+ * Binary tree utilities for problem 57.
+ *
+ * @param type of node values
+ */
+@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
+public class BtreeP57> {
private Node root;
private static class Node {
@@ -15,10 +21,15 @@ private static class Node {
}
}
- public BTreeP57() {
+ public BtreeP57() {
this.root = null;
}
+ /**
+ * Constructs the tree by inserting all given values.
+ *
+ * @param values values to insert
+ */
public void construct(T[] values) {
for (T value : values) {
insert(value);
@@ -44,6 +55,11 @@ private Node insertRec(Node node, T value) {
return node;
}
+ /**
+ * Checks if this tree is symmetric.
+ *
+ * @return {@code true} if the tree is symmetric, otherwise {@code false}
+ */
public boolean isSymmetric() {
return root == null || isMirror(root.left, root.right);
}
diff --git a/src/main/java/org/nintynine/problems/BTreeP58.java b/src/main/java/org/nintynine/problems/BtreeP58.java
similarity index 62%
rename from src/main/java/org/nintynine/problems/BTreeP58.java
rename to src/main/java/org/nintynine/problems/BtreeP58.java
index 384bf7c..813c583 100644
--- a/src/main/java/org/nintynine/problems/BTreeP58.java
+++ b/src/main/java/org/nintynine/problems/BtreeP58.java
@@ -4,15 +4,16 @@
import java.util.List;
import java.util.Objects;
-public class BTreeP58 {
- private BTreeP58() {}
+/** Utilities for constructing symmetric binary trees. */
+public class BtreeP58 {
+ private BtreeP58() {}
- public static class BTreeP58Node {
+ public static class BtreeP58Node {
char value;
- BTreeP58Node left;
- BTreeP58Node right;
+ BtreeP58Node left;
+ BtreeP58Node right;
- BTreeP58Node(char value) {
+ BtreeP58Node(char value) {
this.value = value;
left = null;
right = null;
@@ -27,7 +28,7 @@ public int hashCode() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
- BTreeP58Node bTreeP58Node = (BTreeP58Node) o;
+ BtreeP58Node bTreeP58Node = (BtreeP58Node) o;
return value == bTreeP58Node.value
&& Objects.equals(left, bTreeP58Node.left)
&& Objects.equals(right, bTreeP58Node.right);
@@ -42,26 +43,26 @@ public String toString() {
}
}
- public static List symCbalTrees(int nodes) {
+ public static List symCbalTrees(int nodes) {
if (nodes % 2 == 0) return new ArrayList<>();
return generateSymCbalTrees(nodes);
}
- private static List generateSymCbalTrees(int nodes) {
- List result = new ArrayList<>();
+ private static List generateSymCbalTrees(int nodes) {
+ List result = new ArrayList<>();
if (nodes == 0) return result;
if (nodes == 1) {
- result.add(new BTreeP58Node('X'));
+ result.add(new BtreeP58Node('X'));
return result;
}
int remainingNodes = nodes - 1;
if (remainingNodes % 2 != 0) return result;
- List subtrees = generateBalancedSubtrees(remainingNodes / 2);
- for (BTreeP58Node leftSubtree : subtrees) {
- BTreeP58Node root = new BTreeP58Node('X');
+ List subtrees = generateBalancedSubtrees(remainingNodes / 2);
+ for (BtreeP58Node leftSubtree : subtrees) {
+ BtreeP58Node root = new BtreeP58Node('X');
root.left = cloneTree(leftSubtree);
root.right = cloneTree(mirrorTree(leftSubtree));
result.add(root);
@@ -70,27 +71,27 @@ private static List generateSymCbalTrees(int nodes) {
return result;
}
- private static List generateBalancedSubtrees(int nodes) {
- List result = new ArrayList<>();
+ private static List generateBalancedSubtrees(int nodes) {
+ List result = new ArrayList<>();
if (nodes == 0) {
result.add(null);
return result;
}
if (nodes == 1) {
- result.add(new BTreeP58Node('X'));
+ result.add(new BtreeP58Node('X'));
return result;
}
int remainingNodes = nodes - 1;
for (int leftNodes = remainingNodes / 2; leftNodes <= (remainingNodes + 1) / 2; leftNodes++) {
int rightNodes = remainingNodes - leftNodes;
- List leftSubtrees = generateBalancedSubtrees(leftNodes);
- List rightSubtrees = generateBalancedSubtrees(rightNodes);
+ List leftSubtrees = generateBalancedSubtrees(leftNodes);
+ List rightSubtrees = generateBalancedSubtrees(rightNodes);
- for (BTreeP58Node left : leftSubtrees) {
- for (BTreeP58Node right : rightSubtrees) {
- BTreeP58Node root = new BTreeP58Node('X');
+ for (BtreeP58Node left : leftSubtrees) {
+ for (BtreeP58Node right : rightSubtrees) {
+ BtreeP58Node root = new BtreeP58Node('X');
root.left = cloneTree(left);
root.right = cloneTree(right);
result.add(root);
@@ -100,17 +101,17 @@ private static List generateBalancedSubtrees(int nodes) {
return result;
}
- private static BTreeP58Node mirrorTree(BTreeP58Node root) {
+ private static BtreeP58Node mirrorTree(BtreeP58Node root) {
if (root == null) return null;
- BTreeP58Node mirrored = new BTreeP58Node(root.value);
+ BtreeP58Node mirrored = new BtreeP58Node(root.value);
mirrored.left = mirrorTree(root.right);
mirrored.right = mirrorTree(root.left);
return mirrored;
}
- private static BTreeP58Node cloneTree(BTreeP58Node root) {
+ private static BtreeP58Node cloneTree(BtreeP58Node root) {
if (root == null) return null;
- BTreeP58Node clone = new BTreeP58Node(root.value);
+ BtreeP58Node clone = new BtreeP58Node(root.value);
clone.left = cloneTree(root.left);
clone.right = cloneTree(root.right);
return clone;
diff --git a/src/main/java/org/nintynine/problems/BTreeP59.java b/src/main/java/org/nintynine/problems/BtreeP59.java
similarity index 96%
rename from src/main/java/org/nintynine/problems/BTreeP59.java
rename to src/main/java/org/nintynine/problems/BtreeP59.java
index 8ee4a3b..bb91d5c 100644
--- a/src/main/java/org/nintynine/problems/BTreeP59.java
+++ b/src/main/java/org/nintynine/problems/BtreeP59.java
@@ -5,8 +5,9 @@
import java.util.Objects;
@SuppressWarnings("DuplicatedCode")
-public class BTreeP59 {
- private BTreeP59() {}
+/** Utility class for generating completely balanced binary trees. */
+public class BtreeP59 {
+ private BtreeP59() {}
public static class BTree59Node {
char value;
diff --git a/src/main/java/org/nintynine/problems/BtreeP60.java b/src/main/java/org/nintynine/problems/BtreeP60.java
new file mode 100644
index 0000000..8fc9b60
--- /dev/null
+++ b/src/main/java/org/nintynine/problems/BtreeP60.java
@@ -0,0 +1,189 @@
+package org.nintynine.problems;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Height-balanced binary tree utilities for problem 60.
+ *
+ * @param node value type
+ */
+@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
+public class BtreeP60 {
+ private final T value;
+ protected BtreeP60 left;
+ protected BtreeP60 right;
+
+ /**
+ * Creates a new tree node with the given value.
+ *
+ * @param value node value
+ */
+ public BtreeP60(T value) {
+ this.value = value;
+ this.left = null;
+ this.right = null;
+ }
+
+ /**
+ * Computes the minimum number of nodes of a height-balanced tree for a
+ * given height.
+ *
+ * @param h desired height
+ * @return minimum node count
+ */
+ public static int minNodes(int h) {
+ if (h <= 0) {
+ return 0;
+ }
+ if (h == 1) {
+ return 1;
+ }
+ return 1 + minNodes(h - 1) + minNodes(h - 2);
+ }
+
+ /**
+ * Computes the maximum possible height of a tree with the given number of
+ * nodes while remaining height balanced.
+ *
+ * @param n node count
+ * @return maximum height
+ */
+ public static int maxHeight(int n) {
+ if (n <= 0) {
+ return 0;
+ }
+ int h = 0;
+ while (minNodes(h) <= n) {
+ h++;
+ }
+ return h - 1;
+ }
+
+ /**
+ * Generates all height-balanced trees with the given number of nodes.
+ *
+ * @param n number of nodes
+ * @return list of trees
+ */
+ public static List> hbalTreeNodes(int n) {
+ if (n <= 0) {
+ return Collections.emptyList();
+ }
+ if (n == 1) {
+ List> result = new ArrayList<>();
+ result.add(new BtreeP60<>("x"));
+ return result;
+ }
+
+ // Special case for 2 nodes
+ if (n == 2) {
+ List> result = new ArrayList<>();
+ // Create left-child tree
+ BtreeP60 leftTree = new BtreeP60<>("x");
+ leftTree.left = new BtreeP60<>("x");
+ result.add(leftTree);
+
+ // Create right-child tree
+ BtreeP60 rightTree = new BtreeP60<>("x");
+ rightTree.right = new BtreeP60<>("x");
+ result.add(rightTree);
+
+ return result;
+ }
+
+ List> result = new ArrayList<>();
+ int maxH = maxHeight(n);
+ int minH = (int) Math.ceil(Math.log(n + (double) 1) / Math.log(2));
+
+ for (int h = minH; h <= maxH; h++) {
+ result.addAll(generateTreesWithHeight(h, n));
+ }
+
+ return result;
+ }
+
+ /**
+ * Generates trees of a specific height containing the given number of nodes.
+ */
+ private static List> generateTreesWithHeight(int height, int n) {
+ List> result = new ArrayList<>();
+
+ if (n == 0) {
+ return Collections.emptyList();
+ }
+ if (n == 1) {
+ result.add(new BtreeP60<>("x"));
+ return result;
+ }
+
+ for (int leftNodes = 0; leftNodes < n; leftNodes++) {
+ int rightNodes = n - 1 - leftNodes;
+
+ List> leftSubtrees = hbalTreeNodes(leftNodes);
+ List> rightSubtrees = hbalTreeNodes(rightNodes);
+
+ for (BtreeP60 left : leftSubtrees) {
+ for (BtreeP60 right : rightSubtrees) {
+ if (isHeightBalanced(left, right)
+ && getHeight(left) <= height - 1
+ && getHeight(right) <= height - 1) {
+ BtreeP60 root = new BtreeP60<>("x");
+ root.left = cloneTree(left);
+ root.right = cloneTree(right);
+ result.add(root);
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Checks if two subtrees are height balanced relative to each other.
+ */
+ private static boolean isHeightBalanced(BtreeP60 left, BtreeP60 right) {
+ return Math.abs(getHeight(left) - getHeight(right)) <= 1;
+ }
+
+ /**
+ * Returns the height of the given tree.
+ *
+ * @param node root node
+ * @return tree height
+ */
+ public static int getHeight(BtreeP60> node) {
+ if (node == null) {
+ return 0;
+ }
+ return 1 + Math.max(getHeight(node.left), getHeight(node.right));
+ }
+
+ /**
+ * Creates a deep copy of the given tree.
+ */
+ private static BtreeP60 cloneTree(BtreeP60 node) {
+ if (node == null) {
+ return null;
+ }
+ BtreeP60 clone = new BtreeP60<>(node.value);
+ clone.left = cloneTree(node.left);
+ clone.right = cloneTree(node.right);
+ return clone;
+ }
+
+ // Getters for testing
+ public T getValue() {
+ return value;
+ }
+
+ public BtreeP60 getLeft() {
+ return left;
+ }
+
+ public BtreeP60 getRight() {
+ return right;
+ }
+}
diff --git a/src/main/java/org/nintynine/problems/BtreeP61.java b/src/main/java/org/nintynine/problems/BtreeP61.java
new file mode 100644
index 0000000..f11810f
--- /dev/null
+++ b/src/main/java/org/nintynine/problems/BtreeP61.java
@@ -0,0 +1,40 @@
+package org.nintynine.problems;
+
+/**
+ * Binary tree utilities for problem 61.
+ *
+ * @param node value type
+ */
+@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
+public class BtreeP61 extends BtreeP60 {
+
+ /**
+ * Creates a new tree node with the given value.
+ *
+ * @param value node value
+ */
+ public BtreeP61(T value) {
+ super(value);
+ }
+
+ /**
+ * Counts the number of leaves in the given tree.
+ *
+ * @param tree tree to inspect
+ * @param element type
+ * @return leaf count
+ */
+ public static int countLeaves(BtreeP61 tree) {
+ if (tree == null) {
+ return 0;
+ }
+
+ // A leaf is a node with no children
+ if (tree.getLeft() == null && tree.getRight() == null) {
+ return 1;
+ }
+
+ // Recursively count leaves in left and right subtrees
+ return countLeaves((BtreeP61) tree.getLeft()) + countLeaves((BtreeP61) tree.getRight());
+ }
+}
diff --git a/src/main/java/org/nintynine/problems/BTreeP68.java b/src/main/java/org/nintynine/problems/BtreeP68.java
similarity index 98%
rename from src/main/java/org/nintynine/problems/BTreeP68.java
rename to src/main/java/org/nintynine/problems/BtreeP68.java
index ba0e377..75ac087 100644
--- a/src/main/java/org/nintynine/problems/BTreeP68.java
+++ b/src/main/java/org/nintynine/problems/BtreeP68.java
@@ -12,9 +12,9 @@
*
See issue #61.
*/
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
-public class BTreeP68 {
+public class BtreeP68 {
- private BTreeP68() {
+ private BtreeP68() {
// utility class
}
diff --git a/src/main/java/org/nintynine/problems/BTreeP69.java b/src/main/java/org/nintynine/problems/BtreeP69.java
similarity index 98%
rename from src/main/java/org/nintynine/problems/BTreeP69.java
rename to src/main/java/org/nintynine/problems/BtreeP69.java
index e1badfe..2026043 100644
--- a/src/main/java/org/nintynine/problems/BTreeP69.java
+++ b/src/main/java/org/nintynine/problems/BtreeP69.java
@@ -11,9 +11,9 @@
* representation.
*/
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
-public class BTreeP69 {
+public class BtreeP69 {
- private BTreeP69() {
+ private BtreeP69() {
// utility class
}
diff --git a/src/main/java/org/nintynine/problems/MathP37.java b/src/main/java/org/nintynine/problems/MathP37.java
index aae2744..604a3ea 100644
--- a/src/main/java/org/nintynine/problems/MathP37.java
+++ b/src/main/java/org/nintynine/problems/MathP37.java
@@ -85,7 +85,7 @@ public static long[] comparePerformance(long m) {
// Test primitive method (P34)
startTime = System.nanoTime();
- long result1 = MathP34.totientPhi(m);
+ final long result1 = MathP34.totientPhi(m);
endTime = System.nanoTime();
times[0] = endTime - startTime;
diff --git a/src/main/java/org/nintynine/problems/MathP41.java b/src/main/java/org/nintynine/problems/MathP41.java
index f188d24..4f36684 100644
--- a/src/main/java/org/nintynine/problems/MathP41.java
+++ b/src/main/java/org/nintynine/problems/MathP41.java
@@ -117,7 +117,7 @@ public static String formatGoldbachList(List compositions) {
return compositions.stream().map(GoldbachListEntry::toString).collect(Collectors.joining("\n"));
}
- /** Represents a Goldbach composition list entry */
+ /** Represents a Goldbach composition list entry. */
public record GoldbachListEntry(long number, MathP40.GoldbachPair pair) {
@Override
public String toString() {
diff --git a/src/main/java/org/nintynine/problems/MyListP02.java b/src/main/java/org/nintynine/problems/MyListP02.java
index 444ba32..88444c1 100644
--- a/src/main/java/org/nintynine/problems/MyListP02.java
+++ b/src/main/java/org/nintynine/problems/MyListP02.java
@@ -3,15 +3,25 @@
import java.util.Arrays;
import java.util.NoSuchElementException;
+/**
+ * Utility list that provides a method to get the penultimate element.
+ *
+ * @param element type
+ */
public class MyListP02 extends MyList {
@SafeVarargs
public MyListP02(T... elements) {
super(elements);
}
+ /**
+ * Returns the element just before the last one in the list.
+ *
+ * @return second-to-last element
+ */
public T lastButOne() {
return Arrays.stream(items)
- .reduce(Pair.empty(), Pair::shift, (_, b) -> b)
+ .reduce(Pair.empty(), Pair::shift, (ignore, b) -> b)
.secondLastOrThrow();
}
diff --git a/src/main/java/org/nintynine/problems/MyListP03.java b/src/main/java/org/nintynine/problems/MyListP03.java
index 38f4de0..b75f0d2 100644
--- a/src/main/java/org/nintynine/problems/MyListP03.java
+++ b/src/main/java/org/nintynine/problems/MyListP03.java
@@ -2,12 +2,23 @@
import java.util.Arrays;
+/**
+ * Provides a method to select the Kth element of a list.
+ *
+ * @param element type
+ */
public class MyListP03 extends MyListP02 {
@SafeVarargs
public MyListP03(T... elements) {
super(elements);
}
+ /**
+ * Returns the element at the given 1-based position.
+ *
+ * @param k position (1-based)
+ * @return element at that position
+ */
public T elementAt(long k) {
if (k < 1) {
throw new IllegalArgumentException("Position must be greater than 0");
diff --git a/src/main/java/org/nintynine/problems/MyListP05.java b/src/main/java/org/nintynine/problems/MyListP05.java
index 6937532..217a586 100644
--- a/src/main/java/org/nintynine/problems/MyListP05.java
+++ b/src/main/java/org/nintynine/problems/MyListP05.java
@@ -3,12 +3,22 @@
import java.util.Arrays;
import java.util.LinkedList;
+/**
+ * Provides a reverse operation for lists.
+ *
+ * @param element type
+ */
public class MyListP05 extends MyListP04 {
@SafeVarargs
public MyListP05(T... elements) {
super(elements);
}
+ /**
+ * Returns a new list with the elements in reverse order.
+ *
+ * @return reversed list
+ */
public MyListP05 reverse() {
return new MyListP05<>(
Arrays.stream(items)
diff --git a/src/main/java/org/nintynine/problems/MyListP07.java b/src/main/java/org/nintynine/problems/MyListP07.java
index c2aa509..0df25ce 100644
--- a/src/main/java/org/nintynine/problems/MyListP07.java
+++ b/src/main/java/org/nintynine/problems/MyListP07.java
@@ -4,6 +4,11 @@
import java.util.List;
import java.util.stream.Stream;
+/**
+ * Utility class for flattening nested lists.
+ *
+ * @param type of elements in the list
+ */
public class MyListP07 extends MyListP06 {
@SafeVarargs
public MyListP07(T... elements) {
diff --git a/src/main/java/org/nintynine/problems/MyListP08.java b/src/main/java/org/nintynine/problems/MyListP08.java
index 9aca4f2..d4b06a9 100644
--- a/src/main/java/org/nintynine/problems/MyListP08.java
+++ b/src/main/java/org/nintynine/problems/MyListP08.java
@@ -4,12 +4,22 @@
import java.util.Objects;
import java.util.stream.LongStream;
+/**
+ * Removes consecutive duplicates from the list.
+ *
+ * @param element type
+ */
public class MyListP08 extends MyListP07 {
@SafeVarargs
public MyListP08(T... elements) {
super(elements);
}
+ /**
+ * Compresses consecutive duplicate elements.
+ *
+ * @return a new list without consecutive duplicates
+ */
public MyListP08 compress() {
if (length() == 0) {
return new MyListP08<>();
diff --git a/src/main/java/org/nintynine/problems/MyListP10.java b/src/main/java/org/nintynine/problems/MyListP10.java
index 1a6f813..8510c7c 100644
--- a/src/main/java/org/nintynine/problems/MyListP10.java
+++ b/src/main/java/org/nintynine/problems/MyListP10.java
@@ -71,8 +71,12 @@ public EncodedElement(long count, T element) {
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
EncodedElement> that = (EncodedElement>) o;
return count == that.count && Objects.equals(element, that.element);
}
diff --git a/src/main/java/org/nintynine/problems/MyListP13.java b/src/main/java/org/nintynine/problems/MyListP13.java
index 647ba1a..537f130 100644
--- a/src/main/java/org/nintynine/problems/MyListP13.java
+++ b/src/main/java/org/nintynine/problems/MyListP13.java
@@ -83,7 +83,7 @@ private void addEncodedElement(List