410 likes | 577 Views
Algorithms. Part III. Data Structures Ch. 10: Elementary Data Structures Ming-Te Chi. 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)
E N D
Algorithms Part III. Data Structures Ch. 10: Elementary Data Structures Ming-Te Chi Ch10 Elementary DS
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
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
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
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
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
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
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
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
CH. 10. ELEMENTARY DATA STRUCTURES Ch10 Elementary DS
10.1 Stacks and Queues • Stacks and Queues • 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
An array implementation of a stack S Ch10 Elementary DS
empty, underflows, overflows STACK_EMPTY(S ) 1 if S.top == 0 2 return TRUE 3 else return FALSE Ch10 Elementary DS
PUSH(S,x) 1 S.top = S.top + 1 2 S[S.top] = x POP(S ) 1 ifSTACK-EMPTY(S ) 2 then error“underflow” 3 elseS.top = S.top -1 4 returnS[S.top + 1] Ch10 Elementary DS
An array implementation of a queue Q Ch10 Elementary DS
ENQUEUE(Q, x) 1 Q[Q.tail] = x 2 ifQ.tail == Q.length 3 Q.tail = 1 4 elseQ.tail = Q.tail +1 Anything wrong? Ch10 Elementary DS
DEQUEUE(Q ) 1 x= Q[Q.head] 2 if Q.head == Q.length 3 Q.head = 1 4 elseQ.head = Q.head +1 5 returnx Ch10 Elementary DS
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
10.2 Linked lists List-Insert(L, x), x.key=25 List-Delete(L, x), x.key=4 Ch10 Elementary DS
LIST_SEARCH(L,k) 1 x= L.head 2 whilex ≠ NIL and x.key≠ k 3 x= x.next 4 returnx O(n) time in the worst case Ch10 Elementary DS
LIST_INSERT(L,x) 1 x.next= L.head 2 ifL.head≠ NIL 3 L.head.prev =x 4 L.head =x 5 x.prev= NIL O(1) Ch10 Elementary DS
LIST_DELETE(L,x) 1 ifx.prev≠ NIL 2 next[prev[x]] = x.next 3 elseL.head= x.next 4 ifx.next≠ NIL 5 x.next.prev= x.prev • (Call LIST_SEARCH first O(n)) O(1) or O(n) Ch10 Elementary DS
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
LIST_DELETE’(L,x) 1 x.prev.next=x.next 2 x.next.prev= x.prev Ch10 Elementary DS
LIST_SEARCH’(L,k) 1 x= L.nil.next 2 whilex≠ L.nil and x.key ≠ k 3 x= x.next 4 returnx Ch10 Elementary DS
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
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
11.3 Implementing pointers and objects • A multiple-array representation of objects Ch10 Elementary DS
A single array representation of objects Ch10 Elementary DS
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
Allocating and freeing objects--garbage collector Ch10 Elementary DS
Allocate_object( ),LIST_INSERT(L,4),Key(4)=25 Ch10 Elementary DS
LIST_DELETE(L,5), FREE_OBJECT(5) Ch10 Elementary DS
Two link lists Ch10 Elementary DS
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
Binary trees Ch10 Elementary DS
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
Rooted tree with unbounded branching Ch10 Elementary DS
Other tree representations • Heap • Others Ch10 Elementary DS