chore: improve FibonacciHeap (#5251)

This commit is contained in:
abdala-elgendy 2024-06-24 11:42:29 +03:00 committed by GitHub
parent a5b4c6173f
commit 7b17ead902
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 9 additions and 9 deletions

View File

@ -8,7 +8,7 @@ public class FibonacciHeap {
private static int totalCuts = 0; private static int totalCuts = 0;
private int numOfTrees = 0; private int numOfTrees = 0;
private int numOfHeapNodes = 0; private int numOfHeapNodes = 0;
private int markedHeapNoodesCounter = 0; private int markedHeapNodesCounter = 0;
/* /*
* a constructor for an empty Heap * a constructor for an empty Heap
@ -190,7 +190,7 @@ public class FibonacciHeap {
* Potential = #trees + 2*#markedNodes * Potential = #trees + 2*#markedNodes
*/ */
public int potential() { public int potential() {
return numOfTrees + (2 * markedHeapNoodesCounter); return numOfTrees + (2 * markedHeapNodesCounter);
} }
/** /**
@ -232,7 +232,7 @@ public class FibonacciHeap {
if (!curr.isMarked()) { // stop the recursion if (!curr.isMarked()) { // stop the recursion
curr.mark(); curr.mark();
if (!curr.isRoot()) { if (!curr.isRoot()) {
this.markedHeapNoodesCounter++; this.markedHeapNodesCounter++;
} }
} else { } else {
if (curr.isRoot()) { if (curr.isRoot()) {
@ -252,7 +252,7 @@ public class FibonacciHeap {
private void cut(HeapNode curr) { private void cut(HeapNode curr) {
curr.parent.rank--; curr.parent.rank--;
if (curr.marked) { if (curr.marked) {
this.markedHeapNoodesCounter--; this.markedHeapNodesCounter--;
curr.marked = false; curr.marked = false;
} }
if (curr.parent.child == curr) { // we should change the parent's child if (curr.parent.child == curr) { // we should change the parent's child

View File

@ -22,7 +22,7 @@ public class MaxHeap implements Heap {
System.out.println("Null element. Not added to heap"); System.out.println("Null element. Not added to heap");
} }
} }
if (maxHeap.size() == 0) { if (maxHeap.isEmpty()) {
System.out.println("No element has been added, empty heap."); System.out.println("No element has been added, empty heap.");
} }
} }

View File

@ -22,7 +22,7 @@ public class MinHeap implements Heap {
System.out.println("Null element. Not added to heap"); System.out.println("Null element. Not added to heap");
} }
} }
if (minHeap.size() == 0) { if (minHeap.isEmpty()) {
System.out.println("No element has been added, empty heap."); System.out.println("No element has been added, empty heap.");
} }
} }

View File

@ -14,11 +14,11 @@ package com.thealgorithms.datastructures.heaps;
*/ */
public class MinPriorityQueue { public class MinPriorityQueue {
private int[] heap; private final int[] heap;
private int capacity; private final int capacity;
private int size; private int size;
// calss the constructor and initializes the capacity // class the constructor and initializes the capacity
MinPriorityQueue(int c) { MinPriorityQueue(int c) {
this.capacity = c; this.capacity = c;
this.size = 0; this.size = 0;