350 likes | 498 Views
Elementary Data Structures. Stacks and Queues. The element removed is prespecified A stack implements last-in, first-out (LIFO) A queue implements first-in, first-out (FIFO) What about FOFI, or LILO?. Stacks. Implemented using an array Use P USH and P OP operations
E N D
Elementary Data Structures Jeff Chastine
Stacks and Queues • The element removed is prespecified • A stack implements last-in, first-out (LIFO) • A queue implements first-in, first-out (FIFO) • What about FOFI, or LILO? Jeff Chastine
Stacks • Implemented using an array • Use PUSH and POP operations • Has attribute top[S] • Contains elements S[1..top[S]] • When top[S] = 0, the stack is empty • If top[S] > n, then we have an overflow Jeff Chastine
7 1 2 3 4 5 6 15 6 2 9 top[S] = 4 7 1 2 3 4 5 6 15 6 2 9 17 3 top[S] = 4 7 1 2 3 4 5 6 15 6 2 9 17 3 top[S] = 4 Jeff Chastine
STACK-EMPTY (S) 1 iftop[S] = 0 2 then return TRUE 3 else return FALSE PUSH (S, x) 1 top[S] top[S] + 1 2 S[top[S]] x POP (S) 1 if STACK-EMPTY (S) 2 then error “underflow” 3 else top[S] top[S] – 1 4 returnS[top[S]+1] Note: All operations performed in O(1) Jeff Chastine
Queues • Supports ENQUEUE and DEQUEUE operations • Has a head and tail • New elements are placed at the tail • Dequeued element is always at head • Locations “wrap around” • When head[Q] = tail[Q], Q is empty • When head[Q] = tail[Q] + 1, Q is full Jeff Chastine
1 2 3 4 5 6 7 8 9 10 11 12 15 6 9 8 4 head[Q] = 7 tail[Q] = 7 1 2 3 4 5 6 7 8 9 10 11 12 3 5 15 6 9 8 4 17 tail[Q] = 7 head[Q] = 7 1 2 3 4 5 6 7 8 9 10 11 12 3 5 15 6 9 8 4 17 tail[Q] = 7 head[Q] = 7 Jeff Chastine
ENQUEUE (Q, x) 1 Q[tail[Q]] x 2 iftail[Q] = length[Q] 3 thentail[Q] 1 4 elsetail[Q] tail[Q] + 1 DEQUEUE (Q) 1 x Q[head[Q]] 2 ifhead[Q] = length[Q] 3 thenhead[Q] 1 4 else head[Q] head[Q] + 1 5 returnx Jeff Chastine
Linked Lists • A data structure where the objects are arranged linearly according to pointers • Each node has a pointer to the next node • Can be a doubly linked list • Each node has a next and previous pointer • Could also be circularly linked • If prev[x] = NIL, we call that node the head • If next[x] = NIL, we call that node the tail • The list may or may not be sorted! Jeff Chastine
Example head[L] / 9 16 4 1 / Jeff Chastine
Searching a Linked List LIST-SEARCH (L, k) 1 x head[L] 2 whilex NIL and key[x] k 3 dox next[x] 4 returnx Worst case, this takes (n) Jeff Chastine
Inserting into a Linked List LIST-INSERT (L, x) 1 next[x] head[L] 2 ifhead[L] NIL 3 thenprev[head[L]] x 4 head[L] x 5 prev[x] NIL Note: runs in O(1) Jeff Chastine
Example head[L] / 9 16 4 1 / head[L] / 25 9 16 4 1 / Jeff Chastine
Deleting from a Linked List LIST-DELETE (L, x) 1 ifprev[x] NIL 2 thennext[prev[x]] next[x] 3 elsehead[L] next[x] 4 ifnext[x] NIL 5 thenprev[next[x]] prev[x] Jeff Chastine
Example head[L] / 9 16 4 1 / head[L] / 25 9 16 4 1 / head[L] / 25 9 16 1 / Jeff Chastine
Sentinel Nodes • Used to simplify checking of boundary conditions • Here is a circular, doubly-linked list 9 16 4 1 nil[L] Jeff Chastine
Binary Search TreesBSTs • Very important data structure • Can support many dynamic-set operations: • SEARCH • MINIMUM/MAXIMUM • PREDECESSOR/SUCCESSOR • INSERT/DELETE • Operations take time proportional to the height of the tree, often O(lg n) Jeff Chastine
Binary Search Trees • Each node has a key, as well as left, right and parent pointers • Thus, the root has a parent = NIL • Binary search tree property: • Let x be a node. If y is a node in the left subtree, key[y] key[x]. If y is a node in the right subtree, key[y] > key[x]. Jeff Chastine
5 3 7 2 5 8 INORDER-TREE-WALK (x) 1 ifx NIL 2 then INORDER-TREE-WALK(left[x]) 3 print key[x] 4 INORDER-TREE-WALK(right[x]) In-Order: 2, 3, 5, 5, 7, 8 Pre-Order: 5, 3, 2, 5, 7, 8 Post-Order: 2, 5, 3, 8, 7, 5 Jeff Chastine
Proof: In-Order Walk Takes (n) • Let c be the time for the test x NIL, and d be the time to print • Suppose tree T has kleft nodes and n-k right nodes. • Using substitution, we assume T(n)=(c+d)n+c T(n) = T(k) + T(n-k-1) + d = ((c+d)k+c) + ((c+d)(n-k-1)+c) + d = (c+d)n + c – (c+d) + c + d = (c+d)n + c Jeff Chastine
Querying a Binary Search Tree • Tree should support the following: • SEARCH – determine if an element exists in T • MINIMUM – return the smallest element in T • MAXIMUM – return the largest element in T • SUCCESSOR – given a key, return the next largest element, if any • PREDECESSOR – given a key, return the next smallest element, if any Jeff Chastine
TREE-SEARCH (x, k) 1 ifx = NIL or k = key[x] 2 then returnx 3 ifk < key[x] 4 then return TREE-SEARCH(left[x], k) 5 else return TREE-SEARCH(right[x], k) Note: run time is O(h), where h is the height of the tree Jeff Chastine
Tree Minimum/Maximum TREE-MINIMUM(x) 1 whileleft[x] NIL 2 dox left[x] 3 returnx TREE-MAXIMUM (x) 1 whileright[x] NIL 2 dox right[x] 3 returnx Note: binary search tree property guarantees this is correct Jeff Chastine
Successor/Predecessor • Successor is the node with the smallest key greater than key[x]. • Not necessary to compare keys! • Two cases: • If right subtree is non-empty, find its minimum • If right subtree is empty, find lowest ancestor of x whose left child is also an ancestor of x Jeff Chastine
TREE-SUCCESSOR (x) 1 ifright[x] NIL 2 then return TREE-MINIMUM(right[x]) 3 y p[x] 4 whiley NIL and x = right[y] 5 dox y 6 y p[y] 7 returny Note: simply go up the tree until we encounter a node that is the left child. Runs in O(h) Jeff Chastine
Inserting into a BST 50 56 30 71 27 43 88 Jeff Chastine
Inserting into a BST 50 30 71 56 27 43 88 Jeff Chastine
Inserting into a BST 50 74 30 71 27 43 56 88 Jeff Chastine
Inserting into a BST 50 30 71 74 27 43 56 88 Jeff Chastine
Inserting into a BST 50 30 71 27 43 56 88 74 Jeff Chastine
Inserting into a BST 50 30 71 27 43 56 88 74 Jeff Chastine
Deleting • Three cases, given node z: • z has no children. Delete it, and update the parent. • If z has only one child, splice it out by making a link between its child and parent. • If z has two children, splice out z’s successor y and replace z with y Jeff Chastine
15 15 5 16 5 16 3 12 20 3 12 20 10 13 18 23 10 18 23 6 6 7 7 Case 1: z has no children Jeff Chastine
15 15 20 5 16 5 3 12 20 3 12 18 23 10 13 18 23 10 13 6 6 7 7 Case 2: z has one child Jeff Chastine
z 15 15 20 5 16 6 3 12 20 3 12 18 23 10 13 18 23 10 13 6 7 y 7 Case 3: z has two children Jeff Chastine