data structures: queuessvenkat/summer2010dsa/slides/session11.pdf · data structures: queues...

8
Data Structures: Queues Fundamental Data Structures Sequences form some of the very basic data structures Sequences are a collection of data or objects There are different types of sequences List—ordered collection or sequence of objects Set—unordered collection, often elements are unique Hash (or hashmap)—associative collection of elements based on a key-value pair 2

Upload: others

Post on 17-Jul-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures: Queuessvenkat/summer2010dsa/slides/session11.pdf · Data Structures: Queues Fundamental Data Structures Sequences form some of the very basic data structures Sequences

Data Structures: Queues

Fundamental Data StructuresSequences form some of the very basic data structures

Sequences are a collection of data or objects

There are different types of sequences

List—ordered collection or sequence of objects

Set—unordered collection, often elements are unique

Hash (or hashmap)—associative collection of elements

based on a key-value pair

2

Page 2: Data Structures: Queuessvenkat/summer2010dsa/slides/session11.pdf · Data Structures: Queues Fundamental Data Structures Sequences form some of the very basic data structures Sequences

Types of Queues

Queue—a FIFO policy for maintaining elements

Stack—a LIFO policy

Deque—Inserts and removes allowed at both ends

3

General Operationsinsert

delete

search

minimum

maximum

successor

predecessor

iteration4

Page 3: Data Structures: Queuessvenkat/summer2010dsa/slides/session11.pdf · Data Structures: Queues Fundamental Data Structures Sequences form some of the very basic data structures Sequences

Stack ImplementationYou need to care for overflows and underflows

Evaluation of push and pop is O(1) time

IsEmpty(S) return S.top = 0Push(S, x) S.top <- S.top + 1 S[S.top] <- xPop(S) if IsEmpty(S) raise error underflow S.top <- S.top - 1 return S[S.top + 1]

5

QueueQueues allow insert (or enque) and delete (deque—don’t confuse this operation with the Deque data structure)

The operations of insert and delete are O(1) time

Insert(Q, x) Q[Q.tail] <- x if Q.tail = Q.length Q.tail <- 1 else Q.tail = Q.tail + 1

6

Delete(Q) x <- Q[Q.head] if Q.head = Q.length Q.head <- 1 else Q.head = Q.head + 1 return x

Page 4: Data Structures: Queuessvenkat/summer2010dsa/slides/session11.pdf · Data Structures: Queues Fundamental Data Structures Sequences form some of the very basic data structures Sequences

Data Structures in Java

Java 1.0 provided a few data structures. For most part you should consider those (Vector, Hashtable, ...) as deprecated

List<>, Map<>, Deque<> are more modern

Let’s explore ArrayList and Deque

7

Linked ListElements arranged in linear order

Order determined using pointers

Doubly linked lists have key, prev, and next

Search is Θ(n) operation in time, insert is O(1) in time

8

ListSearch(L, k) x <- L.head while x != null && x.key != k x <- x.next return x

Insert(L, x) x.next <- L.head x.prev <- null if L.head != null L.head.prev <- x L.head <- x

Page 5: Data Structures: Queuessvenkat/summer2010dsa/slides/session11.pdf · Data Structures: Queues Fundamental Data Structures Sequences form some of the very basic data structures Sequences

Linked List

Delete is O(1) if key is given, otherwise Θ(n) to search and delete

9

Delete(L, x) if x.prev != null x.prev.next <- x.next else L.head <- x.next if x.next != null x.next.prev <- x.prev

Binary TreesCan be represented using parent, left, and right pointers

10

Page 6: Data Structures: Queuessvenkat/summer2010dsa/slides/session11.pdf · Data Structures: Queues Fundamental Data Structures Sequences form some of the very basic data structures Sequences

Rooted TreesRooted trees with unbounded branching

Above technique for binary tree is not scalable to handle this

You can’t have many pointers

hard to know how many to have

waste of space if you pre allocate

A scheme that uses only O(n) space for n-node rooted tree can use left-child pointer and right-sibling pointer

11

Rooted Trees

12

Page 7: Data Structures: Queuessvenkat/summer2010dsa/slides/session11.pdf · Data Structures: Queues Fundamental Data Structures Sequences form some of the very basic data structures Sequences

What’s the Efficiency of ops?

Search a linked list

Search a queue

insert element into a queue to the end

insert element into the middle of a queue

Find minimum in a queue

13

Iterators

Why can’t a collection simply provide navigation operation?

Why do you need iterators in the first place?

14

Page 8: Data Structures: Queuessvenkat/summer2010dsa/slides/session11.pdf · Data Structures: Queues Fundamental Data Structures Sequences form some of the very basic data structures Sequences

Iterators

Two types of iterators

External iterators

You typically use a for loop for this

Internal iterators

Collections typically provide a way to navigate through the contents using these iterators

15

Iterators

Iterators in Java

Iterators in functional languages (and languages that support functional style operations)

16