Download presentation
Presentation is loading. Please wait.
Published byDorthy Scott Modified over 9 years ago
1
Data Structures Hanoch Levi and Daniel Cohen-Or Nov 2011 Lecture 1 Abstract Data Types Lists, Stacks, Queues, Deques Arrays, Linked Lists Amortized Analysis
2
Lists (Sequences) Insert b at position i : Delete the item at position i: [a 0 a 1 a 2 a 3 … a i-1 a i a i+1 … a n-2 a n-1 ] b Retrieve the item at position i:
3
3 Abstract Data Type (ADT) Lists (Sequences) List() – Create an empty list Length(L) – Return the length of list L Insert(L,i,b) – Insert b as the i-th item of L Retrieve(L,i) – Return the i-th item of L Delete(L,i) – Delete and return the i-th item of L ( Search(L,b) – Return the position of b in L, or −1 ) Interesting special cases: Insert-First(L,b), Retrieve-First(L), Delete-First(L) Insert-Last(L,b), Retrieve-Last(L), Delete-Last(L)
4
Abstract Data Types (ADT) Stacks, Queues, Dequeues Stacks – Implement only: Insert-Last(L,b), Retrieve-Last(L), Delete-Last(L) Also known as: Push(L,b), Top(L), Pop(L) Last In First Out (LIFO) Queues – Implement only: Insert-Last(L,b), Retrieve-First(L), Delete-First(L) First In First Out (FIFO) Deques (double ended queues) – Implement only: Insert-First(L,b), Retrieve-First(L), Delete-First(L) Insert-Last(L,b), Retrieve-Last(L), Delete-Last(L)
5
יישום השמטת כפולים Procedure PURGE (L:LIST) p,q: Position begin (1) p:= FIRST(L) (2)while p<>END(L) do begin (3) q:= NEXT(p,L) (4) while q<>END(L) do (5) if same (RETRIVE(p,L), RETRIVE(q,L)) (6) then DELETE(q,L) (7)else (8) q:= NEXT(q,L) (9) p:= NEXT(p,L) end; end; {PURGE} qאינו מקודם כי הרשימה התכווצה p q
6
Implementing lists using arrays L array maxlen length a0a0 a1a1 … a n−1 n M M n *** We need to know the maximal length M in advance. Retrieve(L,i) (of any element) takes O(1) time Insert-Last(L,b) and Delete-Last(L) take O(1) time Stack operations in O(1) time
7
Implementing lists using arrays L array maxlen length a0a0 a1a1 … a n−1 n M M n *** We need to know the maximal length M in advance. Retrieve(L,i) (of any element) takes O(1) time Insert-Last(L,b) and Delete-Last(L) take O(1) time Insert(L,i,b) and Delete(L,i) take O(n−i+1) time
8
Implementing lists using arrays L array maxlen length n M M n n−1 0 1 n+1 Delete-Last very similar
9
Implementing lists using arrays L array maxlen length n M M n n−1 0 1 n+1 i We need to move n−i items, then insert. O(n−i+1) time.
10
Circular Array 10 Start End
11
Implementing lists using circular arrays Implement queue and deque operations in O(1) time L array maxlen length n M M n start 2 New field: start 01 2
12
Implementing lists using circular arrays Implement queue and deque operations in O(1) time L array maxlen length M−4 M M start 7 Occupied region can wrap around! M−401M−1 2
13
Implementing lists using circular arrays Implement queue and deque operations in O(1) time L array maxlen length 0 M M n start n−1 0 1 1 n Code of other operations similar
14
Implementing lists using singly linked lists Lists of unbounded length Support some additional operations length n last L first a0a0 a1a1 a n-1 a2a2 … List object List-Node object itemnext
15
item Insert-First with singly linked lists L first last length … n a0a0 a1a1 a n-1 a2a2 … next Insert-First(L,b) Generate a new List-Node object B containing b Increment L.length b (Return B) B Add B to the list n+1 Adjust L.last, if necessary
16
item Insert-First with singly linked lists L first last length … n a0a0 a1a1 a n-1 a2a2 … next b B n+1 Insert-Last and Delete-First– very similar, also O(1) time Unfortunately, Delete-Last requires O(n+1) time
17
item Retrieve with singly linked lists L first last length n a0a0 a1a1 a n-1 a2a2 … next Retrieving the i-th item takes O(i+1) time
18
item Insert with singly linked lists L first last length n a0a0 a1a1 a n-1 a2a2 … next Inserting an item into position i takes O(i+1) time
19
Inserting a node (After a given node) Insert-After(A,B) – Insert B after A a i−1 aiai … A … b B
20
Deleting a node (After a given node, not the last one) a i−1 aiai … A … Delete-After(A) – Delete the node following A a i+1 What happens to the node removed?
21
21 Abstraction barriers List Insert, Retrieve, Delete Search User List-Node Retrieve-Node Insert-After, Delete-After
22
22 Circular arrays vs. Linked Lists Circular arrays Linked lists Insert/Delete-First Insert-Last O(1) Delete-LastO(1)O(n) Insert/Delete(i)O(min{i+1,n−i+1})O(i+1) Retrieve(i)O(1)O(i+1) Arrays have a fixed size. (For the time being.) Linked lists can do a few more things. Arrays always better than lists?
23
Concatenating lists Concat(L 1,L 2 ) – Attach L 2 to the end of L 1 L1L1 n a0a0 a1a1 a n-1 a2a2 … m b0b0 b1b1 b m-1 b2b2 … L2L2 n+m (What happened to L 2 ?) O(1) time!
24
24 Circular arrays vs. Linked Lists Circular arrays Linked lists Insert/Delete-First Insert-Last O(1) Delete-LastO(1)O(n) Insert/Delete(i)O(min{i+1,n−i+1})O(i+1) Retrieve(i)O(1)O(i+1) ConcatO(min{n 1,n 2 }+1)O(1) Linked list can do still more things…
25
Modified ADT for lists The current specification does not allow us to utilize one of the main capabilities of linked lists: Insert-After, Delete-After, Next in O(1) time We can define a new ADT in which the user is allowed to call List-Node, Insert-After, Delete-After.
26
Lists – A second abstraction [ a 0 a 1 … a i … a n-1 ] A List-Node(b) – Create a List-Node containing item b Item(B) – Return the item contained in List-Node B Insert(L,i,B) – Insert B as the i-th List-Node of L Retrieve(L,i) – Return the i-th List-Node of L Delete(L,i) – Delete and return the i-th List-Node of L Concat(L 1, L 2 ) – Concatenate L 1 and L 2 B Lists are now composed of List-Nodes List() – Create an empty list Length(L) – Return the length of list L
27
Next(A) – Return the List-Node following A (assuming there is one) Insert-After(A,B) – Insert B after A (assuming B is not in any list) Delete-After(A) – Delete the List-Node following A (assuming …) Note: L is not an argument of these operations! Lists – A second abstraction [ a 0 a 1 … a i … a n-1 ] A B We now allow the following additional operations: These operations assume that A is contained in some list, while B is not contained in any list
28
Implementing lists (of List-Nodes) using singly linked lists length n last L first a0a0 a1a1 a n-1 a2a2 … List object List-Node object itemnext We cannot maintain length We cannot maintain last
29
Delete vs. Delete-After We have: Delete-After(A) – Delete the List-Item following A Even more useful would be to have: Delete(A) – Delete List-Item A from the list containing it We cannot delete A from a singly linked list without a pointer to the List-Item preceding it. [ a 0 a 1 … a i … a n-1 ] A B
30
Implementing lists using doubly linked lists Inserting and deleting nodes in O(1) time Concatenation also in O(1) time First and last elements are special. Complicates code. a0a0 a n-1 … a1a1 a2a2 L first nextitem prev Each List-Node now has a prev field
31
Deleting a node from a Doubly Linked List Delete(A) – Delete node A from its list a i−1 … a i+1 Each node now has a prev field aiai A Note: A itself not changed! Is that good?
32
Inserting a node into a Doubly Linked List Insert-After(A,B) – Insert node B after node A a i−1 aiai A b B
33
Implementing lists using circular doubly linked lists The list is now circular L first A sentinel is added … a3a3 a2a2 a1a1 a n-1 No special treatment of first and last elements Each node has a successor and a predecessor Every node can be removed in O(1) time
34
Empty list L Implementing lists using circular doubly linked lists
35
L … a3a3 a2a2 a1a1 a n-1 Queue and Deque operations in O(1) time
36
Implementing lists using circular doubly linked lists L … a3a3 a2a2 a1a1 a n-1 Exercise: Implement Insert(L,i,B)
37
Pitfalls of the new ADT L1L1 … a3a3 a2a2 a1a1 a n-1 L2L2 … b3b3 b2b2 b1b1 b m-1 A B Insert-After(A,B) L 2 is not a valid list now Should call Delete(B) before Insert-After(A,B)
38
Implementing Lists with indices 38
39
Implementing lists using (circular) arrays with resizing 39
40
Implementing lists using (circular) arrays with resizing L array maxlen length a0a0 a1a1 … a M−1 M M M M What do we do when the array is full? We cannot just extend the array. We must allocate a larger array and copy What should be the size of the new array?
41
Implementing lists using (circular) arrays with resizing L array maxlen length a0a0 a1a1 … a M−1 M M M If we start with an empty array and increase its length by 1, then the time to do n Insert-Last operations is about: M
42
Implementing lists using (circular) arrays with doubling When the array is full, double its size and copy What is the cost of n Insert-Last operations? Assume, for simplicity that n=2 k cost of insertion cost of copying The average/amortized cost of each operation is O(1)
43
Worst-case bounds Suppose that a data structure supports k different types of operations T 1, T 2,…,T k Let worst(T i ), the maximal time that a single operation of type T i may take. Let op 1, op 2,… op n be a sequence of operations that includes n i operations of type T i. Sometimes, this bound is very loose.
44
Amortized bounds We say that amort(T i ) is an amortized bound on the cost of operation of type T i iff for every valid sequence of operations op 1, op 2,… op n that includes n i operations of type T i we have Note: Amortized bounds are bounds, they are not unique
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.