aboutsummaryrefslogtreecommitdiffstats
path: root/csci1913
diff options
context:
space:
mode:
authorRossTheRoss <msattr@gmail.com>2019-12-05 12:00:21 -0600
committerRossTheRoss <msattr@gmail.com>2019-12-05 12:00:21 -0600
commit13cf790b6f0a9a316e2661adf280f72ca80909e9 (patch)
tree7913df9e341123e638bc1bde2b68ddc0ab347241 /csci1913
parentA (diff)
downloadhomework-13cf790b6f0a9a316e2661adf280f72ca80909e9.tar
homework-13cf790b6f0a9a316e2661adf280f72ca80909e9.tar.gz
homework-13cf790b6f0a9a316e2661adf280f72ca80909e9.tar.bz2
homework-13cf790b6f0a9a316e2661adf280f72ca80909e9.tar.lz
homework-13cf790b6f0a9a316e2661adf280f72ca80909e9.tar.xz
homework-13cf790b6f0a9a316e2661adf280f72ca80909e9.tar.zst
homework-13cf790b6f0a9a316e2661adf280f72ca80909e9.zip
Implement enqueue
Diffstat (limited to 'csci1913')
-rw-r--r--csci1913/Java/lab12_strap012.java37
1 files changed, 29 insertions, 8 deletions
diff --git a/csci1913/Java/lab12_strap012.java b/csci1913/Java/lab12_strap012.java
index 433f58a..86da35c 100644
--- a/csci1913/Java/lab12_strap012.java
+++ b/csci1913/Java/lab12_strap012.java
@@ -1,7 +1,7 @@
class PriorityQueue<Base> {
private class Node {
private Base object;
- private int rank;
+ private int rank;
private Node left;
private Node right;
private Node(Base object, int rank) {
@@ -19,13 +19,17 @@ class PriorityQueue<Base> {
return root.right==root.left;
}
public Base dequeue() {
- if (isEmpty()) {
+ if (isEmpty())
throw new IllegalStateException();
- } else {
- Node Test = root.right;
-
- return Test.object;
+ Node top = root;
+ Node bottom = root.right; //First node is always to the right of the root.
+ while (bottom.right != null) { //Find lowest rank
+ top = bottom;
+ bottom = bottom.right;
}
+
+ return bottom.object;
+
}
// Unlike the BST’s discussed in the lectures, the nodes in
// each left subtree have ranks less than or equal to the
@@ -33,10 +37,27 @@ class PriorityQueue<Base> {
// ranks greater than the rank at the root.
// This allows two or more nodes to have the same rank.
public void enqueue(Base object, int rank) {
- if (rank < 0) {
+ if (rank < 0)
throw new IllegalArgumentException();
+ boolean LR = true;
+ Node top = root;
+ Node bottom = root.right;
+ while (bottom != null) {
+ top = bottom;
+ if (bottom.rank <= rank) {
+ bottom = bottom.left;
+ LR = false;
+ //LR = true;
+ } else {
+ bottom = bottom.right;
+ LR = true;
+ //LR = false;
+ }
+ }
+ if (LR) {
+ top.right = new Node(object, rank);
} else {
-
+ top.left = new Node(object, rank);
}
}