style: format code (#4212)

close #4204
This commit is contained in:
acbin
2023-06-09 18:52:05 +08:00
committed by GitHub
parent ad03086f54
commit 00282efd8b
521 changed files with 5233 additions and 7309 deletions

View File

@@ -47,10 +47,10 @@ 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
} else { // tree is not empty
min.setNext(toInsert);
this.updateMin(toInsert);
}
@@ -69,14 +69,14 @@ public class FibonacciHeap {
if (this.empty()) {
return;
}
if (this.numOfHeapNodes == 1) { //if there is only one tree
if (this.numOfHeapNodes == 1) { // if there is only one tree
this.min = null;
this.numOfTrees--;
this.numOfHeapNodes--;
return;
}
//change all children's parent to null//
if (this.min.child != null) { //min has a child
// change all children's parent to null//
if (this.min.child != null) { // min has a child
HeapNode child = this.min.child;
HeapNode tmpChild = child;
child.parent = null;
@@ -85,14 +85,14 @@ public class FibonacciHeap {
child.parent = null;
}
}
//delete the node//
// delete the node//
if (this.numOfTrees > 1) {
(this.min.prev).next = this.min.next;
(this.min.next).prev = this.min.prev;
if (this.min.child != null) {
(this.min.prev).setNext(this.min.child);
}
} else { //this.numOfTrees = 1
} else { // this.numOfTrees = 1
this.min = this.min.child;
}
this.numOfHeapNodes--;
@@ -136,17 +136,15 @@ public class FibonacciHeap {
}
/**
* Return a counters array, where the value of the i-th index is the number of trees with rank i in the heap.
* returns an empty array for an empty heap
* Return a counters array, where the value of the i-th index is the number of trees with rank i
* in the heap. returns an empty array for an empty heap
*/
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) {
@@ -163,8 +161,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
}
/**
@@ -176,13 +174,13 @@ 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;
}
if (x.getKey() >= x.parent.getKey()) {
return;
} //we don't need to cut
} // we don't need to cut
HeapNode prevParent = x.parent;
this.cut(x);
this.cascadingCuts(prevParent);
@@ -197,17 +195,18 @@ public class FibonacciHeap {
}
/**
* This static function returns the total number of link operations made during the run-time of the program.
* A link operation is the operation which gets as input two trees of the same rank, and generates a tree of
* rank bigger by one.
* This static function returns the total number of link operations made during the run-time of
* the program. A link operation is the operation which gets as input two trees of the same
* rank, and generates a tree of rank bigger by one.
*/
public static int totalLinks() {
return totalLinks;
}
/**
* This static function returns the total number of cut operations made during the run-time of the program.
* A cut operation is the operation which disconnects a subtree from its parent (during decreaseKey/delete methods).
* This static function returns the total number of cut operations made during the run-time of
* the program. A cut operation is the operation which disconnects a subtree from its parent
* (during decreaseKey/delete methods).
*/
public static int totalCuts() {
return totalCuts;
@@ -231,7 +230,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++;
} else {
@@ -255,10 +254,10 @@ public class FibonacciHeap {
this.markedHeapNoodesCounter--;
curr.marked = false;
}
if (curr.parent.child == curr) { //we should change the parent's child
if (curr.next == curr) { //curr do not have brothers
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;
}
}
@@ -285,10 +284,8 @@ 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) {
@@ -398,7 +395,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

@@ -45,16 +45,10 @@ public class GenericHeap<T extends Comparable<T>> {
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
) {
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
) {
if (rci < this.size() && isLarger(this.data.get(rci), this.data.get(mini)) > 0) {
mini = rci;
}
if (mini != pi) {
@@ -67,7 +61,7 @@ public class GenericHeap<T extends Comparable<T>> {
return this.data.get(0);
}
//t has higher property then return +ve
// t has higher property then return +ve
private int isLarger(T t, T o) {
return t.compareTo(o);
}
@@ -83,7 +77,7 @@ public class GenericHeap<T extends Comparable<T>> {
public void updatePriority(T item) {
int index = map.get(item);
//because we enter lesser value then old vale
// because we enter lesser value then old vale
upHeapify(index);
}
}

View File

@@ -122,10 +122,8 @@ 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;
}
@@ -134,10 +132,7 @@ 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

@@ -2,117 +2,113 @@ package com.thealgorithms.datastructures.heaps;
import java.util.ArrayList;
/*
/*
* This is a leftist heap that follows the same operations as a
* binary min heap, but may be unbalanced at times and follows a
* leftist property, in which the left side is more heavy on the
* right based on the null-path length (npl) values.
*
*
* Source: https://iq.opengenus.org/leftist-heap/
*
*
*/
public class LeftistHeap {
private class Node {
private int element, npl;
private Node left, right;
private class Node {
private int element, npl;
private Node left, right;
// Node constructor setting the data element and left/right pointers to null
private Node(int element) {
this.element = element;
left = right = null;
npl = 0;
}
}
// Node constructor setting the data element and left/right pointers to null
private Node(int element) {
this.element = element;
left = right = null;
npl = 0;
}
}
private Node root;
private Node root;
// Constructor
public LeftistHeap() {
root = null;
}
// Constructor
public LeftistHeap() {
root = null;
}
// Checks if heap is empty
public boolean isEmpty() {
return root == null;
}
// Checks if heap is empty
public boolean isEmpty() {
return root == null;
}
// Resets structure to initial state
public void clear() {
// We will put head is null
root = null;
}
// Resets structure to initial state
public void clear() {
// We will put head is null
root = null;
}
// Merge function that merges the contents of another leftist heap with the
// current one
public void merge(LeftistHeap h1) {
// If the present function is rhs then we ignore the merge
root = merge(root, h1.root);
h1.root = null;
}
// Merge function that merges the contents of another leftist heap with the
// current one
public void merge(LeftistHeap h1) {
// If the present function is rhs then we ignore the merge
root = merge(root, h1.root);
h1.root = null;
}
// Function merge with two Nodes a and b
public Node merge(Node a, Node b) {
if (a == null)
return b;
// Function merge with two Nodes a and b
public Node merge(Node a, Node 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) {
Node temp = a;
a = b;
b = temp;
}
// Violates leftist property, so must do a swap
if (a.element > b.element) {
Node temp = a;
a = b;
b = temp;
}
// Now we call the function merge to merge a and b
a.right = merge(a.right, b);
// Now we call the function merge to merge a and b
a.right = merge(a.right, b);
// Violates leftist property so must swap here
if (a.left == null) {
a.left = a.right;
a.right = null;
} else {
if (a.left.npl < a.right.npl) {
Node temp = a.left;
a.left = a.right;
a.right = temp;
}
a.npl = a.right.npl + 1;
}
return a;
}
// Violates leftist property so must swap here
if (a.left == null) {
a.left = a.right;
a.right = null;
} else {
if (a.left.npl < a.right.npl) {
Node temp = a.left;
a.left = a.right;
a.right = temp;
}
a.npl = a.right.npl + 1;
}
return a;
}
// Function insert. Uses the merge function to add the data
public void insert(int a) {
root = merge(new Node(a), root);
}
// Function insert. Uses the merge function to add the data
public void insert(int a) {
root = merge(new Node(a), root);
}
// Returns and removes the minimum element in the heap
public int extract_min() {
// If is empty return -1
if (isEmpty())
return -1;
// Returns and removes the minimum element in the heap
public int extract_min() {
// If is empty return -1
if (isEmpty()) return -1;
int min = root.element;
root = merge(root.left, root.right);
return min;
}
int min = root.element;
root = merge(root.left, root.right);
return min;
}
// Function returning a list of an in order traversal of the data structure
public ArrayList<Integer> in_order() {
ArrayList<Integer> lst = new ArrayList<>();
in_order_aux(root, lst);
return new ArrayList<>(lst);
}
// Function returning a list of an in order traversal of the data structure
public ArrayList<Integer> in_order() {
ArrayList<Integer> lst = new ArrayList<>();
in_order_aux(root, lst);
return new ArrayList<>(lst);
}
// Auxiliary function for in_order
private void in_order_aux(Node n, ArrayList<Integer> lst) {
if (n == null)
return;
in_order_aux(n.left, lst);
lst.add(n.element);
in_order_aux(n.right, lst);
}
// Auxiliary function for in_order
private void in_order_aux(Node n, ArrayList<Integer> lst) {
if (n == null) return;
in_order_aux(n.left, lst);
lst.add(n.element);
in_order_aux(n.right, lst);
}
}

View File

@@ -70,30 +70,20 @@ 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())));
}
}
@@ -112,12 +102,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"
);
} catch (EmptyHeapException e) {
e.printStackTrace();
}
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");
}
@@ -125,22 +113,13 @@ 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);
}
}
@@ -150,9 +129,7 @@ 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

@@ -64,30 +64,20 @@ 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())));
}
}
@@ -106,12 +96,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"
);
} catch (EmptyHeapException e) {
e.printStackTrace();
}
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");
}
@@ -119,22 +107,13 @@ 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);
}
}
@@ -144,9 +123,7 @@ 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

@@ -82,10 +82,7 @@ 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;
@@ -94,14 +91,8 @@ 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;
}