Skip to content

Commit b03470d

Browse files
author
codelogicws
committed
Binary Tree Priority Queue
starting to implement
1 parent 13e7301 commit b03470d

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed
Binary file not shown.
Binary file not shown.
63 Bytes
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ws.codelogic.algorithms.priorityqueue;
2+
3+
public class BinaryTree <T>{
4+
5+
private T[] elements;
6+
private int currentElement;
7+
8+
public BinaryTree(T[] emptyArray){
9+
elements = emptyArray;
10+
currentElement = -1;
11+
}
12+
13+
public void add(T element){
14+
elements[++currentElement] = element;
15+
}
16+
17+
public T remove(){
18+
return elements[currentElement--];
19+
}
20+
21+
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ws.codelogic.algorithms.priorityqueue;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import static org.junit.Assert.*;
6+
7+
public class PriorityQueueTest {
8+
9+
private BinaryTree<Integer> queue;
10+
11+
@Before
12+
public void setUp() {
13+
Integer[] array = new Integer[5];
14+
queue = new BinaryTree<Integer>(array);
15+
}
16+
17+
@Test
18+
public void adding1ObjectYeildsThatObject() {
19+
queue.add(9);
20+
checkNumber(9);
21+
}
22+
23+
private void checkNumber(Integer numberToTest){
24+
Integer numberFromQueue = queue.remove();
25+
assertEquals(numberFromQueue, numberToTest);
26+
}
27+
28+
@Test
29+
public void adding3ObjectYeildsThoseObjectsInOrder() {
30+
queue.add(5);
31+
queue.add(1);
32+
queue.add(9);
33+
checkNumber(9);
34+
checkNumber(5);
35+
checkNumber(1);
36+
}
37+
38+
}

src/ws/codelogic/test/TestSuite.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ws.codelogic.algorithms.Stack.ArrayStackTest;
66
import ws.codelogic.algorithms.Stack.LinkStackTest;
77
import ws.codelogic.algorithms.arithmetic.evaluation.DijkstrasTwoStackTest;
8+
import ws.codelogic.algorithms.priorityqueue.PriorityQueueTest;
89
import ws.codelogic.algorithms.search.SearchTest;
910
import ws.codelogic.algorithms.selection.QuickSelectTest;
1011
import ws.codelogic.algorithms.shuffle.ShufflerTest;
@@ -14,6 +15,7 @@
1415

1516
@RunWith(Suite.class)
1617
@Suite.SuiteClasses({
18+
PriorityQueueTest.class,
1719
Dijkstras3WayPartitionTest.class,
1820
QuickSelectTest.class,
1921
QuickSortTest.class,

0 commit comments

Comments
 (0)