Queue ADT Operations Enqueue, Dequeue, Peek, isEmpty Implementation: Circular Array (Weiss), Linked List, Circular Array (text) Application: Level-Order Tree Traversal
Implementing a Queue Using Vector /Array –requires estimate of maximum queue length –may grow dynamically –Ø = empty slots –Can contain varied data/objects (not necessarily homogeneous) 28-17Golf #1 ØØØ 212 rules! rearfront
Implementing a Queue Using Linked List –flexible, adjusts to problem size –implementing a linked list nodes and references/links/pointers front 212 rules! Ø rear Golf #1
Implementing a Queue Using Linked List –implementing a linked list cursor implementation front = 5 freelist = Golf #1 212 rules! Ø Ø Ø Ø Ø Ø rear = 1
Implementing a Queue Vector/Array Linked List –enqueueO(1) * O(1) –dequeueO(1)O(1) –peekO(1)O(1) –isEmptyO(1)O(1) *assuming no need for Vector/Array expansion
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 rear = 0 Q
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 rear = 0 1 enqueue (“A”) A Q
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 rear = 1 2 enqueue (“B”) A B Q
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 rear = 2 3 enqueue (“C”) A B C Q
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 0 1 rear = 3 dequeue() // returns “A” A B C Q
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 1 2 rear = 3 dequeue() // returns “B” A B C Q
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue front = 2 3 rear = 3 dequeue() // returns “C” A B C Q
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { } // isEmpty front = 3 rear = 3 // Is the queue empty? // Complete isEmpty() method. A B C Q
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 3 4 enqueue(“D”); A B C Q D
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 4 5 enqueue(“E”); A B C Q DE
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 5 6 enqueue(“F”); A B C Q F DE
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 6 7 enqueue(“G”); A B C Q F DE G
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 7 0 enqueue(“H”); A B C Q F DE G H
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 0 1 enqueue(“I”); I B C Q F DE G H
enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty front = 3 rear = 1 2 enqueue(“J”); I J C Q F DE G H
front = 3 rear = enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty boolean isFull() { } // isFull // Is the queue full? // Complete isFull() method. I J C Q F DE G H
front = 3 4 rear = enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty boolean isFull() { return front==(rear+1)%Q.length; } // isFull dequeue(); // returns “D” I J C Q F DE G H
front = 4 5 rear = enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty boolean isFull() { return front==(rear+1)%Q.length; } // isFull dequeue(); // returns “E” I J C Q F DE G H
front = 5 rear = enqueue (Object item) { rear = (rear+1)%Q.length; if (front == rear) throw QueueFullException; Q[rear] = item; } // enqueue Object dequeue() { if (front == rear) throw QueueEmptyException; front = (front+1)%Q.length; return Q[front]; } // dequeue boolean isEmpty() { return (front == rear); } // isEmpty boolean isFull() { return front==(rear+1)%Q.length; } // isFull // How many elements does the // queue have? Generalize. I J C Q F DE G H
Level-Order Traversal B D A E IJ K MNL F H C G
B D A E IJ K MNL F H C G Level
B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q = Ø output =
B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q A output =
B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q B C output = A
B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q C D E output = A B
B D A E IJ K MNL F H C G levelOrder (Node root) { Queue q; q.enqueue (root); while (!q.isEmpty()) { Node n = (Node) q.dequeue(); visit(n); if (n.left() != null) q.enqueue(n.left()); if (n.right() != null) q.enqueue(n.right()); } // while } // levelOrder q D E F G output = A B C etc.