Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
poo2019numeric
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
francois.hofer
poo2019numeric
Commits
abce7fba
Commit
abce7fba
authored
5 years ago
by
fefe
Browse files
Options
Downloads
Plain Diff
Merge branch 'master' of
https://gitedu.hesge.ch/francois.hofer/poo2019numeric
parents
a3487d51
c24274b2
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/ch/hepia/structure/BinaryHeap.java
+89
-52
89 additions, 52 deletions
src/main/java/ch/hepia/structure/BinaryHeap.java
with
89 additions
and
52 deletions
src/main/java/ch/hepia/structure/BinaryHeap.java
+
89
−
52
View file @
abce7fba
import
java.lang.Math
;
import
java.util.Random
;
import
java.util.Random
;
import
java.util.List
;
public
class
BinaryHeap
{
private
int
[]
heap
;
...
...
@@ -15,6 +16,18 @@ public class BinaryHeap{
public
int
parent
(
int
i
){
return
(
i
-
1
)/
2
;
}
public
int
left
(
int
i
){
return
(
2
*
i
+
1
);
}
public
int
right
(
int
i
){
return
(
2
*
i
+
2
);
}
private
void
swap
(
int
index1
,
int
index2
)
{
int
element
=
heap
[
index1
];
heap
[
index1
]
=
heap
[
index2
];
heap
[
index2
]
=
element
;
}
public
void
push
(
int
val
){
if
(
nbOfElem
==
maxCap
)
{
...
...
@@ -22,77 +35,101 @@ public class BinaryHeap{
return
;
}
// First insert the new key at the end
nbOfElem
++;
int
i
=
nbOfElem
-
1
;
heap
[
i
]
=
val
;
// Fix the min heap property if it is violated
while
(
i
!=
0
&&
heap
[
parent
(
i
)]
>
heap
[
i
])
{
int
temp
=
heap
[
i
];
heap
[
i
]
=
heap
[
parent
(
i
)];
heap
[
parent
(
i
)]
=
temp
;
i
=
parent
(
i
);
}
}
heap
[
nbOfElem
]
=
val
;
nbOfElem
++;
int
index
=
nbOfElem
-
1
;
public
void
populate
(
int
nbValues
){
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
<
nbValues
;
i
++)
{
int
randomVal
=
random
.
nextInt
(
100
);
//System.out.println(randomVal);
this
.
push
(
randomVal
);
while
(
index
>
0
&&
heap
[
parent
(
index
)]
<
heap
[
index
])
{
swap
(
parent
(
index
),
index
);
index
=
parent
(
index
);
}
//return heap;
}
/*private static void Tamiser(int[] arbre, int noeud, int n){
int k = noeud;
int j = 2 * k;
while (j <= n)
{
if ((j < n) && (arbre[j] < arbre[j + 1]))
j++;
if (arbre[k] < arbre[j])
{
int swap = arbre[k];
arbre[k] = arbre[j];
arbre[j] = swap;
k = j;
j = 2 * k;
public
int
pop
(){
int
element
=
heap
[
0
];
heap
[
0
]
=
heap
[
nbOfElem
-
1
];
nbOfElem
--;
int
index
=
0
;
while
(
left
(
index
)
<
nbOfElem
)
{
int
smallestChildIndex
=
left
(
index
);
if
(
right
(
index
)
<
nbOfElem
&&
heap
[
right
(
index
)]
<
heap
[
left
(
index
)])
{
smallestChildIndex
=
right
(
index
);
}
else
if
(
heap
[
index
]
<
heap
[
smallestChildIndex
])
{
swap
(
index
,
smallestChildIndex
);
}
else
{
break
;
}
index
=
smallestChildIndex
;
}
return
element
;
}
public
void heapSort
(){
for (int i = this.heap.length >> 1; i >= 0; i--)
Tamiser(this.heap, i, this.heap.length - 1);
public
int
peek
(){
return
heap
[
0
];
}
for (int i = this.heap.length - 1; i >= 1; i--)
{
int swap = this.heap[i];
this.heap[i] = this.heap[0];
this.heap[0] = swap;
Tamiser(this.heap, 0, i - 1);
public
void
addAll
(
List
<
Integer
>
lst
){
for
(
int
x
:
lst
){
this
.
push
(
x
);
}
}*/
}
public
boolean
isEmpty
(){
return
(
nbOfElem
==
0
);
}
public
boolean
exists
(
int
k
){
for
(
int
x
:
heap
){
if
(
x
==
k
){
return
true
;
}
}
return
false
;
}
public
int
size
(){
return
nbOfElem
;
}
public
int
depth
(){
int
prof
=
1
;
// on compte la racine
int
size
=
nbOfElem
;
do
{
size
=
size
/
2
;
prof
=
prof
+
1
;
}
while
(
size
>
1
);
return
prof
;
}
public
void
populate
(
int
nbValues
){
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
<
nbValues
;
i
++)
{
int
randomVal
=
random
.
nextInt
(
100
);
this
.
push
(
randomVal
);
}
}
public
void
printHeap
(){
for
(
int
x
:
this
.
heap
)
{
System
.
out
.
print
ln
(
x
);
System
.
out
.
print
(
x
+
" "
);
}
}
public
static
void
main
(
String
[]
args
){
BinaryHeap
test
=
new
BinaryHeap
(
1
0
);
BinaryHeap
test
=
new
BinaryHeap
(
1
5
);
test
.
populate
(
19
);
test
.
printHeap
();
int
depth
=
test
.
depth
();
System
.
out
.
println
(
"profondeur = "
+
depth
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment