Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Similar documents
Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Binary Trees

Trees. (Trees) Data Structures and Programming Spring / 28

A set of nodes (or vertices) with a single starting point

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Trees. Tree Structure Binary Tree Tree Traversals

Arrays and Linked Lists

12 Abstract Data Types

Programming II (CS300)

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

What Can You Use a Queue For?

Postfix (and prefix) notation

CS301 - Data Structures Glossary By

Trees. Eric McCreath

Binary Trees, Binary Search Trees

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Trees. CSE 373 Data Structures

Chapter 8: Data Abstractions

Why Do We Need Trees?

CSE 530A. B+ Trees. Washington University Fall 2013

CMPT 225. Binary Search Trees

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Algorithms. AVL Tree

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Queues. Queue ADT Queue Implementation Priority Queues

Lecture 13: AVL Trees and Binary Heaps

Trees. A tree is a directed graph with the property

Chapter 20: Binary Trees

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Lecture No. 10. Reference Variables. 22-Nov-18. One should be careful about transient objects that are stored by. reference in data structures.

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Chapter 4: Trees. 4.2 For node B :

Data Structures Question Bank Multiple Choice

CS350: Data Structures B-Trees

csci 210: Data Structures Trees

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Design and Analysis of Algorithms Lecture- 9: B- Trees

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

CS24 Week 8 Lecture 1

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

CMSC 341 Lecture 14: Priority Queues, Heaps

CE 221 Data Structures and Algorithms

EE 368. Weeks 5 (Notes)

Garbage Collection: recycling unused memory

Algorithms. Deleting from Red-Black Trees B-Trees

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

CE 221 Data Structures and Algorithms

Programming II (CS300)

Algorithms and Data Structures

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Analysis of Algorithms

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

CS 216 Exam 1 Fall SOLUTION

CS 350 : Data Structures B-Trees

Data Structures Lesson 9

Self-Balancing Search Trees. Chapter 11

CSE 230 Intermediate Programming in C and C++ Binary Tree

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Binary Search Trees. Analysis of Algorithms

ITI Introduction to Computing II

Priority Queue ADT. Revised based on textbook author s notes.

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

Properties of red-black trees

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

B-Trees and External Memory

Module 2: Priority Queues

Fundamentals of Data Structure

Linked Memory. Pointers Linked Lists. September 21, 2017 Cinda Heeren / Geoffrey Tien 1

Data Structure. IBPS SO (IT- Officer) Exam 2017

Hash Tables. Lower bounds on sorting Hash functions. March 05, 2018 Cinda Heeren / Geoffrey Tien 1

Motivation for B-Trees

CSE 214 Computer Science II Introduction to Tree

B-Trees and External Memory

Data Structures and Algorithms

Advanced Set Representation Methods

Module 2: Priority Queues

COMP : Trees. COMP20012 Trees 219

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Recall: Properties of B-Trees

Transcription:

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