From fb957931af7a4ba0cb6301279ce974d139caeced Mon Sep 17 00:00:00 2001 From: Adrian Paras Date: Sun, 4 Dec 2022 13:39:47 -0500 Subject: [PATCH 1/4] Adding LeftistHeap --- .../datastructures/heaps/LeftistHeap.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java new file mode 100644 index 000000000000..3502577d8c2e --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java @@ -0,0 +1,107 @@ +package com.thealgorithms.datastructures.heaps; + +public class LeftistHeap { + 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; + } + } + + private Node root; + + // Constructor + public LeftistHeap() { + 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; + } + + // Merge function that merges the contents of another leftist heap with the + // current one + public void merge(LeftistHeap rhs) { + // If the present function is rhs then we ignore the merge + if (this == rhs) + return; + + root = merge(root, rhs.root); + rhs.root = null; + } + + // 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; + + // 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); + + // 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); + } + + // 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; + } + + // Function printing an in order traversal of the data structure + public void in_order() { + in_order_aux(root); + } + + // Auxiliary function for in_order + private void in_order_aux(Node n) { + if (n == null) + return; + in_order_aux(n.left); + System.out.print(n.element + " "); + in_order_aux(n.right); + } +} \ No newline at end of file From 90738584180613ab7120947f5c6673d3043951c5 Mon Sep 17 00:00:00 2001 From: Adrian Paras Date: Sun, 4 Dec 2022 14:00:08 -0500 Subject: [PATCH 2/4] Adding test --- .../datastructures/heaps/LeftistHeap.java | 18 +++++++----- .../datastructures/heaps/LeftistHeapTest.java | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java index 3502577d8c2e..352929943dac 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java @@ -1,5 +1,7 @@ package com.thealgorithms.datastructures.heaps; +import java.util.ArrayList; + public class LeftistHeap { private class Node { private int element, npl; @@ -91,17 +93,19 @@ public int extract_min() { return min; } - // Function printing an in order traversal of the data structure - public void in_order() { - in_order_aux(root); + // Function returning a list of an in order traversal of the data structure + public ArrayList in_order() { + ArrayList lst = new ArrayList<>(); + in_order_aux(root, lst); + return new ArrayList<>(lst); } // Auxiliary function for in_order - private void in_order_aux(Node n) { + private void in_order_aux(Node n, ArrayList lst) { if (n == null) return; - in_order_aux(n.left); - System.out.print(n.element + " "); - in_order_aux(n.right); + in_order_aux(n.left, lst); + lst.add(n.element); + in_order_aux(n.right, lst); } } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java b/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java new file mode 100644 index 000000000000..b0cb0d19674b --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java @@ -0,0 +1,28 @@ +package com.thealgorithms.datastructures.heaps; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class LeftistHeapTest { + + @Test + void testLeftistHeap() { + LeftistHeap heap = new LeftistHeap(); + Assertions.assertTrue(heap.isEmpty()); + heap.insert(6); + Assertions.assertTrue(!heap.isEmpty()); + heap.insert(2); + heap.insert(3); + heap.insert(1); + heap.in_order(); + Assertions.assertTrue(heap.in_order().toString().equals("[6, 2, 3, 1]")); + Assertions.assertTrue(heap.extract_min() == 1); + Assertions.assertTrue(heap.in_order().toString().equals("[6, 2, 3]")); + heap.insert(8); + heap.insert(12); + heap.insert(4); + Assertions.assertTrue(heap.in_order().toString().equals("[8, 3, 12, 2, 6, 4]")); + heap.clear(); + Assertions.assertTrue(heap.isEmpty()); + } +} \ No newline at end of file From f9ae3c3c3d16db322f4afc7f706fe75c30d2b1fe Mon Sep 17 00:00:00 2001 From: Adrian Paras Date: Sun, 4 Dec 2022 14:06:47 -0500 Subject: [PATCH 3/4] Update LeftistHeap.java --- .../thealgorithms/datastructures/heaps/LeftistHeap.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java index 352929943dac..9e16bccb1b14 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java @@ -35,13 +35,10 @@ public void clear() { // Merge function that merges the contents of another leftist heap with the // current one - public void merge(LeftistHeap rhs) { + public void merge(LeftistHeap h1) { // If the present function is rhs then we ignore the merge - if (this == rhs) - return; - - root = merge(root, rhs.root); - rhs.root = null; + root = merge(root, h1.root); + h1.root = null; } // Function merge with two Nodes a and b From 6351d293002556c37b201c1e4904f07f14c61a14 Mon Sep 17 00:00:00 2001 From: Adrian Paras Date: Sun, 4 Dec 2022 14:16:15 -0500 Subject: [PATCH 4/4] Updating LeftistHeap.java --- .../datastructures/heaps/LeftistHeap.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java index 9e16bccb1b14..66861ac1d111 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java @@ -2,6 +2,16 @@ 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;