2
2
3
3
import java .util .ArrayList ;
4
4
5
- /*
6
- * This is a leftist heap that follows the same operations as a
7
- * binary min heap, but may be unbalanced at times and follows a
8
- * leftist property, in which the left side is more heavy on the
9
- * right based on the null-path length (npl) values.
5
+ /**
6
+ * This class implements a Leftist Heap, which is a type of priority queue
7
+ * that follows similar operations to a binary min-heap but allows for
8
+ * unbalanced structures based on the leftist property.
10
9
*
11
- * Source: https://iq.opengenus.org/leftist-heap/
10
+ * <p>
11
+ * A Leftist Heap maintains the leftist property, which ensures that the
12
+ * left subtree is heavier than the right subtree based on the
13
+ * null-path length (npl) values. This allows for efficient merging
14
+ * of heaps and supports operations like insertion, extraction of
15
+ * the minimum element, and in-order traversal.
16
+ * </p>
12
17
*
18
+ * <p>
19
+ * For more information on Leftist Heaps, visit:
20
+ * <a href="https://iq.opengenus.org/leftist-heap/">OpenGenus</a>
21
+ * </p>
13
22
*/
14
-
15
23
public class LeftistHeap {
24
+ // Node class representing each element in the Leftist Heap
16
25
private static final class Node {
17
26
private final int element ;
18
27
private int npl ;
19
28
private Node left ;
20
29
private Node right ;
21
30
22
- // Node constructor setting the data element and left/right pointers to null
31
+ // Node constructor that initializes the element and sets child pointers to null
23
32
private Node (int element ) {
24
33
this .element = element ;
25
34
left = null ;
@@ -30,31 +39,45 @@ private Node(int element) {
30
39
31
40
private Node root ;
32
41
33
- // Constructor
42
+ // Constructor initializing an empty Leftist Heap
34
43
public LeftistHeap () {
35
44
root = null ;
36
45
}
37
46
38
- // Checks if heap is empty
47
+ /**
48
+ * Checks if the heap is empty.
49
+ *
50
+ * @return true if the heap is empty; false otherwise
51
+ */
39
52
public boolean isEmpty () {
40
53
return root == null ;
41
54
}
42
55
43
- // Resets structure to initial state
56
+ /**
57
+ * Resets the heap to its initial state, effectively clearing all elements.
58
+ */
44
59
public void clear () {
45
- // We will put head is null
46
- root = null ;
60
+ root = null ; // Set root to null to clear the heap
47
61
}
48
62
49
- // Merge function that merges the contents of another leftist heap with the
50
- // current one
63
+ /**
64
+ * Merges the contents of another Leftist Heap into this one.
65
+ *
66
+ * @param h1 the LeftistHeap to be merged into this heap
67
+ */
51
68
public void merge (LeftistHeap h1 ) {
52
- // If the present function is rhs then we ignore the merge
69
+ // Merge the current heap with the provided heap and set the provided heap's root to null
53
70
root = merge (root , h1 .root );
54
71
h1 .root = null ;
55
72
}
56
73
57
- // Function merge with two Nodes a and b
74
+ /**
75
+ * Merges two nodes, maintaining the leftist property.
76
+ *
77
+ * @param a the first node
78
+ * @param b the second node
79
+ * @return the merged node maintaining the leftist property
80
+ */
58
81
public Node merge (Node a , Node b ) {
59
82
if (a == null ) {
60
83
return b ;
@@ -64,17 +87,17 @@ public Node merge(Node a, Node b) {
64
87
return a ;
65
88
}
66
89
67
- // Violates leftist property, so must do a swap
90
+ // Ensure that the leftist property is maintained
68
91
if (a .element > b .element ) {
69
92
Node temp = a ;
70
93
a = b ;
71
94
b = temp ;
72
95
}
73
96
74
- // Now we call the function merge to merge a and b
97
+ // Merge the right child of node a with node b
75
98
a .right = merge (a .right , b );
76
99
77
- // Violates leftist property so must swap here
100
+ // If left child is null, make right child the left child
78
101
if (a .left == null ) {
79
102
a .left = a .right ;
80
103
a .right = null ;
@@ -89,14 +112,21 @@ public Node merge(Node a, Node b) {
89
112
return a ;
90
113
}
91
114
92
- // Function insert. Uses the merge function to add the data
115
+ /**
116
+ * Inserts a new element into the Leftist Heap.
117
+ *
118
+ * @param a the element to be inserted
119
+ */
93
120
public void insert (int a ) {
94
121
root = merge (new Node (a ), root );
95
122
}
96
123
97
- // Returns and removes the minimum element in the heap
124
+ /**
125
+ * Extracts and removes the minimum element from the heap.
126
+ *
127
+ * @return the minimum element in the heap, or -1 if the heap is empty
128
+ */
98
129
public int extractMin () {
99
- // If is empty return -1
100
130
if (isEmpty ()) {
101
131
return -1 ;
102
132
}
@@ -106,14 +136,23 @@ public int extractMin() {
106
136
return min ;
107
137
}
108
138
109
- // Function returning a list of an in order traversal of the data structure
139
+ /**
140
+ * Returns a list of the elements in the heap in in-order traversal.
141
+ *
142
+ * @return an ArrayList containing the elements in in-order
143
+ */
110
144
public ArrayList <Integer > inOrder () {
111
145
ArrayList <Integer > lst = new ArrayList <>();
112
146
inOrderAux (root , lst );
113
147
return new ArrayList <>(lst );
114
148
}
115
149
116
- // Auxiliary function for in_order
150
+ /**
151
+ * Auxiliary function for in-order traversal
152
+ *
153
+ * @param n the current node
154
+ * @param lst the list to store the elements in in-order
155
+ */
117
156
private void inOrderAux (Node n , ArrayList <Integer > lst ) {
118
157
if (n == null ) {
119
158
return ;
0 commit comments