Lecture 6 Data Structures
description
Transcript of Lecture 6 Data Structures
![Page 1: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/1.jpg)
Lecture 6 Data Structures
![Page 2: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/3.jpg)
;]][[ ;1][][
),Push(
xStopSStopStop
xS
2
2
4
4
6
6
8
8
9
9 x
top
![Page 4: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/6.jpg)
;1][][ else 1][en th
][][ if ;]][[
),Enqueue(
QtailQtailQtail
QlengthQtailxQtailQ
xQ
1
1
2
2
3
3 x
tail
![Page 7: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/7.jpg)
1
1
2
2
3
3
head
;1][][ else 1][en th
][][ if ]];[[
)Dequeue(
QheadQheadQhead
QlengthQheadQheadQx
Q
x
![Page 8: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/9.jpg)
];1[return )(Maximum-Heap
key.largest the with ofelement thereturns )Maximum(
AA
SS
![Page 10: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/11.jpg)
);(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](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/12.jpg)
);],[-,(Key-Increase-Heap ;]][-[
;1][-][- ),(Insert-Heap-Max
.set into element theinserts ),Insert(
keyAsizeheapAAsizeheapA
AsizeheapAsizeheapkeyA
SxxS
![Page 13: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/14.jpg)
;][ ;][
;]][[ then][ if
];[][ ),(Insert-List
nilxprevxLhead
xLheadprevnilLheadLheadxnext
xL
1
12
][Lhead
][Lheadx
x is an object!!!
![Page 15: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/15.jpg)
];[]][[ then][ if
];[][ else ][]][[ then
][ if ),(Delete-List
xprevxnextprevnilxnext
xnextLheadxnextxprevnext
nilxprevxL
1
12
x
x is an object!!!
32
![Page 16: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/17.jpg)
];[]][[ ];[]][[
),(Delete-List
xprevxnextprevxnextxprevnext
xL
![Page 18: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/19.jpg)
Hash Table• Collision resolution by chaining
![Page 20: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/20.jpg)
Hash Functions
)1mod()(.mod)(
kAmkhmkkh
![Page 21: Lecture 6 Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022070423/56816799550346895ddcd740/html5/thumbnails/21.jpg)
What we learnt in this lecture?
• Review elementary data structures