Queues ADT description Implementations Cinda Heeren / Geoffrey Tien 1
Queues Assume that we want to store data for a print queue for a student printer Student ID Time File name The printer is to be assigned to file in the order in which they are received A fair algorithm Example: Print Queue Cinda Heeren / Geoffrey Tien 2
Classes for Print Queues To maintain the print queue we would require at least two classes Request class Collection class to store requests The collection class should support the desired behaviour FIFO (First In First Out) The ADT is a queue Cinda Heeren / Geoffrey Tien 3
Queues In a queue items are inserted at the back (end/tail) and removed from the front (head) Queues are FIFO (First In First Out) data structures fair data structures Applications include: Server requests Instant messaging servers queue up incoming messages Database requests Print queues Operating systems often use queues to schedule CPU jobs The waiting list for this course! (it s presumably fair) Various algorithm implementations Cinda Heeren / Geoffrey Tien 4
Queue Operations A queue should implement at least the first two of these operations: enqueue insert item at the back of the queue dequeue remove an item from the front peek return the item at the front of the queue without removing it isempty check if the queue does not contain any items Like stacks, it is assumed that these operations will be implemented efficiently That is, in constant time Cinda Heeren / Geoffrey Tien 5
Queue Implementation Consider using an array as the underlying structure for a queue, we could Make the back of the queue the current size of the array, much like the stack implementation Initially make the front of the queue index 0 Inserting an item is easy Using an Array What to do when items are removed? Either move all remaining items down slow Or increment the front index wastes space Cinda Heeren / Geoffrey Tien 6
Circular Arrays Trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end of the array wraps around to the start of the array 7 0 6 1 0 1 2 3 4 5 6 7 5 2 4 3 Cinda Heeren / Geoffrey Tien 7
The modulo Operator The mod operator (%) calculates remainders: 1%5 = 1, 2%5 = 2, 5%5 = 0, 8%5 = 3 The mod operator can be used to calculate the front and back positions in a circular array Thereby avoiding comparisons to the array size The back of the queue is: (front + num) % arrlength where num is the number of items in the queue After removing an item, the front of the queue is: (front + 1) % arrlength Member attributes: int front; int arrlength; int* arr; int num; Cinda Heeren / Geoffrey Tien 8
Array Queue Example Queue q; q.enqueue(6); 0 01 front num 6 0 1 2 3 4 5 Insert item at (front + num) % queue.length, then increment num Cinda Heeren / Geoffrey Tien 9
Array Queue Example Queue q; q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue(); q.dequeue(); 01 2 front 6 num Insert item at (front + num) % queue.length, then increment num 54 3 4 7 3 8 0 1 2 3 4 5 Remove item at front, then decrement num and make front = (front + 1) % queue.length Cinda Heeren / Geoffrey Tien 10
Array Queue Example Queue q; q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); q.dequeue(); q.dequeue(); q.enqueue(9); q.enqueue(5); 2 front 5 num Insert item at (front + num) % queue.length, then increment num 34 5 7 3 8 0 1 2 3 4 5 Remove item at front, then decrement num and make front = (front + 1) % queue.length 9 Need to check that the back of the queue does not overtake the front Cinda Heeren / Geoffrey Tien 11
Array queue resizing Suppose we have an array-based queue and we have performed some enqueue and dequeue operations Then we perform more enqueues to fill the array How should we resize the array to allow for more enqueue operations? 12 5 76 33 2 41 front???????????? Cinda Heeren / Geoffrey Tien 12
Queue Implementation Using a Linked List Removing items from the front of the queue is straightforward Items should be inserted at the back of the queue in constant time So we must avoid traversing through the list Use a second node pointer to keep track of the node at the back of the queue Requires a little extra administration Member attributes: Node* front; Node* back; int num; Cinda Heeren / Geoffrey Tien 13
List Queue Example Queue q; q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.dequeue(); front back 6 4 7 3 Cinda Heeren / Geoffrey Tien 14
Exercise Given a Queue class implemented with a (dynamic) circular array of 8 int elements and an integer size member, the following block of code is executed: Queue qa; qa.enqueue(34); qa.enqueue(29); qa.enqueue(11); qa.dequeue(); qa.enqueue(47); qa.dequeue(); Queue qb = qa; qa.enqueue(6); qb.enqueue(52); qb.dequeue(); qb.dequeue(); qa.dequeue(); Shallow Copy, Deep Copy What will be output as the contents of each queue qa and qb if: qb is created as a shallow copy of qa? qb is created as a deep copy of qb? Cinda Heeren / Geoffrey Tien 15
Trees Introduction & terminology Cinda Heeren / Geoffrey Tien 16
Review: Linked Lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed as chains of such nodes List Cinda Heeren / Geoffrey Tien 17
Trees Root Trees are also constructed from nodes Nodes may now have pointers to one or more other nodes A set of nodes with a single starting point called the root of the tree (root node) Each node is connected by an edge to another node A tree is a connected graph There is a path to every node in the tree A tree has one less edge than the number of nodes Cinda Heeren / Geoffrey Tien 18
Is it a tree? Yes! No! Nodes are not all connected Yes! Although not a binary tree No! There is an extra edge (5 nodes, 5 edges) Yes! Actually equivalent to the blue tree Cinda Heeren / Geoffrey Tien 19
Tree Relationships Node v is said to be a child of u, and u the parent of v if There is an edge between the nodes u and v, and edge root A Parent of B, C, D u is above v in the tree, This relationship can be generalized B C D E and F are descendants of A E F G D and A are ancestors of G B, C and D are siblings F and G are? Cinda Heeren / Geoffrey Tien 20
More Tree Terminology A leaf is a node with no children A path is a sequence of nodes v 1 v n where v i is a parent of v i+1 (1 i n) A subtree is any node in the tree along with all of its descendants A binary tree is a tree with at most two children per node The children are referred to as left and right We can also refer to left and right subtrees Cinda Heeren / Geoffrey Tien 21
Tree Terminology Example A A path from A to D to G A subtree rooted at B B C D E F G C, E, F, G are leaf nodes Cinda Heeren / Geoffrey Tien 22
Binary Tree Terminology A Left subtree of A B C Right child of A D E F G Right subtree of C H I J Cinda Heeren / Geoffrey Tien 23
Measuring Trees The height of a node v is the length of the longest path from v to a leaf The height of the tree is the height of the root The depth of a node v is the length of the path from v to the root This is also referred to as the level of a node Note that there is a slightly different formulation of the height of a tree Where the height of a tree is said to be the number of different levels of nodes in the tree (including the root) Cinda Heeren / Geoffrey Tien 24
Tree Measurements Explained A Height of tree is 3 Height of node B is 2 B C Level 1 D E F G Level 2 Depth of node E is 2 H I J Level 3 Cinda Heeren / Geoffrey Tien 25
Perfect Binary Trees A binary tree is perfect, if No node has only one child And all the leaves have the same depth A perfect binary tree of height h has 2 h+1 1 nodes, of which 2 h are leaves Perfect trees are also complete Cinda Heeren / Geoffrey Tien 26
Height of a Perfect Tree Each level doubles the number of nodes Level 1 has 2 nodes (2 1 ) Level 2 has 4 nodes (2 2 ) or 2 times the number in Level 1 Therefore a tree with h levels has 2 h+1 1 nodes The root level has 1 node 01 11 12 Bottom level has 2 h nodes, i.e. just over half of the nodes are leaves 21 22 23 24 31 32 33 34 35 36 37 38 Cinda Heeren / Geoffrey Tien 27
Complete Binary Trees A binary tree is complete if The leaves are on at most two different levels, The second to bottom level is completely filled in and The leaves on the bottom level are as far to the left as possible D B E A F C Cinda Heeren / Geoffrey Tien 28
Balanced Binary Trees A binary tree is balanced if Leaves are all about the same distance from the root The exact specification varies Sometimes trees are balanced by comparing the height of nodes e.g. the height of a node s right subtree is at most one different from the height of its left subtree (e.g. AVL trees) Sometimes a tree's height is compared to the number of nodes e.g. red-black trees Cinda Heeren / Geoffrey Tien 29
Balanced Binary Trees A A B C B C D E F D E F G Cinda Heeren / Geoffrey Tien 30
Unbalanced Binary Trees A A B C B D E C D F Cinda Heeren / Geoffrey Tien 31
Readings for this lesson Koffman Chapters 6.1 6.3 (Queues) Chapter 8.1 (Tree terminology) Cinda Heeren / Geoffrey Tien 32