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
steven.liatti
poo2019numeric
Commits
4f543c18
Commit
4f543c18
authored
5 years ago
by
tanguy.dietrich
Browse files
Options
Downloads
Patches
Plain Diff
Ecriture de .push() pour le BinaryHeap
parent
433883ea
Branches
Branches containing commit
No related tags found
2 merge requests
!2
Tanguy
,
!1
David
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/ch/hepia/structure/BinaryHeap.java
+68
-2
68 additions, 2 deletions
src/main/java/ch/hepia/structure/BinaryHeap.java
src/test/java/ch/hepia/structure/AppTest.java
+15
-2
15 additions, 2 deletions
src/test/java/ch/hepia/structure/AppTest.java
with
83 additions
and
4 deletions
src/main/java/ch/hepia/structure/BinaryHeap.java
+
68
−
2
View file @
4f543c18
...
@@ -8,14 +8,80 @@ import java.util.function.Function;
...
@@ -8,14 +8,80 @@ import java.util.function.Function;
final
public
class
BinaryHeap
final
public
class
BinaryHeap
{
{
final
private
List
<
Integer
>
lstBinaryHeap
;
final
private
List
<
Integer
>
lstBinaryHeap
;
private
BinaryHeap
()
private
int
taille
;
public
BinaryHeap
()
{
{
lstBinaryHeap
=
new
ArrayList
<
Integer
>();
this
.
lstBinaryHeap
=
new
ArrayList
<
Integer
>();
this
.
taille
=
0
;
}
}
public
void
push
(
int
value
)
public
void
push
(
int
value
)
{
{
this
.
taille
++;
this
.
lstBinaryHeap
.
add
(
value
);
this
.
sort
();
}
private
void
sort
()
{
int
index
=
this
.
size
()-
1
;
while
(
index
!=
0
)
{
if
(
this
.
getParentNodeValue
(
index
)<
this
.
get
(
index
))
{
//les place doivent etre changer
this
.
switchPlace
(
this
.
getParentNodeIndex
(
index
),
index
);
//test la valeur d'a cote
}
else
{
//test si la branche existe
if
(
this
.
getSameLevelIndex
(
index
)<
this
.
size
())
{
//si elle existe
index
=
this
.
getSameLevelIndex
(
index
);
//passe a l'index d'a coter
if
(
this
.
getParentNodeValue
(
index
)<
this
.
get
(
index
))
{
//les place doivent etre changer
this
.
switchPlace
(
this
.
getParentNodeIndex
(
index
),
index
);
}
}
}
index
=
this
.
getParentNodeIndex
(
index
);
}
}
private
void
switchPlace
(
int
index1
,
int
index2
)
{
int
tmp
;
tmp
=
this
.
lstBinaryHeap
.
get
(
index1
);
this
.
set
(
index1
,
this
.
get
(
index2
));
this
.
set
(
index2
,
tmp
);
}
public
void
print
()
{
for
(
int
val:
lstBinaryHeap
)
{
System
.
out
.
println
(
val
);
}
}
public
int
size
()
{
return
this
.
taille
;
}
private
int
getSameLevelIndex
(
int
index
)
{
if
(
index
%
2
==
0
)
{
return
index
-
1
;
}
else
{
return
index
+
1
;
}
}
}
private
int
getRightNodeIndex
(
int
index
)
private
int
getRightNodeIndex
(
int
index
)
...
...
This diff is collapsed.
Click to expand it.
src/test/java/ch/hepia/structure/AppTest.java
+
15
−
2
View file @
4f543c18
...
@@ -11,7 +11,20 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
...
@@ -11,7 +11,20 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
class
BinaryHeapTest
class
BinaryHeapTest
{
{
@Test
@Test
void
BinaryHeapMainOperations
()
{
void
BinaryHeapPushTest
()
assertEquals
(
1
,
1
);
{
BinaryHeap
bHeap
=
new
BinaryHeap
();
bHeap
.
push
(
100
);
bHeap
.
push
(
19
);
bHeap
.
push
(
36
);
bHeap
.
push
(
17
);
bHeap
.
push
(
3
);
bHeap
.
push
(
25
);
bHeap
.
push
(
1
);
bHeap
.
push
(
2
);
bHeap
.
push
(
7
);
bHeap
.
push
(
40
);
bHeap
.
print
();
//assertEquals();
}
}
}
}
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