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! 28-17 Ø rear Golf #1

4 Implementing a Queue Using Linked List –implementing a linked list cursor implementation front = 5 freelist = 8 012345678012345678 2 7 1 4 3 6 0 Golf #1 212 rules! 28-17 Ø Ø Ø Ø Ø Ø 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 01 2 3 4 7 6 5 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 = 2 01 2 3 4 7 6 5 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 = 2 01 2 3 4 7 6 5 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 = 2 01 2 3 4 7 6 5 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 = 2 01 2 3 4 7 6 5 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 0 1 2 3 4

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