abstract data types
play

Abstract Data Types EECS 214, Fall 2017 What is an ADT? An ADT - PowerPoint PPT Presentation

Abstract Data Types EECS 214, Fall 2017 What is an ADT? An ADT defjnes: An ADT omits: How the values are concretely represented How the operations work 2 A set of (abstract) values A set of (abstract) operations on those values What


  1. Abstract Data Types EECS 214, Fall 2017

  2. What is an ADT? An ADT defjnes: An ADT omits: How the values are concretely represented How the operations work 2 • A set of (abstract) values • A set of (abstract) operations on those values

  3. What is an ADT? An ADT defjnes: An ADT omits: 2 • A set of (abstract) values • A set of (abstract) operations on those values • How the values are concretely represented • How the operations work

  4. ADT: Stack Signature: push (Stack, Element): Void pop (Stack): Element isEmpty (Stack): Bool 3 Looks like: |3 4 5 �

  5. ADT: Stack Signature: 3 Looks like: |3 4 5 � • push (Stack, Element): Void • pop (Stack): Element • isEmpty (Stack): Bool

  6. ADT: Queue (FIFO) Signature: enqueue (Queue, Element): Void dequeue (Queue): Element isEmpty (Queue): Bool 4 Looks like: � 3 4 5 �

  7. ADT: Queue (FIFO) Signature: 4 Looks like: � 3 4 5 � • enqueue (Queue, Element): Void • dequeue (Queue): Element • isEmpty (Queue): Bool

  8. Stack versus Queue Stack signature: Queue signature: 5 • push (Stack, Element): Void • pop (Stack): Element • isEmpty (Stack): Bool • enqueue (Queue, Element): Void • dequeue (Queue): Element • isEmpty (Queue): Bool

  9. Adding laws means that if precondition p is true when we apply f to x then we will get y as a result, and postcondition q will be true afterward. Examples: a a a a a a 6 { p } f ( x ) ⇒ y { q }

  10. Adding laws means that if precondition p is true when we apply f to x then we will get y as a result, and postcondition q will be true afterward. Examples: 6 { p } f ( x ) ⇒ y { q } { a = [2 , 4 , 6 , 8] } a [2] ⇒ 6 { a = [2 , 4 , 6 , 8] } { a = [2 , 4 , 6 , 8] } a [2] = 0 { a = [2 , 4 , 0 , 8] }

  11. ADT: Stack Signature: Laws: 7 Looks like: |3 4 5 � • push (Stack, Element): Void • pop (Stack): Element • isEmpty (Stack): Bool isEmpty ( |� ) ⇒ ⊤ isEmpty ( | e 1 . . . e k e k +1 � ) ⇒ ⊥ { s = | e 1 . . . e k �} push ( s , e ) { s = | e 1 . . . e k e �} { s = | e 1 . . . e k e k +1 �} pop ( s ) ⇒ e k +1 { s = | e 1 . . . e k �}

  12. ADT: Queue (FIFO) Signature: Laws: 8 Looks like: � 3 4 5 � • enqueue (Queue, Element): Void • dequeue (Queue): Element • isEmpty (Queue): Bool isEmpty ( �� ) ⇒ ⊤ isEmpty ( � e 1 . . . e k e k +1 � ) ⇒ ⊥ { q = � e 1 . . . e k �} enqueue ( q , e ) { q = � e 1 . . . e k e �} { q = � e 1 e 2 . . . e k �} dequeue ( q ) ⇒ e 1 { q = � e 2 . . . e k �}

  13. Stack implementation: linked list let s = new_stack() pop(s) pop(s) push(s, 5) push(s, 4) push(s, 3) push(s, 2) next data 2 data 5 next data 4 next data 3 head next 9

  14. Stack implementation: linked list let s = new_stack() pop(s) pop(s) push(s, 5) push(s, 4) push(s, 3) push(s, 2) next data 2 data 5 next data 4 next data 3 head next 9

  15. Stack implementation: linked list let s = new_stack() pop(s) pop(s) push(s, 5) push(s, 4) push(s, 3) push(s, 2) next data 2 data 5 next data 4 next data 3 head next 9

  16. Stack implementation: linked list let s = new_stack() pop(s) pop(s) push(s, 5) push(s, 4) push(s, 3) push(s, 2) next data 2 data 5 next data 4 next data 3 head next 9

  17. Stack implementation: linked list let s = new_stack() pop(s) pop(s) push(s, 5) push(s, 4) push(s, 3) push(s, 2) next data 2 data 5 next data 4 next data 3 head next 9

  18. Stack implementation: linked list let s = new_stack() pop(s) pop(s) push(s, 5) push(s, 4) push(s, 3) push(s, 2) next data 2 data 5 next data 4 next data 3 head next 9

  19. Stack implementation: linked list let s = new_stack() pop(s) pop(s) push(s, 5) push(s, 4) push(s, 3) push(s, 2) next data 2 data 5 next data 4 next data 3 head next 9

  20. Stack implementation: array data len 0 let s = new_stack() 2 push(s, 2) 2 3 push(s, 3) 2 3 4 push(s, 4) 2 3 4 5 push(s, 5) push(s, 6) 10

  21. Stack implementation: array data len 1 let s = new_stack() 2 push(s, 2) 2 3 push(s, 3) 2 3 4 push(s, 4) 2 3 4 5 push(s, 5) push(s, 6) 10

  22. Stack implementation: array data len 2 let s = new_stack() 2 push(s, 2) 2 3 push(s, 3) 2 3 4 push(s, 4) 2 3 4 5 push(s, 5) push(s, 6) 10

  23. Stack implementation: array data len 3 let s = new_stack() 2 push(s, 2) 2 3 push(s, 3) 2 3 4 push(s, 4) 2 3 4 5 push(s, 5) push(s, 6) 10

  24. Stack implementation: array data len 4 let s = new_stack() 2 push(s, 2) 2 3 push(s, 3) 2 3 4 push(s, 4) 2 3 4 5 push(s, 5) push(s, 6) 10

  25. Stack implementation: array data len 4 let s = new_stack() 2 push(s, 2) 2 3 push(s, 3) 2 3 4 push(s, 4) 2 3 4 5 push(s, 5) push(s, 6) 10

  26. ADT: Stack Signature: Laws: 11 Looks like: |3 4 5 � • push (Stack, Element): Void — O (1) • pop (Stack): Element — O (1) • isEmpty (Stack): Bool — O (1) isEmpty ( |� ) ⇒ ⊤ isEmpty ( | e 1 . . . e k e k +1 � ) ⇒ ⊥ { s = | e 1 . . . e k �} push ( s , e ) { s = | e 1 . . . e k e �} { s = | e 1 . . . e k e k +1 �} pop ( s ) ⇒ e k +1 { s = | e 1 . . . e k �}

  27. Trade-ofgs: linked list stack versus array stack whereas array stack has a fjxed size (or must reallocate) no (or rare) allocation 12 • Linked list stack only fjlls up when memory fjlls up, • Array stack has better constant factors: cache locality and • Array stack space usage is tighter; linked list is smoother

  28. ADT: Queue (FIFO) Signature: Laws: 13 Looks like: � 3 4 5 � • enqueue (Queue, Element): Void — O (1) • dequeue (Queue): Element — O (1) • isEmpty (Queue): Bool — O (1) isEmpty ( �� ) ⇒ ⊤ isEmpty ( � e 1 . . . e k e k +1 � ) ⇒ ⊥ { q = � e 1 . . . e k �} enqueue ( q , e ) { q = � e 1 . . . e k e �} { q = � e 1 e 2 . . . e k �} dequeue ( q ) ⇒ e 1 { q = � e 2 . . . e k �}

  29. Queue implementation: linked list? let q = new_queue() n ? dequeue(q) — enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) next head data 5 next data 4 next data 3 next data 2 14

  30. Queue implementation: linked list? let q = new_queue() n ? dequeue(q) — enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) next head data 5 next data 4 next data 3 next data 2 14

  31. Queue implementation: linked list? let q = new_queue() n ? dequeue(q) — enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) next head data 5 next data 4 next data 3 next data 2 14

  32. Queue implementation: linked list? let q = new_queue() n ? dequeue(q) — enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) next head data 5 next data 4 next data 3 next data 2 14

  33. Queue implementation: linked list? let q = new_queue() n ? dequeue(q) — enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) next head data 5 next data 4 next data 3 next data 2 14

  34. Queue implementation: linked list? let q = new_queue() n ? — dequeue(q) enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) next head data 5 next data 4 next data 3 next data 2 14

  35. Queue implementation: linked list? next enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) let q = new_queue() data 5 head next data 4 next data 3 next data 2 14 dequeue(q) — O ( n ) ?

  36. Queue implementation: array? data len 0 let q = new_queue() 2 enqueue(q, 2) 2 3 enqueue(q, 3) 2 3 4 enqueue(q, 4) 2 3 4 5 enqueue(q, 5) 3 4 5 s.dequeue() — n ??? 15

  37. Queue implementation: array? data len 1 let q = new_queue() 2 enqueue(q, 2) 2 3 enqueue(q, 3) 2 3 4 enqueue(q, 4) 2 3 4 5 enqueue(q, 5) 3 4 5 s.dequeue() — n ??? 15

  38. Queue implementation: array? data len 2 let q = new_queue() 2 enqueue(q, 2) 2 3 enqueue(q, 3) 2 3 4 enqueue(q, 4) 2 3 4 5 enqueue(q, 5) 3 4 5 s.dequeue() — n ??? 15

  39. Queue implementation: array? data len 3 let q = new_queue() 2 enqueue(q, 2) 2 3 enqueue(q, 3) 2 3 4 enqueue(q, 4) 2 3 4 5 enqueue(q, 5) 3 4 5 s.dequeue() — n ??? 15

  40. Queue implementation: array? data len 4 let q = new_queue() 2 enqueue(q, 2) 2 3 enqueue(q, 3) 2 3 4 enqueue(q, 4) 2 3 4 5 enqueue(q, 5) 3 4 5 s.dequeue() — n ??? 15

  41. Queue implementation: array? enqueue(q, 4) n ??? — s.dequeue() 3 4 5 enqueue(q, 5) 2 3 4 5 2 3 4 data enqueue(q, 3) 2 3 enqueue(q, 2) 2 let q = new_queue() len 4 15

  42. Queue implementation: array? data len 4 let q = new_queue() 2 enqueue(q, 2) 2 3 enqueue(q, 3) 2 3 4 enqueue(q, 4) 2 3 4 5 enqueue(q, 5) 3 4 5 15 s.dequeue() — O ( n ) ???

  43. Queue impl.: linked list with tail pointer next dequeue(q) dequeue(q) enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) let q = new_queue() data 2 head next data 3 next data 4 next data 5 tail 16

  44. Queue impl.: linked list with tail pointer next dequeue(q) dequeue(q) enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) let q = new_queue() data 2 head next data 3 next data 4 next data 5 tail 16

  45. Queue impl.: linked list with tail pointer next dequeue(q) dequeue(q) enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) let q = new_queue() data 2 head next data 3 next data 4 next data 5 tail 16

  46. Queue impl.: linked list with tail pointer next dequeue(q) dequeue(q) enqueue(q, 5) enqueue(q, 4) enqueue(q, 3) enqueue(q, 2) let q = new_queue() data 2 head next data 3 next data 4 next data 5 tail 16

Recommend


More recommend