Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<FindBugsFilter>
<Match>
<!-- The Field is used to retain reified type info on a field and is not UNREAD -->
<Class name="org.nintynine.problems.BTreeP56$Node"/>
<Class name="org.nintynine.problems.BtreeP56$Node"/>
<Bug pattern="URF_UNREAD_FIELD"/>
</Match>
</FindBugsFilter>
129 changes: 0 additions & 129 deletions src/main/java/org/nintynine/problems/BTreeP60.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/org/nintynine/problems/BTreeP61.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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");
}
Expand All @@ -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);
}
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.nintynine.problems;

public class BTreeP56<T> {
/**
* Simple binary tree used for symmetry checks in problem 56.
*
* @param <T> node value type
*/
public class BtreeP56<T> {
private Node<T> root;

private static class Node<T> {
Expand All @@ -15,7 +20,7 @@ private static class Node<T> {
}
}

public BTreeP56() {
public BtreeP56() {
this.root = null;
}

Expand All @@ -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<T> left, Node<T> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package org.nintynine.problems;

public class BTreeP57<T extends Comparable<T>> {
/**
* Binary tree utilities for problem 57.
*
* @param <T> type of node values
*/
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public class BtreeP57<T extends Comparable<T>> {
private Node<T> root;

private static class Node<T> {
Expand All @@ -15,10 +21,15 @@ private static class Node<T> {
}
}

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);
Expand All @@ -44,6 +55,11 @@ private Node<T> insertRec(Node<T> 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);
}
Expand Down
Loading