lecture 6 data structures
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 PresentationTRANSCRIPT
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
;]][[ ;1][][
),Push(
xStopSStopStop
xS
2
2
4
4
6
6
8
8
9
9 x
top
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
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
;1][][ else 1][en th
][][ if ;]][[
),Enqueue(
QtailQtailQtail
QlengthQtailxQtailQ
xQ
1
1
2
2
3
3 x
tail
1
1
2
2
3
3
head
;1][][ else 1][en th
][][ if ]];[[
)Dequeue(
QheadQheadQhead
QlengthQheadQheadQx
Q
x
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.
];1[return )(Maximum-Heap
key.largest the with ofelement thereturns )Maximum(
AA
SS
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
);(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
);],[-,(Key-Increase-Heap ;]][-[
;1][-][- ),(Insert-Heap-Max
.set into element theinserts ),Insert(
keyAsizeheapAAsizeheapA
AsizeheapAsizeheapkeyA
SxxS
Doubly Linked List
][Lhead 1 2 3
prev key next
;return ];[ do
][ and nil while];[
),(Search-List
xxnextx
kxkeyxLheadxkL
;][ ;][
;]][[ then][ if
];[][ ),(Insert-List
nilxprevxLhead
xLheadprevnilLheadLheadxnext
xL
1
12
][Lhead
][Lheadx
x is an object!!!
];[]][[ then][ if
];[][ else ][]][[ then
][ if ),(Delete-List
xprevxnextprevnilxnext
xnextLheadxnextxprevnext
nilxprevxL
1
12
x
x is an object!!!
32
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
];[]][[ ];[]][[
),(Delete-List
xprevxnextprevxnextxprevnext
xL
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!
Hash Table• Collision resolution by chaining
Hash Functions
)1mod()(.mod)(
kAmkhmkkh
What we learnt in this lecture?
• Review elementary data structures