lecture 6 data structures

21
Lecture 6 Data Structures

Upload: leanna

Post on 24-Feb-2016

17 views

Category:

Documents


0 download

DESCRIPTION

Lecture 6 Data Structures. Stack. top. 2. 4. 6. 8. 9. top. 2. 4. 6. 8. 9. x. 2. 4. 6. 8. 9. top. 2. 4. 6. 8. 9. x. Queue. head. tail. 1. 3. 6. 2. 5. 1. 2. 3. tail. 1. 2. 3. x. x. 1. 2. 3. head. 1. 2. 3. Priority Queue. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 6  Data Structures

Lecture 6 Data Structures

Page 2: Lecture 6  Data Structures

Stack

;return else return en th

0][ if )Empty(-Stack

falsetrue

StopS

;]][[ ;1][][

),Push(

xStopSStopStop

xS

top

];1][[return and 1][][ else

underflow""error en th)(Empty-Stack if

)Pop(

StopSStopStop

SS

Page 3: Lecture 6  Data Structures

;]][[ ;1][][

),Push(

xStopSStopStop

xS

2

2

4

4

6

6

8

8

9

9 x

top

Page 4: Lecture 6  Data Structures

2

2

4

4

6

6

8

8

9

9 x

top

];1][[return and 1][][ else

underflow""error en th)(Empty-Stack if

)Pop(

StopSStopStop

SS

Page 5: Lecture 6  Data Structures

Queue

;1][][ else 1][en th

][][ if ]];[[

)Dequeue(

QheadQheadQhead

QlengthQheadQheadQx

Q

1 3 5 6 2head tail

;1][][ else 1][en th

][][ if ;]][[

),Enqueue(

QtailQtailQtail

QlengthQtailxQtailQ

xQ

Page 6: Lecture 6  Data Structures

;1][][ else 1][en th

][][ if ;]][[

),Enqueue(

QtailQtailQtail

QlengthQtailxQtailQ

xQ

1

1

2

2

3

3 x

tail

Page 7: Lecture 6  Data Structures

1

1

2

2

3

3

head

;1][][ else 1][en th

][][ if ]];[[

)Dequeue(

QheadQheadQhead

QlengthQheadQheadQx

Q

x

Page 8: Lecture 6  Data Structures

Priority Queue• A priority queue is a data structure for

maintaining a set of elements, each with an associated value, called a key.

• A max-priority queue supports the following operations: Insert(S,x), Maximum(S),

Extract-Max(S), Increase-Key(S,x,k).• Max-Heap can be used for implementing max-priority queue.

Page 9: Lecture 6  Data Structures

];1[return )(Maximum-Heap

key.largest the with ofelement thereturns )Maximum(

AA

SS

Page 10: Lecture 6  Data Structures

max;return );1,Heapify(-Max

;1][-][- ]];[-[]1[

];1[max ;underflow" heap"error n the

1][- if )(Max-Extract-Heap

key.largest with the ofelement thereturns and removes )(Max-Extract

AAsizeheapAsizeheap

AsizeheapAAA

AsizeheapA

SS

Page 11: Lecture 6  Data Structures

);(Parent and )](Parent[][ exchange do

][)](Parent[ and 1 while;][

;key"current an smaller th iskey new"error n the][ if

),,(Key-Increase-Heap

key value.current s' as large asleast at be toassumed is which , valuenew thekey to s'

element of value theincreases ),,(Key-Increase

iiiAiAiAiAi

keyiA

iAkeykeyiA

xkx

kxS

Page 12: Lecture 6  Data Structures

);],[-,(Key-Increase-Heap ;]][-[

;1][-][- ),(Insert-Heap-Max

.set into element theinserts ),Insert(

keyAsizeheapAAsizeheapA

AsizeheapAsizeheapkeyA

SxxS

Page 13: Lecture 6  Data Structures

Doubly Linked List

][Lhead 1 2 3

prev key next

;return ];[ do

][ and nil while];[

),(Search-List

xxnextx

kxkeyxLheadxkL

Page 14: Lecture 6  Data Structures

;][ ;][

;]][[ then][ if

];[][ ),(Insert-List

nilxprevxLhead

xLheadprevnilLheadLheadxnext

xL

1

12

][Lhead

][Lheadx

x is an object!!!

Page 15: Lecture 6  Data Structures

];[]][[ then][ if

];[][ else ][]][[ then

][ if ),(Delete-List

xprevxnextprevnilxnext

xnextLheadxnextxprevnext

nilxprevxL

1

12

x

x is an object!!!

32

Page 16: Lecture 6  Data Structures

Sentinel• A sentinel is a dummy object that allow us to

simplify the boundary condition.• With sentinel, a doubly linked list can be

turned into a circular one. • The sentinel nil[L] is placed between the head

and the tail.

][Lnil

Page 17: Lecture 6  Data Structures

];[]][[ ];[]][[

),(Delete-List

xprevxnextprevxnextxprevnext

xL

Page 18: Lecture 6  Data Structures

Direct Addressing• Direct addressing is a simple technique

that work well when the universe of keys is reasonably small.

• Direct-address table is an array in which each position, or slot, corresponds to a key in the universe.

• If the universe is large, how to do?• Hashing!

Page 19: Lecture 6  Data Structures

Hash Table• Collision resolution by chaining

Page 20: Lecture 6  Data Structures

Hash Functions

)1mod()(.mod)(

kAmkhmkkh

Page 21: Lecture 6  Data Structures

What we learnt in this lecture?

• Review elementary data structures