FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaDataStructures/Heap/LeftistModule.java at master · benaich/JavaDataStructures · GitHub
benaich
/
JavaDataStructures
Public
Notifications
You must be signed in to change notification settings
Fork
7
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaDataStructures
/
Heap
/
LeftistModule.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
221 lines (171 loc) · 5.28 KB
Breadcrumbs
JavaDataStructures
/
Heap
/
LeftistModule.java
Copy path
File metadata and controls
221 lines (171 loc) · 5.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package
Heap
;
public
class
LeftistModule
{
public
static
interface
Leftist
<
T
extends
Comparable
<?
super
T
>> {
Leftist
left
();
Leftist
right
();
T
data
();
int
rank
();
boolean
isEmpty
();
int
size
();
Leftist
<
T
>
merge
(
Leftist
<
T
>
h
);
T
min
();
Leftist
<
T
>
delete
();
default
Leftist
<
T
>
insert
(
T
t
) {
return
merge
(
heap
(
t
));
}
default
Leftist
<
T
>
insert
(
T
...
array
) {
Leftist
<
T
>
root
=
emptyLeftist
();
for
(
T
item
:
array
) {
root
=
root
.
merge
(
heap
(
item
));
}
return
root
;
}
}
public
static
class
NonEmptyLeftist
<
T
extends
Comparable
<?
super
T
>>
implements
Leftist
<
T
> {
public
Leftist
left
;
public
Leftist
right
;
public
T
data
;
public
int
rank
;
public
NonEmptyLeftist
(
T
x
,
Leftist
a
,
Leftist
b
) {
this
(
0
,
x
,
a
,
b
);
}
public
NonEmptyLeftist
(
int
r
,
T
x
,
Leftist
a
,
Leftist
b
) {
rank
=
r
;
data
=
x
;
left
=
a
;
right
=
b
;
}
@
Override
public
Leftist
left
() {
return
left
;
}
@
Override
public
Leftist
right
() {
return
right
;
}
@
Override
public
T
data
() {
return
data
;
}
@
Override
public
int
rank
() {
return
rank
;
}
@
Override
public
boolean
isEmpty
() {
return
false
;
}
@
Override
public
int
size
() {
return
1
+
left
.
size
() +
right
.
size
();
}
@
Override
public
Leftist
<
T
>
merge
(
Leftist
<
T
>
h
) {
if
(
h
.
isEmpty
()) {
return
this
;
}
if
(
data
.
compareTo
(
h
.
data
()) <=
0
) {
return
heap
(
data
,
left
,
right
.
merge
(
h
));
}
return
heap
(
h
.
data
(),
h
.
left
(),
h
.
right
().
merge
(
this
));
}
public
T
min
() {
return
data
;
}
public
Leftist
<
T
>
delete
() {
return
left
.
merge
(
right
);
}
}
public
static
class
Empty
<
T
extends
Comparable
<?
super
T
>>
implements
Leftist
<
T
> {
static
protected
final
Empty
empty
=
new
Empty
();
@
Override
public
Leftist
left
() {
throw
new
UnsupportedOperationException
();
}
@
Override
public
Leftist
right
() {
throw
new
UnsupportedOperationException
();
}
@
Override
public
T
data
() {
throw
new
UnsupportedOperationException
();
}
@
Override
public
int
rank
() {
return
0
;
}
@
Override
public
String
toString
() {
return
""
;
}
@
Override
public
boolean
isEmpty
() {
return
true
;
}
@
Override
public
int
size
() {
return
0
;
}
@
Override
public
Leftist
<
T
>
merge
(
Leftist
<
T
>
h
) {
return
h
;
}
public
T
min
() {
throw
new
UnsupportedOperationException
();
}
public
Leftist
<
T
>
delete
() {
throw
new
UnsupportedOperationException
();
}
};
/* retourn EMPTY instnace (singleton pattern) */
public
static
<
T
extends
Comparable
<?
super
T
>>
Leftist
<
T
>
emptyLeftist
() {
return
(
Leftist
)
Empty
.
empty
;
}
/* build a leftist heap v1 */
public
static
<
T
extends
Comparable
<?
super
T
>>
Leftist
<
T
>
heap
(
T
x
,
Leftist
<
T
>
a
,
Leftist
<
T
>
b
) {
if
(
a
.
rank
() >
b
.
rank
()) {
return
new
NonEmptyLeftist
<>(
b
.
rank
() +
1
,
x
,
a
,
b
);
}
return
new
NonEmptyLeftist
<>(
a
.
rank
() +
1
,
x
,
b
,
a
);
}
/* build a leftist heap v2 */
public
static
<
T
extends
Comparable
<?
super
T
>>
Leftist
<
T
>
heap
(
T
x
) {
return
heap
(
x
,
emptyLeftist
(),
emptyLeftist
());
}
/* used by HuffmanFunctionalProgramming package */
public
static
<
T
extends
Comparable
<?
super
T
>>
Leftist
<
T
>
insert
(
Leftist
<
T
>
p
,
T
val
) {
return
p
.
insert
(
val
);
}
/* print heap element */
public
static
<
T
extends
Comparable
<?
super
T
>>
void
printNice
(
Leftist
<
T
>
root
,
int
level
) {
if
(
root
.
isEmpty
()) {
return
;
}
printNice
(
root
.
right
(),
level
+
1
);
if
(
level
!=
0
) {
for
(
int
i
=
0
;
i
<
level
-
1
;
i
++) {
System
.
out
.
print
(
"| "
);
}
System
.
out
.
println
(
"|---"
+
root
.
data
());
}
else
{
System
.
out
.
println
(
root
.
data
());
}
printNice
(
root
.
left
(),
level
+
1
);
}
public
static
void
main
(
String
[]
args
) {
Leftist
<
Integer
>
p1
=
emptyLeftist
();
Leftist
<
Integer
>
p2
=
emptyLeftist
();
p1
=
p1
.
insert
(
2
,
3
,
4
,
1
,
2
);
System
.
out
.
println
(
"----------- Leftits Heap 1 -----------"
);
printNice
(
p1
,
1
);
System
.
out
.
println
(
"----------- delete -----------"
);
p1
=
p1
.
delete
();
printNice
(
p1
,
1
);
p2
=
p2
.
insert
(
5
,
9
,
3
,
1
,
2
,
15
,
22
);
System
.
out
.
println
(
"----------- Leftits Heap 1 -----------"
);
printNice
(
p2
,
1
);
Leftist
<
Integer
>
p3
=
p1
.
merge
(
p2
);
System
.
out
.
println
(
"----------- merge Heap 1 & Heap 2 -----------"
);
printNice
(
p3
,
1
);
}
}
Back
|
FazBrowse Home
|
New Git URL