מבני נתונים מבני נתונים בסיסיים1
מבני נתונים
מבני נתונים בסיסיים
1
התשתית
עבור קבוצה מקום אכסון -מבנה נתונים זקוק ל•של ערכים
המכיל קבוצה של זיכרוןמה שעומד לרשותנו זה •תאים הניתנים לגישה ישירה
יכול להיעשות בשתי צורות בסיסיות ארגון -ה•מערך: רצף של תאים–'רשימה מקושרת וכו: תאים בתפזורת–
2
מערך
מבנה•רצף של תאים–
פעולות•יצירה בגודל נתון–גישה לתא באמצעות אינדקס–
תכונות•גישה ישירה–אינו דינאמי–
3
0 1 2 3 4 5
)'גם עצים וכו(רשימות מקושרות מבנה•
התאים מפוזרים בזיכרון–פעולות•
יצירת רשימה ריקה–חיפוש–הוספה–גריעה–
תכונות•דינאמי–אין גישה ישירה–
4
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
Double Linked List with Sentinels
delete (item) next[prev[item]] = next[item] prev[next[item]] = prev[item]
6
)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
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
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
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
11
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3
12
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3
13
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3(
14
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3(
15
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3(
16
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3([
17
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3([
18
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3([
19
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3([
20
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3([ ]
21
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3(
22
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3( )
23
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3
24
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3
25
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3
26
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3
27
סוגריים מאוזנים: מחסנית
2*(2/[7+5])+3
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
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
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
שימוש בתור
)buffers(חוצצים •
31