-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeftistHeap.java
98 lines (79 loc) · 2.1 KB
/
LeftistHeap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package org.gra4j.trochilus;
public class LeftistHeap <E extends Comparable<? super E>> {
private Node<E> root;
public LeftistHeap () {
root = null;
}
public LeftistHeap (E element) {
root = new Node<>(element);
}
private static class Node<E> {
E element;
Node<E> left;
Node<E> right;
int npl;
Node(E element) {
this(element, null, null);
}
Node(E element, Node<E> lt, Node<E> rt) {
this.element = element;
this.left = lt;
this.right = rt;
this.npl = 0;
}
}
public void merge (LeftistHeap<E> rhs) {
if (this == rhs || rhs == null)
return;
root = merge(root, rhs.root);
}
public void insert (E element) {
if (root == null) {
root = new Node<>(element);
return;
}
root = merge(new Node<>(element), root);
}
public E findMin () {
return root == null ? null : root.element;
}
public E deleteMin () {
if (isEmpty())
throw new UnsupportedOperationException("heap is empty");
E min = findMin();
root = merge(root.left, root.right);
return min;
}
public boolean isEmpty () {
return root == null;
}
public void makeEmpty () {
root = null;
}
private Node<E> merge (Node<E> h1, Node<E> h2) {
if (h1 == null)
return h2;
if (h2 == null)
return h1;
if (h1.element.compareTo(h2.element) < 0)
return merge1(h1, h2);
else
return merge1(h2, h1);
}
private Node<E> merge1 (Node<E> h1, Node<E> h2) {
if (h1.left == null)
h1.left = h2;
else {
h1.right = merge(h1.right, h2);
if (h1.left.npl < h1.right.npl)
swapChildren(h1);
h1.npl = h1.right.npl + 1;
}
return h1;
}
private void swapChildren (Node<E> t) {
Node<E> left = t.left;
t.left = t.right;
t.right = left;
}
}