Infix to reverse polish using a stack

Поделиться
HTML-код
  • Опубликовано: 19 май 2013
  • Converting from infix to reverse polish (postfix) notation using a stack

Комментарии • 89

  • @computerkeen8210
    @computerkeen8210 5 лет назад +35

    you made a 3 hour lecture in only 9 minutes, thats talent

  • @Wintotv
    @Wintotv 7 лет назад +35

    love your english accent too xD i can't understand those Indian tutorials

  • @wakaboomnick
    @wakaboomnick 10 лет назад +24

    Thank you, made it a lot easier than my teacher

  • @eltrasimaco
    @eltrasimaco 9 лет назад +12

    Ive seen several tutorials here but this one is crystal clear

    • @HurrayBanana
      @HurrayBanana  9 лет назад +2

      thanks, that's the idea

    • @T010RD
      @T010RD 6 лет назад

      It's much clearer than virgin's teardrop

  • @Tr4vy
    @Tr4vy 10 лет назад +19

    thanks for the "fack"ing help hehehe

  • @tej_3423
    @tej_3423 5 лет назад +3

    Thanks a lot! I'm taking part in CIE exams computer science tomorrow... you make it more easier

  • @josephburke7924
    @josephburke7924 8 лет назад +1

    every year, when I have to teach RPN, I always end up watching this video. Cheers mate great vid :)

    • @HurrayBanana
      @HurrayBanana  8 лет назад

      +Joseph Burke
      No problems, pity it's come out of the new specifcations. Favourite topic

  • @JP-su9ib
    @JP-su9ib 3 года назад

    you"re such a charismatic, well explaining teatcher.

  • @MrFacePeck
    @MrFacePeck 4 года назад +1

    Finally an understandable explanation of this. Thanks so much.

  • @MustafaOzanAlpay
    @MustafaOzanAlpay 6 лет назад

    that video made everything crystal clear, and loved the accent hehe ^^ cheers mate!

  • @HurrayBanana
    @HurrayBanana  9 лет назад +2

    @kiran, your thinking is correct, because they are the same precedence, need to remove from stack (doesn't really matter with + on + or - on -) but is for + on - or - on +

  • @HawksVideoNest
    @HawksVideoNest 7 лет назад +5

    Well thank you :D Definitely saved me from failing an exam completely

  • @serhii_rudakov
    @serhii_rudakov Год назад

    amazing, Thank you for this great explanation

  • @artsyishita8551
    @artsyishita8551 6 лет назад

    Ohh thank you now i understood this concept....really helpful for my exams...thanks a lot...

  • @Wintotv
    @Wintotv 7 лет назад

    this just saved my life, thanks you so much Mr Banana =)

    • @HurrayBanana
      @HurrayBanana  7 лет назад

      No problem, my favourite topic of all time :)

  • @rapramos5687
    @rapramos5687 7 лет назад +1

    wow thank you so much! helped a lot!

  • @BarnabaRudge
    @BarnabaRudge 9 лет назад +1

    For the good of humanity, such lectures should records and presents.

  • @Wyattap125
    @Wyattap125 8 лет назад

    holy crap this was so helpful

  • @OghmaNano
    @OghmaNano Год назад +1

    Really nice expiation, just implemented it in my semiconductor device model in C.

  • @kombuchamp
    @kombuchamp 4 года назад +1

    Cheers, helped a lot to figure out an algorithm

  • @CaptainClueless01
    @CaptainClueless01 8 лет назад +1

    Well now I can do reverse polish. Top notch!

    • @HurrayBanana
      @HurrayBanana  8 лет назад +2

      +CaptainClueless Everyone should be able to do it, it's cool

  • @thebarnold7234
    @thebarnold7234 2 года назад +3

    I wish this was longer. Love the way you actually taught this.

    • @HurrayBanana
      @HurrayBanana  2 года назад +3

      Thanks, it's such a cool thing reverse polish

  • @einarstensson4323
    @einarstensson4323 8 лет назад

    Thank you! Interesting.

  • @_Rohan007_
    @_Rohan007_ 2 года назад

    Thank you so much you explained it really well Sir

  • @sheilasubbiah5969
    @sheilasubbiah5969 7 лет назад +2

    I had the same question as the student in the video about the left bracket which goes on the stack, but I think I understand now; basically the left bracket 'has a precedence' which other operators compare their own precedence against, but the left bracket ignores its own precedence (i.e. never compares its own precedence against any of the operators (even though it could)) and just goes to sit on top of the stack.
    Thanks for your great example, with an assignment and other tricky bits! It's really helped me.

    • @HurrayBanana
      @HurrayBanana  7 лет назад +1

      glad it helped

    • @HurrayBanana
      @HurrayBanana  7 лет назад +1

      Yeah, the left bracket sits on the stack so further operators can override whatever precedence was already on top.

  • @appleraika
    @appleraika 9 лет назад

    awesome!! thanks :)

  • @SnakeOilDev
    @SnakeOilDev 6 лет назад

    purfect !

  • @jakubputynkowski8554
    @jakubputynkowski8554 8 лет назад

    great explaination :) thank you!

    • @HurrayBanana
      @HurrayBanana  8 лет назад

      No problem, happy it was of some use

  • @James-se1rq
    @James-se1rq 2 года назад +1

    Lindy approved video!

  • @michaelholder5975
    @michaelholder5975 9 лет назад

    What precedence would factorial "!" have. Does it go above exponents?

  • @IDONTEATPANTS
    @IDONTEATPANTS 7 лет назад

    Please be my professor. Also the FACK part made me lol

  • @laureven
    @laureven 4 года назад +1

    is always better to invest time for finding a good tutorial :) ...and Yours is Awesome :) ...search is over :)

  • @krzysztofwelc146
    @krzysztofwelc146 5 лет назад

    God bless You

  • @kidrahulable
    @kidrahulable 7 лет назад

    Great job! Thank you very much for help! ;)
    No I understand it :D

  • @jim93m
    @jim93m 10 лет назад +2

    As easier as it gets :) thnx

  • @kiranrambha
    @kiranrambha 9 лет назад +1

    plus cannot be placed on plus
    minus cannot be placed on minus
    right?????

  • @saifmohammed1481
    @saifmohammed1481 3 года назад

    "Facking" awesome !!

  • @samuelhor9700
    @samuelhor9700 9 лет назад

    Any idea what order of precedence the trig functions sin, cos and tan fall under?

    • @michaelholder5975
      @michaelholder5975 9 лет назад

      Trigonometric functions, or functions in general, go above exponents.
      For example: 2 + 3^4 * cos(0)
      Steps:
      1. 2 is an operand; Add 2 to the output expression
      2. + is an operator; Stack = empty; push + onto the stack
      3. 3 is an operand; Add 3 to the output expression
      4. ^ is an operator; Top of stack has lower precedence; push ^ onto stack
      5. 4 is an operand; Add 4 to the output expression
      6. * is an operator; Top of stack has higher precedence; pop stack until lower precedence is found; push * onto stack
      7. cos is treated as an operator; top of stack has lower precedence; push cos onto stack
      8. "(" = push onto stack regardless of lower or higher precedence or stack is empty
      9. 0 is an operand; Add 0 to output expression
      10. ")" = Pop and add to output expression until "(" = top of stack; pop top of stack
      11. Since ")" is the end of the expression, pop the stack until stack = empty
      Result: 2 3 4 ^ 0 cos * +

  • @oguzkaraca1434
    @oguzkaraca1434 6 лет назад

    did he just give an example over "fack up"?

  • @user-pw3pu8wu3f
    @user-pw3pu8wu3f 5 лет назад

    But how do you deal with expressions with the unary operator like:
    -A + B?

    • @HurrayBanana
      @HurrayBanana  5 лет назад +1

      Higher priority than multiply and divide. Only Pop one item from stack like store does

  • @SuperJatinrajput
    @SuperJatinrajput 8 лет назад

    great video sir, make up for bunk classes.

    • @HurrayBanana
      @HurrayBanana  8 лет назад

      +Jatin Rajput To be fair nothing makes up for bunk'd classes, it's amazing the nuances and added extra's you'll get when someone goes through a topic with you. I always find that I talk about things I never planned.

  • @s1nister688
    @s1nister688 4 года назад

    What happens when there is a modulus sign present?

    • @HurrayBanana
      @HurrayBanana  4 года назад

      Depends what the precedence for modulus is in that language

  • @henryl3e
    @henryl3e 9 лет назад

    would like to know what if there is a negative sign meet with a negative sign ? or any other sign meeting the same ones

    • @henryl3e
      @henryl3e 9 лет назад

      for example pq-rs-/

    • @HurrayBanana
      @HurrayBanana  9 лет назад +3

      WooHoooP
      Don't understand your example that would come from the infix
      (p - q) / (r - s)
      But to answer your question:
      if you have an expression of this form
      a - - b
      the minus in front of the b is actually unary minus (negation, applied to one operand)
      we would re-write that expression like this
      a - ~ b (using tilda to represent the unary minus)
      this would generate as unary minus is higher precedence that + or minus.
      a b ~ -
      which would negate the b before attempting to subtract it from a, which is logical in this example
      The precedence of unary minus is open to debate in some environments its placed higher than exponent (raising to the power ^) but others may place it below this but above multiply and divide.
      ~ b ^ a
      if unary minus was higher we would get
      b ~ a ^
      which would negate b then raise to the power of a, but some would argue that we should have exponent higher than unary minus meaning this
      b a ^ ~
      which would raise the b to the power of a and negate the result

  • @XXxlightmarex
    @XXxlightmarex 7 лет назад +1

    What do i do when i have multiple open brackets e.g. cos((a + b*c)/(d + e*(f-g^h))) ?

    • @HurrayBanana
      @HurrayBanana  7 лет назад

      XXxlightmarex just keep applying the rules. every left bracket goes on top of stack. when you get a right bracket remove contents of stack untill you find a left bracket, then discard that snd continue to parse the expression

    • @HurrayBanana
      @HurrayBanana  7 лет назад

      work through remember operands just get written down as rpn (don't involve the stack at all)
      ((a + b * c) / (d + e * (f - g ^ h)))
      reaching first right bracket stack contains
      *
      +
      (
      (
      remove up to topmost left bracket and discard giving:
      abc*+
      stack only contains
      (
      carrying on until we reach 2nd right bracket, stack should contain:
      ^
      -
      (
      *
      +
      (
      /
      (
      dump stack upto topmost left bracket and discard giving us this much of the rpn:
      abc*+defgh^-
      stack now contains
      *
      +
      (
      /
      (
      parse 3rd right bracket so remove stack contents again upto topmost left bracket giving rpn of:
      abc*+defgh^-*+
      stack contains
      /
      (
      process final right bracket by removing upto topmost left bracket giving rpn of:
      abc*+defgh^-*+/
      stack now empty
      hope this helps

    • @XXxlightmarex
      @XXxlightmarex 7 лет назад

      Thank you very much, you've been a great help

  • @LucasAlfare
    @LucasAlfare 4 года назад

    How to implement unaries using this?

    • @HurrayBanana
      @HurrayBanana  4 года назад

      Higher precedence than multiply and divide but lower than exponent

  • @Shotpl0xGaming
    @Shotpl0xGaming 6 лет назад

    What happens whens 2 symbols hold the same line of precedence?

    • @HurrayBanana
      @HurrayBanana  6 лет назад +1

      Another burgermate you can only place an operator on top if it is higher so remove the item on top of stack snd write it down in the rp expression, then see if the new operator can now go on the stack (like normal)

    • @Shotpl0xGaming
      @Shotpl0xGaming 6 лет назад

      I see, thank you very much for this. Helped me out heaps. Was just doing past papers, came across this scenario again.

  • @marcinpozniak1605
    @marcinpozniak1605 2 года назад

    6:32 why is the left bracket of higher precedence than the power of?

    • @HurrayBanana
      @HurrayBanana  2 года назад

      Left brackets are always going to override precedence, it goes on top of everything, it's also there as a marker for when we encounter a right bracket while parsing, so we know when to stop pulling operators off the stack

    • @marcinpozniak1605
      @marcinpozniak1605 2 года назад

      @@HurrayBanana okay, the drawing in the top right corner misled me. thank you

    • @HurrayBanana
      @HurrayBanana  2 года назад

      The diagram shows that all operators go on top of left bracket if it seen on top of the stack. Parsing a left bracket in the expression doesn't look at precedence as you automatically push it onto the stack

  • @bryanarp19
    @bryanarp19 2 года назад

    Saludos a la BUAP xd

  • @syntaxerorr
    @syntaxerorr 3 года назад

    I can't take it that the K is really an R

  • @mdy405
    @mdy405 9 лет назад

    why did you pop "*" before you push "/" if they are the same priority??? i dont really understand this!!

    • @HurrayBanana
      @HurrayBanana  9 лет назад +4

      You can only push onto the stack if the precedence is higher, if it is the same you must pop the stack until the operator is higher than the contents of the top

    • @mdy405
      @mdy405 9 лет назад

      HurrayBanana Thank you !

  • @JoffreyB
    @JoffreyB 6 лет назад

    somebody add please subtitles

  • @saranshdhyani3164
    @saranshdhyani3164 6 лет назад

    Where is code?

    • @HurrayBanana
      @HurrayBanana  6 лет назад +1

      saransh dhyani this process described here is reasonably easy to code up if you parse the expression first to tokenise each component

  • @messi-ih4uy
    @messi-ih4uy 5 лет назад

    keke do you love me