lecture 6 data structures. stack top 2 2 4 4 6 6 8 8 9 9x

21
Lecture 6 Data Structures

Upload: opal-mcgee

Post on 18-Jan-2018

233 views

Category:

Documents


0 download

DESCRIPTION

x

TRANSCRIPT

Page 1: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

Lecture 6 Data Structures

Page 2: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

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. Stack top 2 2 4 4 6 6 8 8 9 9x

;]][[ ;1][][

),Push(

xStopSStopStop

xS

2

2

4

4

6

6

8

8

9

9 x

top

Page 4: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

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. Stack top 2 2 4 4 6 6 8 8 9 9x

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. Stack top 2 2 4 4 6 6 8 8 9 9x

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

][][ if ;]][[

),Enqueue(

QtailQtailQtail

QlengthQtailxQtailQ

xQ

1

1

2

2

3

3 x

tail

Page 7: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

1

1

2

2

3

3

head

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

][][ if ]];[[

)Dequeue(

QheadQheadQhead

QlengthQheadQheadQx

Q

x

Page 8: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

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. Stack top 2 2 4 4 6 6 8 8 9 9x

];1[return )(Maximum-Heap

key.largest the with ofelement thereturns )Maximum(

AA

SS

Page 10: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

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. Stack top 2 2 4 4 6 6 8 8 9 9x

);(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. Stack top 2 2 4 4 6 6 8 8 9 9x

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

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

.set into element theinserts ),Insert(

keyAsizeheapAAsizeheapA

AsizeheapAsizeheapkeyA

SxxS

Page 13: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

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. Stack top 2 2 4 4 6 6 8 8 9 9x

;][ ;][

;]][[ then][ if

];[][ ),(Insert-List

nilxprevxLhead

xLheadprevnilLheadLheadxnext

xL

1

12

][Lhead

][Lheadx

x is an object!!!

Page 15: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

];[]][[ then][ if

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

][ if ),(Delete-List

xprevxnextprevnilxnext

xnextLheadxnextxprevnext

nilxprevxL

1

12

x

x is an object!!!

32

Page 16: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

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. Stack top 2 2 4 4 6 6 8 8 9 9x

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

),(Delete-List

xprevxnextprevxnextxprevnext

xL

Page 18: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

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. Stack top 2 2 4 4 6 6 8 8 9 9x

Hash Table• Collision resolution by chaining

Page 20: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

Hash Functions

)1mod()(.mod)(

kAmkhmkkh

Page 21: Lecture 6 Data Structures. Stack top 2 2 4 4 6 6 8 8 9 9x

What we learnt in this lecture?

• Review elementary data structures