Skip to content

Commit 8c61df2

Browse files
author
codelogicws
committed
PriorityQueue
still working it out.
1 parent b03470d commit 8c61df2

File tree

6 files changed

+68
-13
lines changed

6 files changed

+68
-13
lines changed
940 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ws.codelogic.algorithms.debug;
2+
3+
public class Debug {
4+
5+
public static void printArray(int[] array){
6+
for(Comparable c : array){
7+
System.out.print(c + ", ");
8+
}
9+
}
10+
}

src/ws/codelogic/algorithms/priorityqueue/BinaryTree.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,61 @@
11
package ws.codelogic.algorithms.priorityqueue;
22

3-
public class BinaryTree <T>{
3+
public class BinaryTree{
44

5-
private T[] elements;
6-
private int currentElement;
5+
private Comparable[] elements;
6+
private int indexOfLastElement;
77

8-
public BinaryTree(T[] emptyArray){
8+
public BinaryTree(Comparable[] emptyArray){
99
elements = emptyArray;
10-
currentElement = -1;
10+
indexOfLastElement = -1;
1111
}
1212

13-
public void add(T element){
14-
elements[++currentElement] = element;
13+
public void add(Comparable element){
14+
elements[++indexOfLastElement] = element;
15+
swim(indexOfLastElement);
1516
}
1617

17-
public T remove(){
18-
return elements[currentElement--];
18+
private void swim(int index) {
19+
if(parentIsLess(index)){
20+
//DEBUG
21+
System.out.println("debug-BinaryTree: need to swap " + index + " and " + parrentIndex(index));
22+
//DEBUG
23+
// swapWithParent(index);
24+
// swim(indexOfParent(index));
25+
}
26+
}
27+
28+
private boolean parentIsLess(int index){
29+
if(hasParent(index)){
30+
int parrentIndex = parrentIndex(index);
31+
if(less(parrentIndex, index)){
32+
return true;
33+
}
34+
}
35+
return false;
36+
}
37+
38+
private int parrentIndex(int index) {
39+
int treeIndex = index + 1;
40+
int parrentTreeIndex = treeIndex/2;
41+
return parrentTreeIndex -1;
42+
}
43+
44+
45+
private boolean less(int index1, int index2) {
46+
return (elements[index1].compareTo(elements[index2]) < 0);
47+
}
48+
49+
private boolean hasParent(int index) {
50+
return index > 0;
51+
}
52+
53+
private int indexOfParent(int currentIndex){
54+
return currentIndex/2;
55+
}
56+
57+
public Comparable remove(){
58+
return elements[indexOfLastElement--];
1959
}
2060

2161

src/ws/codelogic/algorithms/priorityqueue/PriorityQueueTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
public class PriorityQueueTest {
88

9-
private BinaryTree<Integer> queue;
9+
private BinaryTree queue;
1010

1111
@Before
1212
public void setUp() {
1313
Integer[] array = new Integer[5];
14-
queue = new BinaryTree<Integer>(array);
14+
queue = new BinaryTree(array);
1515
}
1616

1717
@Test
@@ -21,18 +21,23 @@ public void adding1ObjectYeildsThatObject() {
2121
}
2222

2323
private void checkNumber(Integer numberToTest){
24-
Integer numberFromQueue = queue.remove();
24+
Integer numberFromQueue = (Integer)queue.remove();
2525
assertEquals(numberFromQueue, numberToTest);
2626
}
2727

2828
@Test
2929
public void adding3ObjectYeildsThoseObjectsInOrder() {
3030
queue.add(5);
31-
queue.add(1);
31+
queue.add(7);
3232
queue.add(9);
3333
checkNumber(9);
3434
checkNumber(5);
3535
checkNumber(1);
3636
}
3737

38+
39+
40+
//array is full
41+
//array is empty
42+
3843
}

0 commit comments

Comments
 (0)