mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
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:
committed by
GitHub
parent
51fcc66345
commit
87b17e0571
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user