style: enable NeedBraces in checkstyle (#5227)

* enable style NeedBraces

* style: enable NeedBraces in checkstyle

---------

Co-authored-by: Samuel Facchinello <samuel.facchinello@piksel.com>
This commit is contained in:
Samuel Facchinello
2024-06-13 21:00:16 +02:00
committed by GitHub
parent 51fcc66345
commit 87b17e0571
68 changed files with 629 additions and 261 deletions

View File

@ -24,7 +24,9 @@ public class CircularBuffer<Item> {
}
public Item get() {
if (isEmpty()) return null;
if (isEmpty()) {
return null;
}
Item item = buffer[getPointer.getAndIncrement()];
size.decrementAndGet();
@ -32,7 +34,9 @@ public class CircularBuffer<Item> {
}
public boolean put(Item item) {
if (isFull()) return false;
if (isFull()) {
return false;
}
buffer[putPointer.getAndIncrement()] = item;
size.incrementAndGet();
@ -49,7 +53,9 @@ public class CircularBuffer<Item> {
}
public int getAndIncrement() {
if (pointer == max) pointer = 0;
if (pointer == max) {
pointer = 0;
}
int tmp = pointer;
pointer++;
return tmp;

View File

@ -127,7 +127,9 @@ public class Kosaraju {
private void dfs(int node, int[] vis, List<List<Integer>> list) {
vis[node] = 1;
for (Integer neighbour : list.get(node)) {
if (vis[neighbour] == 0) dfs(neighbour, vis, list);
if (vis[neighbour] == 0) {
dfs(neighbour, vis, list);
}
}
stack.push(node);
}
@ -136,7 +138,9 @@ public class Kosaraju {
private void dfs2(int node, int[] vis, List<List<Integer>> list) {
vis[node] = 1;
for (Integer neighbour : list.get(node)) {
if (vis[neighbour] == 0) dfs2(neighbour, vis, list);
if (vis[neighbour] == 0) {
dfs2(neighbour, vis, list);
}
}
scc.add(node);
}

View File

@ -82,7 +82,9 @@ public class TarjansAlgorithm {
Stack<Integer> st = new Stack<Integer>();
for (int i = 0; i < v; i++) {
if (insertionTime[i] == -1) stronglyConnCompsUtil(i, lowTime, insertionTime, isInStack, st, graph);
if (insertionTime[i] == -1) {
stronglyConnCompsUtil(i, lowTime, insertionTime, isInStack, st, graph);
}
}
return sccList;

View File

@ -68,10 +68,14 @@ public class HashMap {
public Node findKey(int key) {
if (!isEmpty()) {
Node temp = first;
if (temp.getKey() == key) return temp;
if (temp.getKey() == key) {
return temp;
}
while ((temp = temp.getNext()) != null) {
if (temp.getKey() == key) return temp;
if (temp.getKey() == key) {
return temp;
}
}
}
return null;

View File

@ -188,12 +188,14 @@ public class HashMapCuckooHashing {
throw new IllegalArgumentException("Table is empty");
}
if (Objects.equals(buckets[hash], wrappedInt)) return hash;
if (Objects.equals(buckets[hash], wrappedInt)) {
return hash;
}
hash = hashFunction2(key);
if (!Objects.equals(buckets[hash], wrappedInt))
if (!Objects.equals(buckets[hash], wrappedInt)) {
throw new IllegalArgumentException("Key " + key + " not found in table");
else {
} else {
return hash;
}
}

View File

@ -231,7 +231,9 @@ public class FibonacciHeap {
private void cascadingCuts(HeapNode curr) {
if (!curr.isMarked()) { // stop the recursion
curr.mark();
if (!curr.isRoot()) this.markedHeapNoodesCounter++;
if (!curr.isRoot()) {
this.markedHeapNoodesCounter++;
}
} else {
if (curr.isRoot()) {
return;

View File

@ -56,9 +56,13 @@ public class LeftistHeap {
// Function merge with two Nodes a and b
public Node merge(Node a, Node b) {
if (a == null) return b;
if (a == null) {
return b;
}
if (b == null) return a;
if (b == null) {
return a;
}
// Violates leftist property, so must do a swap
if (a.element > b.element) {
@ -93,7 +97,9 @@ public class LeftistHeap {
// Returns and removes the minimum element in the heap
public int extractMin() {
// If is empty return -1
if (isEmpty()) return -1;
if (isEmpty()) {
return -1;
}
int min = root.element;
root = merge(root.left, root.right);
@ -109,7 +115,9 @@ public class LeftistHeap {
// Auxiliary function for in_order
private void inOrderAux(Node n, ArrayList<Integer> lst) {
if (n == null) return;
if (n == null) {
return;
}
inOrderAux(n.left, lst);
lst.add(n.element);
inOrderAux(n.right, lst);

View File

@ -98,11 +98,13 @@ public class MaxHeap implements Heap {
@Override
public void deleteElement(int elementIndex) {
if (maxHeap.isEmpty()) try {
if (maxHeap.isEmpty()) {
try {
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
} catch (EmptyHeapException e) {
e.printStackTrace();
}
}
if ((elementIndex > maxHeap.size()) || (elementIndex <= 0)) {
throw new IndexOutOfBoundsException("Index out of heap range");
}

View File

@ -92,11 +92,13 @@ public class MinHeap implements Heap {
@Override
public void deleteElement(int elementIndex) {
if (minHeap.isEmpty()) try {
if (minHeap.isEmpty()) {
try {
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
} catch (EmptyHeapException e) {
e.printStackTrace();
}
}
if ((elementIndex > minHeap.size()) || (elementIndex <= 0)) {
throw new IndexOutOfBoundsException("Index out of heap range");
}

View File

@ -23,8 +23,9 @@ public class CircularQueue {
public boolean isFull() {
if (topOfQueue + 1 == beginningOfQueue) {
return true;
} else
} else {
return topOfQueue == size - 1 && beginningOfQueue == 0;
}
}
public void enQueue(int value) {

View File

@ -125,9 +125,13 @@ public class LinkedQueue<T> implements Iterable<T> {
*/
public T peek(int pos) {
if (pos > size) throw new IndexOutOfBoundsException("Position %s out of range!".formatted(pos));
if (pos > size) {
throw new IndexOutOfBoundsException("Position %s out of range!".formatted(pos));
}
Node<T> node = front;
while (pos-- > 0) node = node.next;
while (pos-- > 0) {
node = node.next;
}
return node.data;
}
@ -170,14 +174,18 @@ public class LinkedQueue<T> implements Iterable<T> {
* Clear all nodes in queue
*/
public void clear() {
while (size > 0) dequeue();
while (size > 0) {
dequeue();
}
}
@Override
public String toString() {
StringJoiner join = new StringJoiner(", "); // separator of ', '
Node<T> travel = front;
while ((travel = travel.next) != null) join.add(String.valueOf(travel.data));
while ((travel = travel.next) != null) {
join.add(String.valueOf(travel.data));
}
return '[' + join.toString() + ']';
}

View File

@ -87,9 +87,13 @@ class PriorityQueue {
while (2 * pos <= nItems) {
int current = 2 * pos; // Jump to the positon of child node
// Compare both the children for the greater one
if (current < nItems && queueArray[current] < queueArray[current + 1]) current++;
if (current < nItems && queueArray[current] < queueArray[current + 1]) {
current++;
}
// If the parent node is greater, sink operation is complete. Break the loop
if (queueArray[pos] >= queueArray[current]) break;
if (queueArray[pos] >= queueArray[current]) {
break;
}
// If not exchange the value of parent with child
int temp = queueArray[pos];

View File

@ -60,9 +60,13 @@ public class AVLSimple {
node.height = Math.max(height(node.left), height(node.right)) + 1;
int bf = bf(node);
// LL case
if (bf > 1 && item < node.left.data) return rightRotate(node);
if (bf > 1 && item < node.left.data) {
return rightRotate(node);
}
// RR case
if (bf < -1 && item > node.right.data) return leftRotate(node);
if (bf < -1 && item > node.right.data) {
return leftRotate(node);
}
// RL case
if (bf < -1 && item < node.right.data) {
node.right = rightRotate(node.right);
@ -84,18 +88,24 @@ public class AVLSimple {
private void display(Node node) {
String str = "";
if (node.left != null)
if (node.left != null) {
str += node.left.data + "=>";
else
} else {
str += "END=>";
}
str += node.data + "";
if (node.right != null)
if (node.right != null) {
str += "<=" + node.right.data;
else
} else {
str += "<=END";
}
System.out.println(str);
if (node.left != null) display(node.left);
if (node.right != null) display(node.right);
if (node.left != null) {
display(node.left);
}
if (node.right != null) {
display(node.right);
}
}
private int height(Node node) {
@ -106,7 +116,9 @@ public class AVLSimple {
}
private int bf(Node node) {
if (node == null) return 0;
if (node == null) {
return 0;
}
return height(node.left) - height(node.right);
}

View File

@ -36,7 +36,9 @@ public final class InorderTraversal {
public static List<Integer> iterativeInorder(BinaryTree.Node root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
if (root == null) {
return result;
}
Deque<BinaryTree.Node> stack = new ArrayDeque<>();
while (!stack.isEmpty() || root != null) {

View File

@ -34,10 +34,15 @@ public class KDTree {
* @param points Array of initial points
*/
KDTree(Point[] points) {
if (points.length == 0) throw new IllegalArgumentException("Points array cannot be empty");
if (points.length == 0) {
throw new IllegalArgumentException("Points array cannot be empty");
}
this.k = points[0].getDimension();
for (Point point : points)
if (point.getDimension() != k) throw new IllegalArgumentException("Points must have the same dimension");
for (Point point : points) {
if (point.getDimension() != k) {
throw new IllegalArgumentException("Points must have the same dimension");
}
}
this.root = build(points, 0);
}
@ -48,11 +53,16 @@ public class KDTree {
*
*/
KDTree(int[][] pointsCoordinates) {
if (pointsCoordinates.length == 0) throw new IllegalArgumentException("Points array cannot be empty");
if (pointsCoordinates.length == 0) {
throw new IllegalArgumentException("Points array cannot be empty");
}
this.k = pointsCoordinates[0].length;
Point[] points = Arrays.stream(pointsCoordinates).map(Point::new).toArray(Point[] ::new);
for (Point point : points)
if (point.getDimension() != k) throw new IllegalArgumentException("Points must have the same dimension");
for (Point point : points) {
if (point.getDimension() != k) {
throw new IllegalArgumentException("Points must have the same dimension");
}
}
this.root = build(points, 0);
}
@ -119,7 +129,9 @@ public class KDTree {
public static int comparableDistanceExceptAxis(Point p1, Point p2, int axis) {
int distance = 0;
for (int i = 0; i < p1.getDimension(); i++) {
if (i == axis) continue;
if (i == axis) {
continue;
}
int t = p1.getCoordinate(i) - p2.getCoordinate(i);
distance += t * t;
}
@ -164,10 +176,11 @@ public class KDTree {
* @return The nearest child Node
*/
public Node getNearChild(Point point) {
if (point.getCoordinate(axis) < this.point.getCoordinate(axis))
if (point.getCoordinate(axis) < this.point.getCoordinate(axis)) {
return left;
else
} else {
return right;
}
}
/**
@ -178,10 +191,11 @@ public class KDTree {
* @return The farthest child Node
*/
public Node getFarChild(Point point) {
if (point.getCoordinate(axis) < this.point.getCoordinate(axis))
if (point.getCoordinate(axis) < this.point.getCoordinate(axis)) {
return right;
else
} else {
return left;
}
}
/**
@ -207,9 +221,13 @@ public class KDTree {
* @return The root of the KDTree
*/
private Node build(Point[] points, int depth) {
if (points.length == 0) return null;
if (points.length == 0) {
return null;
}
int axis = depth % k;
if (points.length == 1) return new Node(points[0], axis);
if (points.length == 1) {
return new Node(points[0], axis);
}
Arrays.sort(points, Comparator.comparingInt(o -> o.getCoordinate(axis)));
int median = points.length >> 1;
Node node = new Node(points[median], axis);
@ -225,7 +243,9 @@ public class KDTree {
*
*/
public void insert(Point point) {
if (point.getDimension() != k) throw new IllegalArgumentException("Point has wrong dimension");
if (point.getDimension() != k) {
throw new IllegalArgumentException("Point has wrong dimension");
}
root = insert(root, point, 0);
}
@ -240,11 +260,14 @@ public class KDTree {
*/
private Node insert(Node root, Point point, int depth) {
int axis = depth % k;
if (root == null) return new Node(point, axis);
if (point.getCoordinate(axis) < root.getAxisCoordinate())
if (root == null) {
return new Node(point, axis);
}
if (point.getCoordinate(axis) < root.getAxisCoordinate()) {
root.left = insert(root.left, point, depth + 1);
else
} else {
root.right = insert(root.right, point, depth + 1);
}
return root;
}
@ -257,7 +280,9 @@ public class KDTree {
* @return The Node corresponding to the specified point
*/
public Optional<Node> search(Point point) {
if (point.getDimension() != k) throw new IllegalArgumentException("Point has wrong dimension");
if (point.getDimension() != k) {
throw new IllegalArgumentException("Point has wrong dimension");
}
return search(root, point);
}
@ -270,8 +295,12 @@ public class KDTree {
* @return The Node corresponding to the specified point
*/
public Optional<Node> search(Node root, Point point) {
if (root == null) return Optional.empty();
if (root.point.equals(point)) return Optional.of(root);
if (root == null) {
return Optional.empty();
}
if (root.point.equals(point)) {
return Optional.of(root);
}
return search(root.getNearChild(point), point);
}
@ -295,9 +324,13 @@ public class KDTree {
* @return The Node with minimum value in the specified axis of the point
*/
public Node findMin(Node root, int axis) {
if (root == null) return null;
if (root == null) {
return null;
}
if (root.getAxis() == axis) {
if (root.left == null) return root;
if (root.left == null) {
return root;
}
return findMin(root.left, axis);
} else {
Node left = findMin(root.left, axis);
@ -327,9 +360,13 @@ public class KDTree {
* @return The Node with maximum value in the specified axis of the point
*/
public Node findMax(Node root, int axis) {
if (root == null) return null;
if (root == null) {
return null;
}
if (root.getAxis() == axis) {
if (root.right == null) return root;
if (root.right == null) {
return root;
}
return findMax(root.right, axis);
} else {
Node left = findMax(root.left, axis);
@ -358,7 +395,9 @@ public class KDTree {
* @return The new root of the subtree
*/
private Node delete(Node root, Node node) {
if (root == null) return null;
if (root == null) {
return null;
}
if (root.equals(node)) {
if (root.right != null) {
Node min = findMin(root.right, root.getAxis());
@ -368,13 +407,15 @@ public class KDTree {
Node min = findMin(root.left, root.getAxis());
root.point = min.point;
root.left = delete(root.left, min);
} else
} else {
return null;
}
}
if (root.getAxisCoordinate() < node.point.getCoordinate(root.getAxis()))
if (root.getAxisCoordinate() < node.point.getCoordinate(root.getAxis())) {
root.left = delete(root.left, node);
else
} else {
root.right = delete(root.right, node);
}
return root;
}
@ -395,13 +436,21 @@ public class KDTree {
* @param nearest The nearest neighbor found so far.
* */
private Node findNearest(Node root, Point point, Node nearest) {
if (root == null) return nearest;
if (root.point.equals(point)) return root;
if (root == null) {
return nearest;
}
if (root.point.equals(point)) {
return root;
}
int distance = Point.comparableDistance(root.point, point);
int distanceExceptAxis = Point.comparableDistanceExceptAxis(root.point, point, root.getAxis());
if (distance < Point.comparableDistance(nearest.point, point)) nearest = root;
if (distance < Point.comparableDistance(nearest.point, point)) {
nearest = root;
}
nearest = findNearest(root.getNearChild(point), point, nearest);
if (distanceExceptAxis < Point.comparableDistance(nearest.point, point)) nearest = findNearest(root.getFarChild(point), point, nearest);
if (distanceExceptAxis < Point.comparableDistance(nearest.point, point)) {
nearest = findNearest(root.getFarChild(point), point, nearest);
}
return nearest;
}
}

View File

@ -40,11 +40,19 @@ public class LazySegmentTree {
* Shift the lazy value of this node to its children.
*/
public void shift() {
if (lazy == 0) return;
if (this.left == null && this.right == null) return;
if (lazy == 0) {
return;
}
if (this.left == null && this.right == null) {
return;
}
this.value += this.lazy;
if (this.left != null) this.left.applyUpdate(this.lazy);
if (this.right != null) this.right.applyUpdate(this.lazy);
if (this.left != null) {
this.left.applyUpdate(this.lazy);
}
if (this.right != null) {
this.right.applyUpdate(this.lazy);
}
this.lazy = 0;
}
@ -56,8 +64,12 @@ public class LazySegmentTree {
* @return The new Node.
*/
static Node merge(Node left, Node right) {
if (left == null) return right;
if (right == null) return left;
if (left == null) {
return right;
}
if (right == null) {
return left;
}
Node result = new Node(left.start, right.end, left.value + right.value);
result.left = left;
result.right = right;
@ -97,7 +109,9 @@ public class LazySegmentTree {
* @return The root of the new LazySegmentTree.
*/
private Node buildTree(int[] array, int start, int end) {
if (end - start < 2) return new Node(start, end, array[start]);
if (end - start < 2) {
return new Node(start, end, array[start]);
}
int mid = (start + end) >> 1;
Node left = buildTree(array, start, mid);
Node right = buildTree(array, mid, end);
@ -117,7 +131,9 @@ public class LazySegmentTree {
curr.applyUpdate(diff);
return;
}
if (left >= curr.end || right <= curr.start) return;
if (left >= curr.end || right <= curr.start) {
return;
}
curr.shift();
updateRange(left, right, diff, curr.left);
updateRange(left, right, diff, curr.right);
@ -133,8 +149,12 @@ public class LazySegmentTree {
* @return The Node representing the sum of the given range.
*/
private Node getRange(int left, int right, Node curr) {
if (left <= curr.start && curr.end <= right) return curr;
if (left >= curr.end || right <= curr.start) return null;
if (left <= curr.start && curr.end <= right) {
return curr;
}
if (left >= curr.end || right <= curr.start) {
return null;
}
curr.shift();
return Node.merge(getRange(left, right, curr.left), getRange(left, right, curr.right));
}

View File

@ -36,15 +36,21 @@ public final class PreOrderTraversal {
public static List<Integer> iterativePreOrder(BinaryTree.Node root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
if (root == null) {
return result;
}
Deque<BinaryTree.Node> stack = new LinkedList<>();
stack.push(root);
while (!stack.isEmpty()) {
BinaryTree.Node node = stack.pop();
result.add(node.data);
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return result;

View File

@ -52,16 +52,22 @@ public final class SameTreesCheck {
BinaryTree.Node second = q2.poll();
// check that some node can be null
// if the check is true: both nodes are null or both nodes are not null
if (!equalNodes(first, second)) return false;
if (!equalNodes(first, second)) {
return false;
}
if (first != null) {
if (!equalNodes(first.left, second.left)) return false;
if (!equalNodes(first.left, second.left)) {
return false;
}
if (first.left != null) {
q1.add(first.left);
q2.add(second.left);
}
if (!equalNodes(first.right, second.right)) return false;
if (!equalNodes(first.right, second.right)) {
return false;
}
if (first.right != null) {
q1.add(first.right);
q2.add(second.right);