Download presentation
Presentation is loading. Please wait.
1
Algorithms Part III. Data Structures
Ch. 10: Elementary Data Structures Ming-Te Chi Ch10 Elementary DS
2
Data Structures Introduction Ch 10. Elementary Data Structures
Ch 11. Hash Tables (will not cover) Ch 12. Binary Search Trees Ch 13. Red-Black Trees (will cover briefly) Ch 14. Augmenting Data Structures Ch10 Elementary DS
3
Introduction Sets manipulated by algorithm can grow, shrink, or change over time. Called dynamic set. Types of operations to be performed on sets: Insert, delete, test-membership (search?), etc. Extract the smallest element, etc. Ch10 Elementary DS
4
Introduction (continue)
Operations to be performed on dynamic sets: queries: return information about the set. Search, minimum, maximum Successor, predecessor. modifying operations: change the set. Insert, delete Data structures that can support any of these operations on a set of size n: O (log n) Ch10 Elementary DS
5
Overview of Chapters (10-14)
Ch 10. Elementary Data Structures Simple data structures such as: Queues, stacks, linked lists, and rooted trees How objects and pointers can be implemented in programming environments that do not support them as primitives. Very elementary. Ch10 Elementary DS
6
Overview of Chapters (10-14)
Ch 11. Hash Tables Introduce hash tables, which support the operations INSERT, DELETE, and SEARCH. Worst-case: O(n) for SEARCH operation. Expected time for hash-table operations: O(1). Analysis relies on probability, but most of the chapter requires no background on this subject. Ch10 Elementary DS
7
Overview of Chapters (10-14)
Ch 12. Binary Search Trees Support all the dynamic-set operations. Worst-case: O(n). Expected time: O(log n). Serve as the basis for many other data structures. Ch10 Elementary DS
8
Overview of Chapters (10-14)
Ch 13. Red-Black Trees A variant of binary search trees. Balanced search tree. Worst-case: O(log n). Ch10 Elementary DS
9
Overview of Chapters (10-14)
Ch 14. Augmenting Data Structures How to augment red-back trees to support operations other than the basic ones. Ch10 Elementary DS
10
CH. 10. ELEMENTARY DATA STRUCTURES
Ch10 Elementary DS
11
10.1 Stacks and Queues Stacks and Queues Stack Queue dynamic set
elements removed from the set by the DELETE operation is pre-specified Stack LIFO policy: Last-In First-Out Delete the element most recently inserted Push (insert), pop (delete) Queue FIFO policy: First-In First-Out Enqueue (insert), dequeue (delete) Ch10 Elementary DS
12
An array implementation of a stack S
Ch10 Elementary DS
13
empty, underflows, overflows STACK_EMPTY(S ) 1 if S.top == 0
2 return TRUE 3 else return FALSE Ch10 Elementary DS
14
2 then error “underflow” 3 else S.top = S.top -1 4 return S[S.top + 1]
PUSH(S,x) 1 S.top = S.top + 1 2 S[S.top] = x POP(S ) 1 if STACK-EMPTY(S ) 2 then error “underflow” 3 else S.top = S.top -1 4 return S[S.top + 1] Ch10 Elementary DS
15
An array implementation of a queue Q
Ch10 Elementary DS
16
ENQUEUE(Q, x) 1 Q[Q.tail] = x 2 if Q.tail == Q.length 3 Q.tail = 1
4 else Q.tail = Q.tail +1 Anything wrong? Ch10 Elementary DS
17
DEQUEUE(Q ) 1 x = Q[Q.head] 2 if Q.head == Q.length 3 Q.head = 1
4 else Q.head = Q.head +1 5 return x Ch10 Elementary DS
18
10.2 Linked lists Data structure in which the objects are arranged in a linear order. The order in a linked list is determined by a pointer in each object. The order in an array is determined by the array indices. Singly linked list, doubly linked list, circular list. Head and tail. Ch10 Elementary DS
19
10.2 Linked lists List-Insert(L, x), x.key=25
List-Delete(L, x), x.key=4 Ch10 Elementary DS
20
O(n) time in the worst case
LIST_SEARCH(L,k) 1 x = L.head 2 while x ≠ NIL and x.key ≠ k 3 x = x.next 4 return x O(n) time in the worst case Ch10 Elementary DS
21
LIST_INSERT(L,x) 1 x.next = L.head 2 if L.head ≠ NIL 3 L.head.prev = x
4 L.head = x 5 x.prev = NIL O(1) Ch10 Elementary DS
22
LIST_DELETE(L,x) O(1) or O(n) 1 if x.prev ≠ NIL
2 next[prev[x]] = x.next 3 else L.head = x.next 4 if x.next ≠ NIL 5 x.next.prev = x.prev (Call LIST_SEARCH first O(n)) O(1) or O(n) Ch10 Elementary DS
23
A Sentinel is a dummy object that allows us to simplify boundary conditions,
List-Insert’(L, x), x.key=25 List-Delete’(L, x), x.key=4 Ch10 Elementary DS
24
LIST_DELETE’(L,x) 1 x.prev.next = x.next 2 x.next.prev = x.prev
Ch10 Elementary DS
25
2 while x ≠ L.nil and x.key ≠ k 3 x = x.next 4 return x
LIST_SEARCH’(L,k) 1 x = L.nil.next 2 while x ≠ L.nil and x.key ≠ k 3 x = x.next 4 return x Ch10 Elementary DS
26
LIST_INSERT’(L,x) 1 x.next = L.nil.next 2 L.nil.next.prev = x
3 L.nil.next = x 4 x.prev = L.nil Ch10 Elementary DS
27
Remarks on sentinels Rarely reduce the asymptotic time bounds of data structure operations. Can only reduce constant factors. Can improve the clarity of code rather than speed. The extra storage used by the sentinels, for small lists, can represent significant wasted memory. Use sentinels only when they truly simplify the code. Ch10 Elementary DS
28
11.3 Implementing pointers and objects
A multiple-array representation of objects Ch10 Elementary DS
29
A single array representation of objects
Ch10 Elementary DS
30
Allocating and freeing objects:
Garbage collector Determining which objects are unused. Free list Singly linked list that keeps the free objects Uses only the “next” pointer. Stack. Ch10 Elementary DS
31
Allocating and freeing objects--garbage collector
Ch10 Elementary DS
32
Allocate_object( ),LIST_INSERT(L,4),Key(4)=25
Ch10 Elementary DS
33
LIST_DELETE(L,5), FREE_OBJECT(5)
Ch10 Elementary DS
34
Ch10 Elementary DS
35
Two link lists Ch10 Elementary DS
36
10.4 Representing rooted trees
Binary trees: parent, left(-child), right(-child) p[x] = NIL x is the root x has no left child left[x] = NIL x has no right child right[x] = NIL Ch10 Elementary DS
37
Binary trees Ch10 Elementary DS
38
10.4 Representing rooted trees
Rooted trees with unbounded branching: parent, left-child, right-sibling p[x] = NIL x is the root left-child[x]: points to the leftmost child of x. right-sibling[x]: points to the sibling of x immediately to the right. x has no children left-child[x] = NIL x is the rightmost child of its parent right-sibling[x] = NIL Ch10 Elementary DS
39
Rooted tree with unbounded branching
Ch10 Elementary DS
40
Other tree representations
Heap Others Ch10 Elementary DS
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.