1 / 35

Elementary Data Structures

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

zariel
Download Presentation

Elementary Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Elementary Data Structures Jeff Chastine

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. Example head[L] / 9 16 4 1 / Jeff Chastine

  11. 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

  12. 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

  13. Example head[L] / 9 16 4 1 / head[L] / 25 9 16 4 1 / Jeff Chastine

  14. 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

  15. Example head[L] / 9 16 4 1 / head[L] / 25 9 16 4 1 / head[L] / 25 9 16 1 / Jeff Chastine

  16. Sentinel Nodes • Used to simplify checking of boundary conditions • Here is a circular, doubly-linked list 9 16 4 1 nil[L] Jeff Chastine

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. Inserting into a BST 50 56 30 71 27 43 88 Jeff Chastine

  27. Inserting into a BST 50 30 71 56 27 43 88 Jeff Chastine

  28. Inserting into a BST 50 74 30 71 27 43 56 88 Jeff Chastine

  29. Inserting into a BST 50 30 71 74 27 43 56 88 Jeff Chastine

  30. Inserting into a BST 50 30 71 27 43 56 88 74 Jeff Chastine

  31. Inserting into a BST 50 30 71 27 43 56 88 74 Jeff Chastine

  32. 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

  33. 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

  34. 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

  35. 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

More Related