Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@@ -1,6 +1,5 @@
package com.thealgorithms.datastructures.heaps;
public class FibonacciHeap {
private static final double GOLDEN_RATIO = (1 + Math.sqrt(5)) / 2;
@@ -48,7 +47,7 @@ public class FibonacciHeap {
* $ret = the HeapNode we inserted
*/
public HeapNode insert(int key) {
HeapNode toInsert = new HeapNode(key); //creates the node
HeapNode toInsert = new HeapNode(key); //creates the node
if (this.empty()) {
this.min = toInsert;
} else { //tree is not empty
@@ -142,9 +141,12 @@ public class FibonacciHeap {
*/
public int[] countersRep() {
if (this.empty()) {
return new int[0]; ///return an empty array
return new int[0]; ///return an empty array
}
int[] rankArray = new int[(int) Math.floor(Math.log(this.size()) / Math.log(GOLDEN_RATIO)) + 1]; //creates the array
int[] rankArray = new int[(int) Math.floor(
Math.log(this.size()) / Math.log(GOLDEN_RATIO)
) +
1]; //creates the array
rankArray[this.min.rank]++;
HeapNode curr = this.min.next;
while (curr != this.min) {
@@ -161,8 +163,8 @@ public class FibonacciHeap {
* @post (numOfnodes = = $prev numOfnodes - 1)
*/
public void delete(HeapNode x) {
this.decreaseKey(x, x.getKey() + 1); //change key to be the minimal (-1)
this.deleteMin(); //delete it
this.decreaseKey(x, x.getKey() + 1); //change key to be the minimal (-1)
this.deleteMin(); //delete it
}
/**
@@ -174,7 +176,7 @@ public class FibonacciHeap {
private void decreaseKey(HeapNode x, int delta) {
int newKey = x.getKey() - delta;
x.key = newKey;
if (x.isRoot()) {//no parent to x
if (x.isRoot()) { //no parent to x
this.updateMin(x);
return;
}
@@ -229,7 +231,7 @@ public class FibonacciHeap {
* @post (numOfnodes == $prev numOfnodes)
*/
private void cascadingCuts(HeapNode curr) {
if (!curr.isMarked()) { //stop the recursion
if (!curr.isMarked()) { //stop the recursion
curr.mark();
if (!curr.isRoot()) this.markedHeapNoodesCounter++;
return;
@@ -257,7 +259,7 @@ public class FibonacciHeap {
if (curr.parent.child == curr) { //we should change the parent's child
if (curr.next == curr) { //curr do not have brothers
curr.parent.child = null;
} else {//curr have brothers
} else { //curr have brothers
curr.parent.child = curr.next;
}
}
@@ -272,7 +274,6 @@ public class FibonacciHeap {
totalCuts++;
}
/*
*
*/
@@ -285,7 +286,10 @@ public class FibonacciHeap {
*
*/
private HeapNode[] toBuckets(HeapNode curr) {
HeapNode[] buckets = new HeapNode[(int) Math.floor(Math.log(this.size()) / Math.log(GOLDEN_RATIO)) + 1];
HeapNode[] buckets = new HeapNode[(int) Math.floor(
Math.log(this.size()) / Math.log(GOLDEN_RATIO)
) +
1];
curr.prev.next = null;
HeapNode tmpCurr;
while (curr != null) {
@@ -347,7 +351,6 @@ public class FibonacciHeap {
return c1;
}
/**
* public class HeapNode
* each HeapNode belongs to a heap (Inner class)
@@ -381,7 +384,6 @@ public class FibonacciHeap {
return this.key;
}
/*
* checks whether the node is marked
* $ret = true if one child has been cut
@@ -397,7 +399,7 @@ public class FibonacciHeap {
private void mark() {
if (this.isRoot()) {
return;
} //check if the node is a root
} //check if the node is a root
this.marked = true;
}

View File

@@ -2,70 +2,88 @@ package com.thealgorithms.datastructures.heaps;
import java.util.*;
public class GenericHeap <T extends Comparable <T> >{
ArrayList <T> data=new ArrayList<>();
HashMap<T,Integer> map=new HashMap<>();
public void add(T item) {
this.data.add(item);
map.put(item,this.data.size()-1);//
upHeapify(this.data.size()-1);
}
private void upHeapify(int ci) {
int pi=(ci-1)/2;
if(isLarger(this.data.get(ci),this.data.get(pi))>0) {
swap(pi,ci);
upHeapify(pi);
}
}
public void display() {
System.out.println(this.data);
}
public int size() {
return this.data.size();
}
public boolean isEmpty() {
return this.size()==0;
}
public T remove() {
this.swap(0,this.size()-1);
T rv=this.data.remove(this.size()-1);
downHeapify(0);
map.remove(rv);
return rv;
}
private void downHeapify(int pi) {
int lci=2*pi+1;
int rci=2*pi+2;
int mini=pi;
if(lci<this.size() && isLarger(this.data.get(lci),this.data.get(mini))>0) {
mini=lci;
}
if(rci<this.size() && isLarger(this.data.get(rci),this.data.get(mini))>0) {
mini=rci;
}
if(mini!=pi) {
this.swap(pi,mini);
downHeapify(mini);
}
}
public T get() {
return this.data.get(0);
}
//t has higher property then return +ve
private int isLarger(T t,T o) {
return t.compareTo(o);
}
private void swap(int i,int j) {
T ith=this.data.get(i);
T jth=this.data.get(j);
this.data.set(i,jth);
this.data.set(j,ith);
map.put(ith,j);
map.put(jth,i);
}
public void updatePriority(T item) {
int index=map.get(item);
//because we enter lesser value then old vale
upHeapify(index);
}
public class GenericHeap<T extends Comparable<T>> {
ArrayList<T> data = new ArrayList<>();
HashMap<T, Integer> map = new HashMap<>();
public void add(T item) {
this.data.add(item);
map.put(item, this.data.size() - 1); //
upHeapify(this.data.size() - 1);
}
private void upHeapify(int ci) {
int pi = (ci - 1) / 2;
if (isLarger(this.data.get(ci), this.data.get(pi)) > 0) {
swap(pi, ci);
upHeapify(pi);
}
}
public void display() {
System.out.println(this.data);
}
public int size() {
return this.data.size();
}
public boolean isEmpty() {
return this.size() == 0;
}
public T remove() {
this.swap(0, this.size() - 1);
T rv = this.data.remove(this.size() - 1);
downHeapify(0);
map.remove(rv);
return rv;
}
private void downHeapify(int pi) {
int lci = 2 * pi + 1;
int rci = 2 * pi + 2;
int mini = pi;
if (
lci < this.size() &&
isLarger(this.data.get(lci), this.data.get(mini)) > 0
) {
mini = lci;
}
if (
rci < this.size() &&
isLarger(this.data.get(rci), this.data.get(mini)) > 0
) {
mini = rci;
}
if (mini != pi) {
this.swap(pi, mini);
downHeapify(mini);
}
}
public T get() {
return this.data.get(0);
}
//t has higher property then return +ve
private int isLarger(T t, T o) {
return t.compareTo(o);
}
private void swap(int i, int j) {
T ith = this.data.get(i);
T jth = this.data.get(j);
this.data.set(i, jth);
this.data.set(j, ith);
map.put(ith, j);
map.put(jth, i);
}
public void updatePriority(T item) {
int index = map.get(item);
//because we enter lesser value then old vale
upHeapify(index);
}
}

View File

@@ -19,7 +19,6 @@ package com.thealgorithms.datastructures.heaps;
* @author Nicolas Renard
*/
public interface Heap {
/**
* @return the top element in the heap, the one with lowest key for min-heap
* or with the highest key for max-heap

View File

@@ -122,8 +122,10 @@ public class HeapElement {
return false;
}
HeapElement otherHeapElement = (HeapElement) o;
return (this.key == otherHeapElement.key)
&& (this.additionalInfo.equals(otherHeapElement.additionalInfo));
return (
(this.key == otherHeapElement.key) &&
(this.additionalInfo.equals(otherHeapElement.additionalInfo))
);
}
return false;
}
@@ -132,7 +134,10 @@ public class HeapElement {
public int hashCode() {
int result = 0;
result = 31 * result + (int) key;
result = 31 * result + (additionalInfo != null ? additionalInfo.hashCode() : 0);
result =
31 *
result +
(additionalInfo != null ? additionalInfo.hashCode() : 0);
return result;
}
}

View File

@@ -46,7 +46,7 @@ public class MaxHeap implements Heap {
if ((elementIndex <= 0) || (elementIndex > maxHeap.size())) {
throw new IndexOutOfBoundsException("Index out of heap range");
}
return maxHeap.get(elementIndex - 1).getKey();
}
@@ -70,22 +70,30 @@ public class MaxHeap implements Heap {
// than any of its children's
private void toggleDown(int elementIndex) {
double key = maxHeap.get(elementIndex - 1).getKey();
boolean wrongOrder
= (key < getElementKey(elementIndex * 2))
|| (key < getElementKey(Math.min(elementIndex * 2, maxHeap.size())));
boolean wrongOrder =
(key < getElementKey(elementIndex * 2)) ||
(key < getElementKey(Math.min(elementIndex * 2, maxHeap.size())));
while ((2 * elementIndex <= maxHeap.size()) && wrongOrder) {
// Check whether it shall swap the element with its left child or its right one if any.
if ((2 * elementIndex < maxHeap.size())
&& (getElementKey(elementIndex * 2 + 1) > getElementKey(elementIndex * 2))) {
if (
(2 * elementIndex < maxHeap.size()) &&
(
getElementKey(elementIndex * 2 + 1) >
getElementKey(elementIndex * 2)
)
) {
swap(elementIndex, 2 * elementIndex + 1);
elementIndex = 2 * elementIndex + 1;
} else {
swap(elementIndex, 2 * elementIndex);
elementIndex = 2 * elementIndex;
}
wrongOrder
= (key < getElementKey(elementIndex * 2))
|| (key < getElementKey(Math.min(elementIndex * 2, maxHeap.size())));
wrongOrder =
(key < getElementKey(elementIndex * 2)) ||
(
key <
getElementKey(Math.min(elementIndex * 2, maxHeap.size()))
);
}
}
@@ -103,9 +111,10 @@ public class MaxHeap implements Heap {
@Override
public void deleteElement(int elementIndex) {
if (maxHeap.isEmpty())
try {
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
if (maxHeap.isEmpty()) try {
throw new EmptyHeapException(
"Attempt to delete an element from an empty heap"
);
} catch (EmptyHeapException e) {
e.printStackTrace();
}
@@ -116,13 +125,22 @@ public class MaxHeap implements Heap {
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
maxHeap.remove(maxHeap.size());
// Shall the new element be moved up...
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2.0))) {
if (
getElementKey(elementIndex) >
getElementKey((int) Math.floor(elementIndex / 2.0))
) {
toggleUp(elementIndex);
} // ... or down ?
else if (((2 * elementIndex <= maxHeap.size())
&& (getElementKey(elementIndex) < getElementKey(elementIndex * 2)))
|| ((2 * elementIndex < maxHeap.size())
&& (getElementKey(elementIndex) < getElementKey(elementIndex * 2)))) {
else if (
(
(2 * elementIndex <= maxHeap.size()) &&
(getElementKey(elementIndex) < getElementKey(elementIndex * 2))
) ||
(
(2 * elementIndex < maxHeap.size()) &&
(getElementKey(elementIndex) < getElementKey(elementIndex * 2))
)
) {
toggleDown(elementIndex);
}
}
@@ -132,7 +150,9 @@ public class MaxHeap implements Heap {
try {
return extractMax();
} catch (Exception e) {
throw new EmptyHeapException("Heap is empty. Error retrieving element");
throw new EmptyHeapException(
"Heap is empty. Error retrieving element"
);
}
}
}

View File

@@ -40,7 +40,7 @@ public class MinHeap implements Heap {
if ((elementIndex <= 0) || (elementIndex > minHeap.size())) {
throw new IndexOutOfBoundsException("Index out of heap range");
}
return minHeap.get(elementIndex - 1).getKey();
}
@@ -64,22 +64,30 @@ public class MinHeap implements Heap {
// than any of its children's
private void toggleDown(int elementIndex) {
double key = minHeap.get(elementIndex - 1).getKey();
boolean wrongOrder
= (key > getElementKey(elementIndex * 2))
|| (key > getElementKey(Math.min(elementIndex * 2, minHeap.size())));
boolean wrongOrder =
(key > getElementKey(elementIndex * 2)) ||
(key > getElementKey(Math.min(elementIndex * 2, minHeap.size())));
while ((2 * elementIndex <= minHeap.size()) && wrongOrder) {
// Check whether it shall swap the element with its left child or its right one if any.
if ((2 * elementIndex < minHeap.size())
&& (getElementKey(elementIndex * 2 + 1) < getElementKey(elementIndex * 2))) {
if (
(2 * elementIndex < minHeap.size()) &&
(
getElementKey(elementIndex * 2 + 1) <
getElementKey(elementIndex * 2)
)
) {
swap(elementIndex, 2 * elementIndex + 1);
elementIndex = 2 * elementIndex + 1;
} else {
swap(elementIndex, 2 * elementIndex);
elementIndex = 2 * elementIndex;
}
wrongOrder
= (key > getElementKey(elementIndex * 2))
|| (key > getElementKey(Math.min(elementIndex * 2, minHeap.size())));
wrongOrder =
(key > getElementKey(elementIndex * 2)) ||
(
key >
getElementKey(Math.min(elementIndex * 2, minHeap.size()))
);
}
}
@@ -97,9 +105,10 @@ public class MinHeap implements Heap {
@Override
public void deleteElement(int elementIndex) {
if (minHeap.isEmpty())
try {
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
if (minHeap.isEmpty()) try {
throw new EmptyHeapException(
"Attempt to delete an element from an empty heap"
);
} catch (EmptyHeapException e) {
e.printStackTrace();
}
@@ -110,13 +119,22 @@ public class MinHeap implements Heap {
minHeap.set(elementIndex - 1, getElement(minHeap.size()));
minHeap.remove(minHeap.size());
// Shall the new element be moved up...
if (getElementKey(elementIndex) < getElementKey((int) Math.floor(elementIndex / 2.0))) {
if (
getElementKey(elementIndex) <
getElementKey((int) Math.floor(elementIndex / 2.0))
) {
toggleUp(elementIndex);
} // ... or down ?
else if (((2 * elementIndex <= minHeap.size())
&& (getElementKey(elementIndex) > getElementKey(elementIndex * 2)))
|| ((2 * elementIndex < minHeap.size())
&& (getElementKey(elementIndex) > getElementKey(elementIndex * 2)))) {
else if (
(
(2 * elementIndex <= minHeap.size()) &&
(getElementKey(elementIndex) > getElementKey(elementIndex * 2))
) ||
(
(2 * elementIndex < minHeap.size()) &&
(getElementKey(elementIndex) > getElementKey(elementIndex * 2))
)
) {
toggleDown(elementIndex);
}
}
@@ -126,7 +144,9 @@ public class MinHeap implements Heap {
try {
return extractMin();
} catch (Exception e) {
throw new EmptyHeapException("Heap is empty. Error retrieving element");
throw new EmptyHeapException(
"Heap is empty. Error retrieving element"
);
}
}
}

View File

@@ -88,7 +88,10 @@ public class MinPriorityQueue {
while (2 * k <= this.size || 2 * k + 1 <= this.size) {
int minIndex;
if (this.heap[2 * k] >= this.heap[k]) {
if (2 * k + 1 <= this.size && this.heap[2 * k + 1] >= this.heap[k]) {
if (
2 * k + 1 <= this.size &&
this.heap[2 * k + 1] >= this.heap[k]
) {
break;
} else if (2 * k + 1 > this.size) {
break;
@@ -97,8 +100,14 @@ public class MinPriorityQueue {
if (2 * k + 1 > this.size) {
minIndex = this.heap[2 * k] < this.heap[k] ? 2 * k : k;
} else {
if (this.heap[k] > this.heap[2 * k] || this.heap[k] > this.heap[2 * k + 1]) {
minIndex = this.heap[2 * k] < this.heap[2 * k + 1] ? 2 * k : 2 * k + 1;
if (
this.heap[k] > this.heap[2 * k] ||
this.heap[k] > this.heap[2 * k + 1]
) {
minIndex =
this.heap[2 * k] < this.heap[2 * k + 1]
? 2 * k
: 2 * k + 1;
} else {
minIndex = k;
}