Download presentation
Presentation is loading. Please wait.
1
ADT description Implementations
Queues ADT description Implementations October 03, 2017 Cinda Heeren / Geoffrey Tien
2
Cinda Heeren / Geoffrey Tien
Queues Example: Print Queue 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 October 03, 2017 Cinda Heeren / Geoffrey Tien
3
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 October 03, 2017 Cinda Heeren / Geoffrey Tien
4
Cinda Heeren / Geoffrey Tien
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 October 03, 2017 Cinda Heeren / Geoffrey Tien
5
Cinda Heeren / Geoffrey Tien
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 October 03, 2017 Cinda Heeren / Geoffrey Tien
6
Cinda Heeren / Geoffrey Tien
Queue Implementation Using an Array 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 What to do when items are removed? Either move all remaining items down – slow Or increment the front index – wastes space October 03, 2017 Cinda Heeren / Geoffrey Tien
7
Cinda Heeren / Geoffrey Tien
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 1 3 2 4 5 6 7 1 2 3 4 5 6 7 October 03, 2017 Cinda Heeren / Geoffrey Tien
8
Cinda Heeren / Geoffrey Tien
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; October 03, 2017 Cinda Heeren / Geoffrey Tien
9
Cinda Heeren / Geoffrey Tien
Array Queue Example 1 Queue q; front num q.enqueue(6); 6 1 2 3 4 5 Insert item at (front + num) % queue.length, then increment num October 03, 2017 Cinda Heeren / Geoffrey Tien
10
Cinda Heeren / Geoffrey Tien
Array Queue Example 1 2 5 4 3 Queue q; front num q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); 6 4 7 3 8 1 2 3 4 5 q.dequeue(); Insert item at (front + num) % queue.length, then increment num q.dequeue(); Remove item at front, then decrement num and make front = (front + 1) % queue.length October 03, 2017 Cinda Heeren / Geoffrey Tien
11
Cinda Heeren / Geoffrey Tien
Array Queue Example 2 5 4 3 Queue q; front num q.enqueue(6); q.enqueue(4); q.enqueue(7); q.enqueue(3); q.enqueue(8); 5 7 3 8 9 1 2 3 4 5 q.dequeue(); Insert item at (front + num) % queue.length, then increment num q.dequeue(); q.enqueue(9); Remove item at front, then decrement num and make front = (front + 1) % queue.length q.enqueue(5); Need to check that the back of the queue does not overtake the front October 03, 2017 Cinda Heeren / Geoffrey Tien
12
Cinda Heeren / Geoffrey Tien
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 ? October 03, 2017 Cinda Heeren / Geoffrey Tien
13
Cinda Heeren / Geoffrey Tien
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; October 03, 2017 Cinda Heeren / Geoffrey Tien
14
Cinda Heeren / Geoffrey Tien
List Queue Example Queue q; q.enqueue(6); front 6 q.enqueue(4); q.enqueue(7); 4 back q.enqueue(3); q.dequeue(); 7 3 October 03, 2017 Cinda Heeren / Geoffrey Tien
15
Cinda Heeren / Geoffrey Tien
Exercise Shallow Copy, Deep Copy 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); Queue qB = qA; qA.Enqueue(6); qB.Enqueue(52); qB.Dequeue(); 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? October 03, 2017 Cinda Heeren / Geoffrey Tien
16
Introduction & terminology
Trees Introduction & terminology October 03, 2017 Cinda Heeren / Geoffrey Tien
17
Cinda Heeren / Geoffrey Tien
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 October 03, 2017 Cinda Heeren / Geoffrey Tien
18
Cinda Heeren / Geoffrey Tien
Trees 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 Root October 03, 2017 Cinda Heeren / Geoffrey Tien
19
Cinda Heeren / Geoffrey Tien
Is it a tree? No! Nodes are not all connected Yes! Yes! Although not a binary tree No! There is an extra edge (5 nodes, 5 edges) Yes! Actually equivalent to the blue tree October 03, 2017 Cinda Heeren / Geoffrey Tien
20
Cinda Heeren / Geoffrey Tien
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 u is above v in the tree, This relationship can be generalized E and F are descendants of A D and A are ancestors of G B, C and D are siblings F and G are? root Parent of B, C, D A edge B C D E F G October 03, 2017 Cinda Heeren / Geoffrey Tien
21
Cinda Heeren / Geoffrey Tien
More Tree Terminology A leaf is a node with no children A path is a sequence of nodes v1 … vn where vi is a parent of vi+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 October 03, 2017 Cinda Heeren / Geoffrey Tien
22
Tree Terminology Example
A path from A to D to G B C D A subtree rooted at B E F G C, E, F, G are leaf nodes October 03, 2017 Cinda Heeren / Geoffrey Tien
23
Binary Tree Terminology
C Right child of A Left subtree of A D E F G Right subtree of C H I J October 03, 2017 Cinda Heeren / Geoffrey Tien
24
Cinda Heeren / Geoffrey Tien
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) October 03, 2017 Cinda Heeren / Geoffrey Tien
25
Tree Measurements Explained
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 October 03, 2017 Cinda Heeren / Geoffrey Tien
26
Cinda Heeren / Geoffrey Tien
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 2h+1 – 1 nodes, of which 2h are leaves Perfect trees are also complete October 03, 2017 Cinda Heeren / Geoffrey Tien
27
Height of a Perfect Tree
Each level doubles the number of nodes Level 1 has 2 nodes (21) Level 2 has 4 nodes (22) or 2 times the number in Level 1 Therefore a tree with ℎ levels has 2 ℎ+1 −1 nodes The root level has 1 node 01 Bottom level has 2h nodes, i.e. just over half of the nodes are leaves 12 11 21 22 23 24 31 32 33 34 35 36 37 38 October 03, 2017 Cinda Heeren / Geoffrey Tien
28
Cinda Heeren / Geoffrey Tien
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 A B C D E F October 03, 2017 Cinda Heeren / Geoffrey Tien
29
Cinda Heeren / Geoffrey Tien
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 October 03, 2017 Cinda Heeren / Geoffrey Tien
30
Cinda Heeren / Geoffrey Tien
Balanced Binary Trees A A B C B C D E F D E F G October 03, 2017 Cinda Heeren / Geoffrey Tien
31
Unbalanced Binary Trees
F October 03, 2017 Cinda Heeren / Geoffrey Tien
32
Readings for this lesson
Koffman Chapters 6.1 – 6.3 (Queues) Chapter 8.1 (Tree terminology) October 03, 2017 Cinda Heeren / Geoffrey Tien
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.