Top Banner
מבני נתונים מבני נתונים בסיסיים1
31

םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

Jul 10, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

מבני נתונים

מבני נתונים בסיסיים

1

Page 2: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

התשתית

עבור קבוצה מקום אכסון -מבנה נתונים זקוק ל•של ערכים

המכיל קבוצה של זיכרוןמה שעומד לרשותנו זה •תאים הניתנים לגישה ישירה

יכול להיעשות בשתי צורות בסיסיות ארגון -ה•מערך: רצף של תאים–'רשימה מקושרת וכו: תאים בתפזורת–

2

Page 3: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

מערך

מבנה•רצף של תאים–

פעולות•יצירה בגודל נתון–גישה לתא באמצעות אינדקס–

תכונות•גישה ישירה–אינו דינאמי–

3

0 1 2 3 4 5

Page 4: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

)'גם עצים וכו(רשימות מקושרות מבנה•

התאים מפוזרים בזיכרון–פעולות•

יצירת רשימה ריקה–חיפוש–הוספה–גריעה–

תכונות•דינאמי–אין גישה ישירה–

4

Page 5: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

Double Linked List

search (k, list) x = head[list] while (x ≠ null &

key[x] ≠ k) do x = next[x]

return x

insert (item, list) next[item] = head[list] if (head[list] ≠ null) then

prev[head[list]] = item head[list] = item prev[item] = null

delete (item, list) if (prev[item] ≠ null) then

next[prev[item]] = next[item] else

head[list] = next[item] if (next[item] ≠ null) then

prev[next[item]] = prev[item]

5

Page 6: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

Double Linked List with Sentinels

delete (item) next[prev[item]] = next[item] prev[next[item]] = prev[item]

6

Page 7: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

)ADT(מבני נתונים מופשטים

– a set of values

– a set of operations, which can be applied uniformly to all these values

– a data representation, and the implementation of the operations.

abstract

implementation

• Sometimes the data type is completely identified with the implementation so we talk about concrete data type.

7

Page 8: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

Abstract Stack

• A stack is a collection of items, for which the following operations are defined:– create(S) creates an empty stack S.

– isEmpty(S) returns true if S is empty, and falseotherwise.

– push(S, item) adds the given item to the stack S.

– pop(S) removes and returns the most recently added item from the stack S.

8

Page 9: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

Implementing Stack with Linked List

• create - create empty list

• isEmpty – iff the list is empty

• push - simply adds the item to the head of the list

• pop - returns the head of the list, and replaces the list by its tail

9

Page 10: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

Implementing (Finite) Stack with Array

• create(S, N)1. Initialize an array of size N2. Initialize an internal pointer top to 0.

• isEmpty(S)1. true iff top = 0

• push(S,item)1. if top = N2. then error “overflow“3. else top top+14. S[top] item;

• pop(S)1. if isEmpty (S)2. then error "underflow"3. else top top-1 4. return(S[top+1]) 10

Page 11: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

11

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3

Page 12: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

12

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3

Page 13: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

13

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3(

Page 14: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

14

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3(

Page 15: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

15

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3(

Page 16: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

16

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3([

Page 17: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

17

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3([

Page 18: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

18

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3([

Page 19: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

19

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3([

Page 20: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

20

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3([ ]

Page 21: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

21

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3(

Page 22: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

22

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3( )

Page 23: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

23

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3

Page 24: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

24

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3

Page 25: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

25

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3

Page 26: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

26

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3

Page 27: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

27

סוגריים מאוזנים: מחסנית

2*(2/[7+5])+3

Page 28: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

Abstract Queue

• A queue is a collection of items, for which the following operations are defined:– create(Q) creates an empty queue Q.

– isEmpty(Q) returns true if Q is empty, and falseotherwise.

– enqueue(Q, item) adds the given item to the queue Q.

– dequeue(Q) removes and returns from the queue Q the least recently added item.

28

Page 29: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

Implementing Queue with Linked List

• create - create empty list

• isEmpty – true iff the list is empty

• enqueue - simply adds the item to the tail of the list

• dequeue- returns the head of the list, and replaces the list by its tail

29

Page 30: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

Implementing (Finite) Queue with Array

• create(Q, N)1. Initialize an array of size N // avoid full array 2. Internal pointers head and tail are initialized to point to the first cell in Q.

• isEmpty(Q)1. true iff head = tail

• enqueue(Q, item)1. if tail = N then next 12. else next tail + 13. if next = head // queue is full4. then error “overflow“5. else Q[tail] item; 6. tail ← next

• dequeue(Q)1. if isEmpty (Q)2. then error "underflow“3. else x Q [head]4. if head = N then head 15. else head head + 16. return x

30

Page 31: םינותנ ינבמ - BGUds132/wiki.files/Presentation03.pdf · תיתשתה הצובק רובע ןוסכא םוקמ -ל קוקז םינותנ הנבמ• םיכרע לש לש הצובק

שימוש בתור

)buffers(חוצצים •

31