1 / 165

Stacks Queues Introduction to Trees

Lecture 11. Stacks Queues Introduction to Trees. Stacks. An Everyday Example. Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’ item I gave you on hold; this is more important, so rush it out first.”

ruthchang
Download Presentation

Stacks Queues Introduction to Trees

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. Lecture 11 StacksQueuesIntroduction to Trees

  2. Stacks

  3. An Everyday Example • Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’ item I gave you on hold; this is more important, so rush it out first.” We’ll end up doing the last item first(last in, first out).

  4. LB In general... • A stack can keep track of, “Where was I?” • Activation Stack • Compilers if if if endif endif if • Cafeterias use stacks: Plates, trays...

  5. The Stack Push Pop

  6. Properties Idea: a “Last In, First Out” (LIFO) data structure Behaviors: • Push: Add to top of stack • Pop: Remove from top of stack (and return that top value) • Top: Return topmost item (but leave it on the stack) • Is_Full: is it full? • Is_Empty: is it empty? • Initialize: empty stack

  7. The Stack as a Logical Data Structure • The stack is an idea • It implies a set of logical behaviors • It can be implemented various ways • Using a linked list or a tree or an array • In this example, we’ll focus on dynamic implementations using dynamic data...

  8. Stacks:Dynamic Implementation A linked list with restricted set of operations to change its state: only modified from one end top 4 17 42

  9. Defining the Node Type This is the simple data structure we will use in the following example. Node definesa record data isoftype Num next isoftype Ptr toa Node endrecord // Node

  10. Complex Node Definition Recall that the same code (with some small modifications) will work with more complex data structures such as is shown here: Student_Rec definesa Record Name isoftype String SSN isoftype String GPA isoftype Num endrecord // Student_Rec Node definesa Record data isoftype Student_Rec next isoftype Ptr toa Node endrecord // Node

  11. Application Programmer Interface procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) // Purpose: push one value onto stack // Pre: top points to NIL-terminated list // Post: the list has one node added procedure Pop (value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) // Pop a value off stack; if empty, result // contains FALSE (value will be undefined) // Pre: top points to a NIL-terminated list // Post: list has one fewer, value is old data

  12. Push • Create new node • Add it to the front top 17 42

  13. Push • Create new node • Add it to the front temp top 42 4 17 ?

  14. Push • Create new node • Add it to the front temp top 42 4 17

  15. Push • Create new node • Add it to the front top 4 42 17

  16. Push Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) // Push one value onto stack temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure // Push

  17. Pop • Capture the first value (to return) • Remove the first node (move top to next) top 4 42 17

  18. Pop • Capture the first value (to return) • Remove the first node (move top to next) top 4 42 value = 4 17

  19. Pop • Capture the first value (to return) • Remove the first node (move top to next) top 4 42 value = 4 17

  20. Pop • Capture the first value (to return) • Remove the first node (move top to next) top 42 17 value (4) is returned

  21. Pop procedure Pop (value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) // Pop an element off the stack if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure // Pop

  22. Student’s Choice? Do Trace Skip Trace

  23. Algorithm Fragment . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) .

  24. top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) .

  25. top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . OK = N =

  26. top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . OK = N =

  27. top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . OK = N =

  28. top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  29. top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  30. top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  31. 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  32. 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  33. 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  34. 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  35. 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . OK = N =

  36. 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  37. 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  38. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  39. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  40. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  41. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- temp endprocedure temp = OK = N =

  42. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . OK = N =

  43. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = result = OK = N =

  44. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = result = OK = N =

  45. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = result = T OK = N =

  46. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = 2 result = T OK = N =

  47. 2 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = 2 result = T OK = N =

  48. 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endif endprocedure value = 2 result = T OK = N =

  49. 42 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . OK = T N = 2

  50. 42 2 top . top isoftype Ptr toa Node OK isoftype Boolean N isoftype Num top <- NIL Push(42, top) Push(2, top) Pop(N, top, OK) if(OK) then print(N) endif Push(7, top) Pop(N, top, OK) Pop(N, top, OK) . OK = T N = 2

More Related