12

Click here to load reader

Data Structures - 06. Queues, Deques, Dictionaries

Embed Size (px)

DESCRIPTION

Slide da cadeira de Estrutura de Dados, ministrado pelo Prof. Dr. Christian Pagot, na Universidade Federal da Paraíba.

Citation preview

Page 1: Data Structures - 06. Queues, Deques, Dictionaries

Universidade Federal da ParaíbaCentro de Informática

Queues, Deques and DictionariesLecture 9

1107186 – Estruturas de Dados

Prof. Christian Azambuja PagotCI / UFPB

Page 2: Data Structures - 06. Queues, Deques, Dictionaries

2Universidade Federal da ParaíbaCentro de Informática

What is a Queue?

● A queue is an abstract data type where elements can be inserted (pushed) and removed (popped) according to the following policies:– Push inserts a new item in the back of the queue.

– Pop removes the item at the front of the queue.

– The above insert/remove policy is also called FIFO (First In-First Out).

Page 3: Data Structures - 06. Queues, Deques, Dictionaries

3Universidade Federal da ParaíbaCentro de Informática

What is a Queue?

● Example:

A

front back

A B

front back

A B C

front back

B C

front back

1) push('A')

2) push('B')

3) push('C')

4) pop()

Page 4: Data Structures - 06. Queues, Deques, Dictionaries

4Universidade Federal da ParaíbaCentro de Informática

What is a Deque?

● Deque: Double Ended Queue.● A deque is an abstract data type where

elements can be inserted (pushed) and removed (popped) according to the following policies:– Push inserts a new item in the back or in the

front of the deque.

– Pop removes the item at the front or at the back of the deque.

Page 5: Data Structures - 06. Queues, Deques, Dictionaries

5Universidade Federal da ParaíbaCentro de Informática

What is a Deque?

● Example:

A

front back

A B

front back

C A B

front back

C A

front back

1) push_front('A')

2) push_back('B')

3) push_front('C')

4) pop_back()

Page 6: Data Structures - 06. Queues, Deques, Dictionaries

6Universidade Federal da ParaíbaCentro de Informática

What is a Dictionary?

● Associative array, map, symbol table or dictionary.

● A dictionary is an abstract data type composed by a collection of (key, value) pairs, where key is unique.

Page 7: Data Structures - 06. Queues, Deques, Dictionaries

7Universidade Federal da ParaíbaCentro de Informática

What is a Dictionary?

● Example:– The set of loans in a library:

“Computer Networks” Christian

“Ordinary Differential Equations” Paulo

“Dicionário Gremista” Christian

Key

Data item

Page 8: Data Structures - 06. Queues, Deques, Dictionaries

8Universidade Federal da ParaíbaCentro de Informática

What is a Dictionary?

● Common operations:– Search(D, k).

– Insert(D, x).

– Delete(D, x).

– Max(D), Min(D).

– Predecessor(D, k), Successor(D, k).

D: dictionary.k: key.x: data item.

Searches for key k in D, and returns a pointer to the corresponding x data item.

Add the x data item to D.

Given a pointer to the x data item in D, removes it from D.

Returns a pointer to the data itemwith largest (or smallest) key.

Returns a pointer to the data itemwhose key is immediately before

(after) k in sorted order.

Page 9: Data Structures - 06. Queues, Deques, Dictionaries

9Universidade Federal da ParaíbaCentro de Informática

Analysis of Some Possible Dictionary Implementations (by Steven S. Skiena)

● Array:

Operation Unsorted Array

Sorted Array

Search(D, k)

Insert(D, x)

Delete(D, x)

Successor(D, k)

Predecessor(D, k)

Minimum(D)

Maximum(D)

O(n)

O(1)

O(1)*

O(n)

O(n)

O(n)

O(n)

O( log n)O(n)

O(n)

O(1)

O(1)

O(1)

O(1)

Page 10: Data Structures - 06. Queues, Deques, Dictionaries

10Universidade Federal da ParaíbaCentro de Informática

Analysis of Some Possible Dictionary Implementations (by Steven S. Skiena)

● Single Linked List:

Operation Unsorted List

Sorted List

Search(D, k)

Insert(D, x)

Delete(D, x)

Successor(D, k)

Predecessor(D, k)

Minimum(D)

Maximum(D)

O(n)

O(1)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(1)

O(n)

O(1)

O(1)

Page 11: Data Structures - 06. Queues, Deques, Dictionaries

11Universidade Federal da ParaíbaCentro de Informática

Analysis of Some Possible Dictionary Implementations (by Steven S. Skiena)

● Doubly Linked List:

Operation Unsorted List

Sorted List

Search(D, k)

Insert(D, x)

Delete(D, x)

Successor(D, k)

Predecessor(D, k)

Minimum(D)

Maximum(D)

O(n)

O(1)

O(1)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(1)

O(1)

O(1)

O(1)

O(1)

Page 12: Data Structures - 06. Queues, Deques, Dictionaries

12Universidade Federal da ParaíbaCentro de Informática

Analysis of Some Possible Dictionary Implementations (by Steven S. Skiena)

● Comparing all possibilities:

Operation UA SA USLL SSLL UDLL SDLLSearch(D, k)

Insert(D, x)

Delete(D, x)

Successor(D, k)

Predecessor(D, k)

Minimum(D)

Maximum(D)

O(n)

O(1)

O(1)

O(n)

O(n)

O(n)

O(n)

O( log n)O(n)

O(n)

O(1)

O(1)

O(1)

O(1)

O(n)

O(1)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(1)

O(n)

O(1)

O(1)

O(n)

O(1)

O(1)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(1)

O(1)

O(1)

O(1)

O(1)

UA: Unsorted array SA: sorted array.USSL: Unsorted Single Linked List SSSL: Sorted Single Linked ListUDSL: Unsorted Doubly Linked List SDSL: Sorted Doubly Linked List