Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue ADT Operations Enqueue, Dequeue, Peek, isEmpty Implementation: Circular Array (Weiss), Linked List, Circular Array (text) Application: Level-Order.

Similar presentations


Presentation on theme: "Queue ADT Operations Enqueue, Dequeue, Peek, isEmpty Implementation: Circular Array (Weiss), Linked List, Circular Array (text) Application: Level-Order."— Presentation transcript:

1 Queue ADT Operations Enqueue, Dequeue, Peek, isEmpty Implementation: Circular Array (Weiss), Linked List, Circular Array (text) Application: Level-Order Tree Traversal

2 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

3 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

4 Implementing a Queue Using Linked List –implementing a linked list cursor implementation front = 5 freelist = Golf #1 212 rules! Ø Ø Ø Ø Ø Ø rear = 1

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 Level-Order Traversal B D A E IJ K MNL F H C G

26 B D A E IJ K MNL F H C G Level

27 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 =

28 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 =

29 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

30 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

31 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.


Download ppt "Queue ADT Operations Enqueue, Dequeue, Peek, isEmpty Implementation: Circular Array (Weiss), Linked List, Circular Array (text) Application: Level-Order."

Similar presentations


Ads by Google