From 9d8dd4cd7134a0de2b3ee3b9873dce1fdd94d7bb Mon Sep 17 00:00:00 2001 From: vil02 Date: Sat, 1 Jun 2024 22:37:23 +0200 Subject: [PATCH] style: include `SS_SHOULD_BE_STATIC` --- spotbugs-exclude.xml | 3 - .../conversions/HexaDecimalToBinary.java | 4 +- .../datastructures/trees/RedBlackBST.java | 92 +++++++++---------- 3 files changed, 47 insertions(+), 52 deletions(-) diff --git a/spotbugs-exclude.xml b/spotbugs-exclude.xml index 02e03725c429..18aaab94b435 100644 --- a/spotbugs-exclude.xml +++ b/spotbugs-exclude.xml @@ -77,9 +77,6 @@ - - - diff --git a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java index c766b83f1c7b..b6228488dc76 100644 --- a/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java +++ b/src/main/java/com/thealgorithms/conversions/HexaDecimalToBinary.java @@ -2,9 +2,6 @@ // Hex [0-9],[A-F] -> Binary [0,1] public class HexaDecimalToBinary { - - private final int longBits = 8; - public String convert(String numHex) { // String a HexaDecimal: int conHex = Integer.parseInt(numHex, 16); @@ -15,6 +12,7 @@ public String convert(String numHex) { } public String completeDigits(String binNum) { + final int longBits = 8; for (int i = binNum.length(); i < longBits; i++) { binNum = "0" + binNum; } diff --git a/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java b/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java index 2961282efe75..01222b739ff0 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java @@ -7,13 +7,13 @@ */ public class RedBlackBST { - private final int red = 0; - private final int black = 1; + private static final int RED = 0; + private static final int BLACK = 1; private class Node { int key = -1; - int color = black; + int color = BLACK; Node left = nil; Node right = nil; Node p = nil; @@ -31,7 +31,7 @@ public void printTree(Node node) { return; } printTree(node.left); - System.out.print(((node.color == red) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n"); + System.out.print(((node.color == RED) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n"); printTree(node.right); } @@ -39,7 +39,7 @@ public void printTreepre(Node node) { if (node == nil) { return; } - System.out.print(((node.color == red) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n"); + System.out.print(((node.color == RED) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n"); printTreepre(node.left); printTreepre(node.right); } @@ -66,10 +66,10 @@ private void insert(Node node) { Node temp = root; if (root == nil) { root = node; - node.color = black; + node.color = BLACK; node.p = nil; } else { - node.color = red; + node.color = RED; while (true) { if (node.key < temp.key) { if (temp.left == nil) { @@ -94,15 +94,15 @@ private void insert(Node node) { } private void fixTree(Node node) { - while (node.p.color == red) { + while (node.p.color == RED) { Node y = nil; if (node.p == node.p.p.left) { y = node.p.p.right; - if (y != nil && y.color == red) { - node.p.color = black; - y.color = black; - node.p.p.color = red; + if (y != nil && y.color == RED) { + node.p.color = BLACK; + y.color = BLACK; + node.p.p.color = RED; node = node.p.p; continue; } @@ -110,15 +110,15 @@ private void fixTree(Node node) { node = node.p; rotateLeft(node); } - node.p.color = black; - node.p.p.color = red; + node.p.color = BLACK; + node.p.p.color = RED; rotateRight(node.p.p); } else { y = node.p.p.left; - if (y != nil && y.color == red) { - node.p.color = black; - y.color = black; - node.p.p.color = red; + if (y != nil && y.color == RED) { + node.p.color = BLACK; + y.color = BLACK; + node.p.p.color = RED; node = node.p.p; continue; } @@ -126,12 +126,12 @@ private void fixTree(Node node) { node = node.p; rotateRight(node); } - node.p.color = black; - node.p.p.color = red; + node.p.color = BLACK; + node.p.p.color = RED; rotateLeft(node.p.p); } } - root.color = black; + root.color = BLACK; } void rotateLeft(Node node) { @@ -234,67 +234,67 @@ boolean delete(Node z) { y.left.p = y; y.color = z.color; } - if (yorigcolor == black) { + if (yorigcolor == BLACK) { deleteFixup(x); } return true; } void deleteFixup(Node x) { - while (x != root && x.color == black) { + while (x != root && x.color == BLACK) { if (x == x.p.left) { Node w = x.p.right; - if (w.color == red) { - w.color = black; - x.p.color = red; + if (w.color == RED) { + w.color = BLACK; + x.p.color = RED; rotateLeft(x.p); w = x.p.right; } - if (w.left.color == black && w.right.color == black) { - w.color = red; + if (w.left.color == BLACK && w.right.color == BLACK) { + w.color = RED; x = x.p; continue; - } else if (w.right.color == black) { - w.left.color = black; - w.color = red; + } else if (w.right.color == BLACK) { + w.left.color = BLACK; + w.color = RED; rotateRight(w); w = x.p.right; } - if (w.right.color == red) { + if (w.right.color == RED) { w.color = x.p.color; - x.p.color = black; - w.right.color = black; + x.p.color = BLACK; + w.right.color = BLACK; rotateLeft(x.p); x = root; } } else { Node w = x.p.left; - if (w.color == red) { - w.color = black; - x.p.color = red; + if (w.color == RED) { + w.color = BLACK; + x.p.color = RED; rotateRight(x.p); w = x.p.left; } - if (w.right.color == black && w.left.color == black) { - w.color = red; + if (w.right.color == BLACK && w.left.color == BLACK) { + w.color = RED; x = x.p; continue; - } else if (w.left.color == black) { - w.right.color = black; - w.color = red; + } else if (w.left.color == BLACK) { + w.right.color = BLACK; + w.color = RED; rotateLeft(w); w = x.p.left; } - if (w.left.color == red) { + if (w.left.color == RED) { w.color = x.p.color; - x.p.color = black; - w.left.color = black; + x.p.color = BLACK; + w.left.color = BLACK; rotateRight(x.p); x = root; } } } - x.color = black; + x.color = BLACK; } public void insertDemo() {