mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 14:04:17 +08:00
Remove unnecessary code (#4141)
This commit is contained in:
@ -74,7 +74,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
|
||||
for (i = 0; i < v - 1; i++) {
|
||||
for (j = 0; j < e; j++) {
|
||||
if (
|
||||
(int) dist[arr[j].u] != Integer.MAX_VALUE &&
|
||||
dist[arr[j].u] != Integer.MAX_VALUE &&
|
||||
dist[arr[j].v] > dist[arr[j].u] + arr[j].w
|
||||
) {
|
||||
dist[arr[j].v] = dist[arr[j].u] + arr[j].w; // Update
|
||||
@ -85,7 +85,7 @@ start vertex, end vertex and weights. Vertices should be labelled with a number
|
||||
// Final cycle for negative checking
|
||||
for (j = 0; j < e; j++) {
|
||||
if (
|
||||
(int) dist[arr[j].u] != Integer.MAX_VALUE &&
|
||||
dist[arr[j].u] != Integer.MAX_VALUE &&
|
||||
dist[arr[j].v] > dist[arr[j].u] + arr[j].w
|
||||
) {
|
||||
neg = 1;
|
||||
|
@ -28,7 +28,7 @@ public class BipartiteGrapfDFS {
|
||||
for (Integer it : adj.get(node)) {
|
||||
if (color[it] == -1) {
|
||||
color[it] = 1 - color[node];
|
||||
if (bipartite(V, adj, color, it) == false) {
|
||||
if (!bipartite(V, adj, color, it)) {
|
||||
return false;
|
||||
}
|
||||
} else if (color[it] == color[node]) {
|
||||
|
@ -12,7 +12,7 @@ class dijkstras {
|
||||
int min = Integer.MAX_VALUE, min_index = -1;
|
||||
|
||||
for (int r = 0; r < k; r++) {
|
||||
if (Set[r] == false && dist[r] <= min) {
|
||||
if (!Set[r] && dist[r] <= min) {
|
||||
min = dist[r];
|
||||
min_index = r;
|
||||
}
|
||||
|
@ -154,11 +154,7 @@ class AdjacencyMatrixGraph {
|
||||
* @return whether or not the vertex exists
|
||||
*/
|
||||
public boolean vertexDoesExist(int aVertex) {
|
||||
if (aVertex >= 0 && aVertex < this.numberOfVertices()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return aVertex >= 0 && aVertex < this.numberOfVertices();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -343,14 +339,14 @@ class AdjacencyMatrixGraph {
|
||||
public String toString() {
|
||||
String s = " ";
|
||||
for (int i = 0; i < this.numberOfVertices(); i++) {
|
||||
s = s + String.valueOf(i) + " ";
|
||||
s = s + i + " ";
|
||||
}
|
||||
s = s + " \n";
|
||||
|
||||
for (int i = 0; i < this.numberOfVertices(); i++) {
|
||||
s = s + String.valueOf(i) + " : ";
|
||||
s = s + i + " : ";
|
||||
for (int j = 0; j < this.numberOfVertices(); j++) {
|
||||
s = s + String.valueOf(this._adjacency[i][j]) + " ";
|
||||
s = s + this._adjacency[i][j] + " ";
|
||||
}
|
||||
s = s + "\n";
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ class PrimMST {
|
||||
int min = Integer.MAX_VALUE, min_index = -1;
|
||||
|
||||
for (int v = 0; v < V; v++) {
|
||||
if (mstSet[v] == false && key[v] < min) {
|
||||
if (!mstSet[v] && key[v] < min) {
|
||||
min = key[v];
|
||||
min_index = v;
|
||||
}
|
||||
@ -80,7 +80,7 @@ class PrimMST {
|
||||
{
|
||||
if (
|
||||
graph[u][v] != 0 &&
|
||||
mstSet[v] == false &&
|
||||
!mstSet[v] &&
|
||||
graph[u][v] < key[v]
|
||||
) {
|
||||
parent[v] = u;
|
||||
|
@ -114,7 +114,7 @@ public class TarjansAlgorithm {
|
||||
stronglyConnCompsUtil(n, lowTime, insertionTime, isInStack, st, graph);
|
||||
//update lowTime for the current node comparing lowtime of adj node
|
||||
lowTime[u] = Math.min(lowTime[u], lowTime[n]);
|
||||
} else if (isInStack[n] == true) {
|
||||
} else if (isInStack[n]) {
|
||||
//If adj node is in stack, update low
|
||||
lowTime[u] = Math.min(lowTime[u], insertionTime[n]);
|
||||
}
|
||||
|
@ -234,7 +234,6 @@ public class FibonacciHeap {
|
||||
if (!curr.isMarked()) { //stop the recursion
|
||||
curr.mark();
|
||||
if (!curr.isRoot()) this.markedHeapNoodesCounter++;
|
||||
return;
|
||||
} else {
|
||||
if (curr.isRoot()) {
|
||||
return;
|
||||
|
@ -51,18 +51,12 @@ public class MinPriorityQueue {
|
||||
|
||||
// returns boolean value whether the heap is empty or not
|
||||
public boolean isEmpty() {
|
||||
if (0 == this.size) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return 0 == this.size;
|
||||
}
|
||||
|
||||
// returns boolean value whether the heap is full or not
|
||||
public boolean isFull() {
|
||||
if (this.size == this.capacity) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return this.size == this.capacity;
|
||||
}
|
||||
|
||||
// prints the heap
|
||||
|
@ -174,8 +174,7 @@ public class CursorLinkedList<T> {
|
||||
}
|
||||
|
||||
// 2- make the os point to the next of the @var{availableNodeIndex}
|
||||
int availableNext = cursorSpace[availableNodeIndex].next;
|
||||
cursorSpace[os].next = availableNext;
|
||||
cursorSpace[os].next = cursorSpace[availableNodeIndex].next;
|
||||
|
||||
// this to indicate an end of the list , helpful at testing since any err
|
||||
// would throw an outOfBoundException
|
||||
|
@ -407,7 +407,7 @@ public class SinglyLinkedList extends Node {
|
||||
list.insert(3);
|
||||
list.insertNth(1, 4);
|
||||
assert list.toString().equals("10->7->5->3->1");
|
||||
System.out.println(list.toString());
|
||||
System.out.println(list);
|
||||
/* Test search function */
|
||||
assert list.search(10) &&
|
||||
list.search(5) &&
|
||||
@ -424,7 +424,7 @@ public class SinglyLinkedList extends Node {
|
||||
list.deleteNth(1);
|
||||
list.delete();
|
||||
assert list.toString().equals("7->3");
|
||||
System.out.println(list.toString());
|
||||
System.out.println(list);
|
||||
assert list.size == 2 && list.size() == list.count();
|
||||
|
||||
list.clear();
|
||||
|
@ -17,21 +17,13 @@ public class CircularQueue {
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
if (beginningOfQueue == -1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return beginningOfQueue == -1;
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
if (topOfQueue + 1 == beginningOfQueue) {
|
||||
return true;
|
||||
} else if (topOfQueue == size - 1 && beginningOfQueue == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else return topOfQueue == size - 1 && beginningOfQueue == 0;
|
||||
}
|
||||
|
||||
public void enQueue(int value) {
|
||||
|
@ -91,13 +91,12 @@ public class Deques<T> {
|
||||
if (tail == null) {
|
||||
// If the deque is empty, add the node as the head and tail
|
||||
head = newNode;
|
||||
tail = newNode;
|
||||
} else {
|
||||
// If the deque is not empty, insert the node as the new tail
|
||||
newNode.prev = tail;
|
||||
tail.next = newNode;
|
||||
tail = newNode;
|
||||
}
|
||||
tail = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
@ -177,6 +177,6 @@ public class Queues {
|
||||
|
||||
System.out.println(myQueue.peekFront()); // Will print 2
|
||||
System.out.println(myQueue.peekRear()); // Will print 7
|
||||
System.out.println(myQueue.toString()); // Will print [2, 5, 3, 7]
|
||||
System.out.println(myQueue); // Will print [2, 5, 3, 7]
|
||||
}
|
||||
}
|
||||
|
@ -49,8 +49,7 @@ public class AVLSimple {
|
||||
|
||||
private Node insert(Node node, int item) {
|
||||
if (node == null) {
|
||||
Node add = new Node(item);
|
||||
return add;
|
||||
return new Node(item);
|
||||
}
|
||||
if (node.data > item) {
|
||||
node.left = insert(node.left, item);
|
||||
|
@ -62,21 +62,20 @@ public class AVLTree {
|
||||
}
|
||||
return;
|
||||
}
|
||||
Node child;
|
||||
if (node.left != null) {
|
||||
Node child = node.left;
|
||||
child = node.left;
|
||||
while (child.right != null) {
|
||||
child = child.right;
|
||||
}
|
||||
node.key = child.key;
|
||||
delete(child);
|
||||
} else {
|
||||
Node child = node.right;
|
||||
child = node.right;
|
||||
while (child.left != null) {
|
||||
child = child.left;
|
||||
}
|
||||
node.key = child.key;
|
||||
delete(child);
|
||||
}
|
||||
node.key = child.key;
|
||||
delete(child);
|
||||
}
|
||||
|
||||
public void delete(int delKey) {
|
||||
@ -216,11 +215,7 @@ public class AVLTree {
|
||||
|
||||
public boolean search(int key) {
|
||||
Node result = searchHelper(this.root, key);
|
||||
if (result != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return result != null;
|
||||
}
|
||||
|
||||
private Node searchHelper(Node root, int key) {
|
||||
|
@ -117,11 +117,9 @@ public class BinaryTree {
|
||||
if (value < parent.data) {
|
||||
parent.left = newNode;
|
||||
parent.left.parent = parent;
|
||||
return;
|
||||
} else {
|
||||
parent.right = newNode;
|
||||
parent.right.parent = parent;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -177,7 +175,6 @@ public class BinaryTree {
|
||||
if (temp == root) {
|
||||
successor.parent = null;
|
||||
root = successor;
|
||||
return true;
|
||||
} // If you're not deleting the root
|
||||
else {
|
||||
successor.parent = temp.parent;
|
||||
@ -188,8 +185,8 @@ public class BinaryTree {
|
||||
} else {
|
||||
temp.parent.left = successor;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
} // One child
|
||||
else {
|
||||
// If it has a right child
|
||||
@ -207,7 +204,6 @@ public class BinaryTree {
|
||||
} else {
|
||||
temp.parent.right = temp.right;
|
||||
}
|
||||
return true;
|
||||
} // If it has a left child
|
||||
else {
|
||||
if (temp == root) {
|
||||
@ -223,8 +219,8 @@ public class BinaryTree {
|
||||
} else {
|
||||
temp.parent.right = temp.left;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -168,7 +168,6 @@ public class GenericTree {
|
||||
for (int i = 0; i < node.child.size(); i++) {
|
||||
depth(node.child.get(i), dep - 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -309,23 +309,20 @@ public class RedBlackBST {
|
||||
|
||||
public void insertDemo() {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
while (true) {
|
||||
System.out.println("Add items");
|
||||
System.out.println("Add items");
|
||||
|
||||
int item;
|
||||
Node node;
|
||||
int item;
|
||||
Node node;
|
||||
|
||||
item = scan.nextInt();
|
||||
while (item != -999) {
|
||||
node = new Node(item);
|
||||
insert(node);
|
||||
item = scan.nextInt();
|
||||
while (item != -999) {
|
||||
node = new Node(item);
|
||||
insert(node);
|
||||
item = scan.nextInt();
|
||||
}
|
||||
printTree(root);
|
||||
System.out.println("Pre order");
|
||||
printTreepre(root);
|
||||
break;
|
||||
}
|
||||
printTree(root);
|
||||
System.out.println("Pre order");
|
||||
printTreepre(root);
|
||||
scan.close();
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,7 @@ public class TrieImp {
|
||||
}
|
||||
currentNode = node;
|
||||
}
|
||||
if (currentNode.end == true) {
|
||||
if (currentNode.end) {
|
||||
currentNode.end = false;
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user