Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE...

23
Sequence Represented As Two Stacks 8 February 2019 OSU CSE 1

Transcript of Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE...

Page 1: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Sequence Represented As Two Stacks

8 February 2019 OSU CSE 1

Page 2: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Client View

• A situation as seen by a client of the kernel class Sequence3, based on its interface:

8 February 2019 OSU CSE 2

<4, 5, 6, 7>

Page 3: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Implementer View

• The same situation as seen by the kernel class implementer, where the data representation is two Stacks called leftand right:

8 February 2019 OSU CSE 3

< >

<4, 5, 6, 7>

Page 4: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Implementer View

• The same situation as seen by the kernel class implementer, where the data representation is two Stacks called leftand right:

8 February 2019 OSU CSE 4

< >

<4, 5, 6, 7>

But this shows just one possible data

representation for the same Sequence value!

Page 5: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Implementer View

• The same situation as seen by the kernel class implementer, where the data representation is two Stacks called leftand right:

8 February 2019 OSU CSE 5

<4>

<5, 6, 7>

Page 6: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Implementer View

• The same situation as seen by the kernel class implementer, where the data representation is two Stacks called leftand right:

8 February 2019 OSU CSE 6

<5, 4>

<6, 7>

Page 7: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Implementer View

• The same situation as seen by the kernel class implementer, where the data representation is two Stacks called leftand right:

8 February 2019 OSU CSE 7

<5, 4>

<6, 7>

What? Yes, left has 5 on the top of the Stack, not 4. That is, these entries of the Sequence are in reverse order from those in the

Stack called left!

Page 8: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Implementer View

• The same situation as seen by the kernel class implementer, where the data representation is two Stacks called leftand right:

8 February 2019 OSU CSE 8

<6, 5, 4>

<7>

Page 9: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Implementer View

• The same situation as seen by the kernel class implementer, where the data representation is two Stacks called leftand right:

8 February 2019 OSU CSE 9

<7, 6, 5, 4>

< >

Page 10: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Picture: Sequence3 on Stack

8 February 2019 OSU CSE 10

What the client sees: a Sequence whose value is

<4, 5, 6, 7>.<4, 5, 6, 7>

Page 11: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Picture: Sequence3 on Stack

8 February 2019 OSU CSE 11

<4, 5, 6, 7>

<6, 5, 4>

<7>

What the implementer might see: two Stacks

whose values are<6, 5, 4> and <7>.

Page 12: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

A Better Picture

8 February 2019 OSU CSE 12

(< >, <4, 5, 6, 7>)

<4, 5, 6, 7>

(<4>, <5, 6, 7>)

(<5, 4>, <6, 7>)

(<7, 6, 5, 4>, < >)

(<6, 5, 4>, <7>)

Page 13: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

A Better Picture

8 February 2019 OSU CSE 13

(< >, <4, 5, 6, 7>)

<4, 5, 6, 7>

(<4>, <5, 6, 7>)

(<5, 4>, <6, 7>)

(<7, 6, 5, 4>, < >)

(<6, 5, 4>, <7>)

Client view:a Sequence.

Page 14: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

A Better Picture

8 February 2019 OSU CSE 14

(< >, <4, 5, 6, 7>)

<4, 5, 6, 7>

(<4>, <5, 6, 7>)

(<5, 4>, <6, 7>)

(<7, 6, 5, 4>, < >)

(<6, 5, 4>, <7>)

Implementer view:two Stacks

(several possibilities).

Page 15: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

A Better Picture

8 February 2019 OSU CSE 15

(< >, <4, 5, 6, 7>)

<4, 5, 6, 7>

(<4>, <5, 6, 7>)

(<5, 4>, <6, 7>)

(<7, 6, 5, 4>, < >)

(<6, 5, 4>, <7>)

Notation: each data representation is an

ordered pair of strings.

Page 16: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

The Interpretation

• To get the Sequence that is represented by the two Stacks, do the following:– Start with the reverse of left, e.g.:

left = <5, 4>

rev(left) = <4, 5>

8 February 2019 OSU CSE 16

Page 17: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

The Interpretation

• To get the Sequence that is represented by the two Stacks, do the following:– Start with the reverse of left, e.g.:

left = <5, 4>

rev(left) = <4, 5>

8 February 2019 OSU CSE 17

Remember, a Sequenceis modeled as a string.

Page 18: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

The Interpretation

• To get the Sequence that is represented by the two Stacks, do the following:– Start with the reverse of left, e.g.:

left = <5, 4>

rev(left) = <4, 5>

8 February 2019 OSU CSE 18

Remember, a Stack is modeled as a string.

Page 19: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

The Interpretation

• To get the Sequence that is represented by the two Stacks, do the following:– Start with the reverse of left, e.g.:

left = <5, 4>

rev(left) = <4, 5>

– Concatenate right to it (on the right), e.g.:rev(left) * right= <4, 5> * <6, 7>

= <4, 5, 6, 7>

8 February 2019 OSU CSE 19

Page 20: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

The Interpretation

• To get the Sequence that is represented by the two Stacks, do the following:– Start with the reverse of left, e.g.:

left = <5, 4>

rev(left) = <4, 5>

– Concatenate right to it (on the right), e.g.:rev(left) * right= <4, 5> * <6, 7>

= <4, 5, 6, 7>

8 February 2019 OSU CSE 20

This is the Sequencevalue represented by the two Stacks left and

right.

Page 21: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Why Reverse Anything?

• To simplify the code for Sequence3, and for performance reasons, it is helpful to have the tops of the two Stacks “facing each other” in the middle of the Sequence

• Since it is up to us as implementers how to interpret the two Stacks as a Sequence, this is how we choose to do it!

8 February 2019 OSU CSE 21

Page 22: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Why Reverse Anything?

• To simplify the code for Sequence3, and for performance reasons, it is helpful to have the tops of the two Stacks “facing each other” in the middle of the Sequence

• Since it is up to us as implementers how to interpret the two Stacks as a Sequence, this is how we choose to do it!

8 February 2019 OSU CSE 22

A critical point to understand: there is no

code in Sequence3 that reverses a Stack!

Page 23: Sequence Represented As Two Stacksweb.cse.ohio-state.edu/software/2231/web-sw2/... · Title: CSE 2231 - Sequence On Two Stacks Author: bucci Created Date: 2/8/2019 7:41:42 AM ...

Why Reverse Anything?

• To simplify the code for Sequence3, and for performance reasons, it is helpful to have the tops of the two Stacks “facing each other” in the middle of the Sequence

• Since it is up to us as implementers how to interpret the two Stacks as a Sequence, this is how we choose to do it!

8 February 2019 OSU CSE 23

The reversal is simply part of the

interpretation of the data representation—a mental computation!