1+ package com .thealgorithms .datastructures .heaps ;
2+
3+ import java .util .ArrayList ;
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.
10+ *
11+ * Source: https://iq.opengenus.org/leftist-heap/
12+ *
13+ */
14+
15+ public class LeftistHeap {
16+ private class Node {
17+ private int element , npl ;
18+ private Node left , right ;
19+
20+ // Node constructor setting the data element and left/right pointers to null
21+ private Node (int element ) {
22+ this .element = element ;
23+ left = right = null ;
24+ npl = 0 ;
25+ }
26+ }
27+
28+ private Node root ;
29+
30+ // Constructor
31+ public LeftistHeap () {
32+ root = null ;
33+ }
34+
35+ // Checks if heap is empty
36+ public boolean isEmpty () {
37+ return root == null ;
38+ }
39+
40+ // Resets structure to initial state
41+ public void clear () {
42+ // We will put head is null
43+ root = null ;
44+ }
45+
46+ // Merge function that merges the contents of another leftist heap with the
47+ // current one
48+ public void merge (LeftistHeap h1 ) {
49+ // If the present function is rhs then we ignore the merge
50+ root = merge (root , h1 .root );
51+ h1 .root = null ;
52+ }
53+
54+ // Function merge with two Nodes a and b
55+ public Node merge (Node a , Node b ) {
56+ if (a == null )
57+ return b ;
58+
59+ if (b == null )
60+ return a ;
61+
62+ // Violates leftist property, so must do a swap
63+ if (a .element > b .element ) {
64+ Node temp = a ;
65+ a = b ;
66+ b = temp ;
67+ }
68+
69+ // Now we call the function merge to merge a and b
70+ a .right = merge (a .right , b );
71+
72+ // Violates leftist property so must swap here
73+ if (a .left == null ) {
74+ a .left = a .right ;
75+ a .right = null ;
76+ } else {
77+ if (a .left .npl < a .right .npl ) {
78+ Node temp = a .left ;
79+ a .left = a .right ;
80+ a .right = temp ;
81+ }
82+ a .npl = a .right .npl + 1 ;
83+ }
84+ return a ;
85+ }
86+
87+ // Function insert. Uses the merge function to add the data
88+ public void insert (int a ) {
89+ root = merge (new Node (a ), root );
90+ }
91+
92+ // Returns and removes the minimum element in the heap
93+ public int extract_min () {
94+ // If is empty return -1
95+ if (isEmpty ())
96+ return -1 ;
97+
98+ int min = root .element ;
99+ root = merge (root .left , root .right );
100+ return min ;
101+ }
102+
103+ // Function returning a list of an in order traversal of the data structure
104+ public ArrayList <Integer > in_order () {
105+ ArrayList <Integer > lst = new ArrayList <>();
106+ in_order_aux (root , lst );
107+ return new ArrayList <>(lst );
108+ }
109+
110+ // Auxiliary function for in_order
111+ private void in_order_aux (Node n , ArrayList <Integer > lst ) {
112+ if (n == null )
113+ return ;
114+ in_order_aux (n .left , lst );
115+ lst .add (n .element );
116+ in_order_aux (n .right , lst );
117+ }
118+ }
0 commit comments