Reverse Polish Grows on Trees - Computerphile

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 พ.ค. 2014
  • Why use Reverse Polish Notation? How does it relate to trees in Computer Science? Professor Brailsford explains how RPN arises naturally, as a linearized form of a tree.
    For further research, the Prof suggests you seek out material on the topics of "Postorder Tree Traversal" and "Dijkstra's Shunting Yard"
    Correction: In the graphic at 06:30, on the illustration of the second tree, the A is incorrectly labelled as a C.
    Reverse Polish Notation & the Stack: • Reverse Polish Notatio...
    The Dawn of Desktop Publishing: • The Dawn of Desktop Pu...
    Upside Down Trees: • How Huffman Trees Work...
    Domino Addition - Numberphile: • Domino Addition - Numb...
    / computerphile
    / computer_phile
    This video was filmed and edited by Sean Riley.
    Computer Science at the University of Nottingham: bit.ly/nottscomputer
    Computerphile is a sister project to Brady Haran's Numberphile. See the full list of Brady's video projects at: bit.ly/bradychannels

ความคิดเห็น • 138

  • @kyledaoust221
    @kyledaoust221 10 ปีที่แล้ว +1

    I dont know dick about computers but I could listen to this guy talk all day

  • @xanokothe
    @xanokothe 10 ปีที่แล้ว +102

    You are amazing Professor. You are the Morgan Freeman of Computerphile

  • @mattilindstrom
    @mattilindstrom 10 ปีที่แล้ว +23

    In some sense RPN "is a way of making you do all the hard work". In another, it is a way of taking out the hard work of converting one's thought proceses into the infix convention.
    My first real experience with RPN came with my first university year and the HP-28C (I had had some passing encounters with the HP-41C and the 10C). To my surprise, instead of being a chore, the RPN method was for me a perfectly natural way of expressing my intent in a calculation. Many of my (also physicist) friends tell the same story: for a slightly scatter-brained way of numerical entry, they would not trade RPN for anything.
    Feel free to drop something botched. Just SWAP or 1/x (after the fact) an incorrect division entry order. Transform the stack as you please. Chunk the equation into digestible bits.
    RPN is perfect for my way of thinking in numerical calculations, but as always, tastes do differ.

    • @AureliusR
      @AureliusR 3 ปีที่แล้ว

      However, even the HP folks admitted that algebraic entry (as TI was doing) was much easier and simpler. Hence why RPN fell out of use. It actually does take more up-front training. You end up using your tool in a way which contradicts how all written math is done. I like the concept of RPN and I like HP calcs, but it's important to recognize that it's *not* inherently superior. It forces you to re-arrange the equation in your head, instead of just typing it in as you would write it down.

    • @christopherpepin6059
      @christopherpepin6059 11 หลายเดือนก่อน +2

      I feel like one thing that is being undersold is that infix calculators, in an attempt to take away the hard work, seriously end up gimping the user by robbing them access to the stack. Meanwhile postfix and prefix calculators are pretty much forced to give you access to a stack and it greatly expands the capabilities of the calculator.
      A great example being looking at the quadratic formula, which is often one of the first programs people are taught to put in their calculators. When you solve it you don't just get one solution, you usually get two. A RPN calculator handles that perfectly. It just pushes both values to the stack. Both values become readily available for you or your programs to use in an incredibly predictable manor.
      Meanwhile an infix calculator isn't really set up to store temporary values. The best most of them will do is store one of the solutions in an ANS variable. If you want both answers though you are just out of luck without resorting to global variables. So on infix calculators you are stuck writing programs that have to stand pretty much completly alone. Creating actual functions that can feed each other was only supported on the most expensive of the TI calculators. Even then the functionality of those functions was limited as you couldn't return complex data structures, something trivial to do on an RPN calculator.

    • @not_herobrine3752
      @not_herobrine3752 6 หลายเดือนก่อน

      ive first found out that RPN exists after discovering forth, and found the idea of manipulating a stack of numbers quite interesting

  • @lordofduct
    @lordofduct 10 ปีที่แล้ว +3

    I remember the first time I wrote my own mathematical script interpreter. Parsing strings into trees and traversing them into what was basically postfix to be operated in a simulated stack was the approach I took. I thought myself quite the genius for inventing such an awesome way of approaching the problem!
    Not but a few hours later... my ego popped.
    I will say though, it's moments like those that make me enjoy math and computer science. Even the first time you're explained it, and how elegant many processes are, I find it beautiful. It's why I continue on in this career.

  • @gulllars4620
    @gulllars4620 10 ปีที่แล้ว +20

    This professor is a joy listening to :)

  • @ThePeaceableKingdom
    @ThePeaceableKingdom 10 ปีที่แล้ว +17

    The animation at 7:00 confused me for just a moment, because the professor said "keep looking to my left" but the animated man keeps looking straight ahead facing 1st the right and then the left (from our point of view).
    I then realized we're looking at the tree from above and the man from the side. If we were seeing the man from above as well, he would always be looking at the tree on his left while moving forward as he circumferences it counter clockwise.
    I also realized that would be a lot harder to draw... :)

  • @TheJaguar1983
    @TheJaguar1983 6 ปีที่แล้ว +1

    Prof. Brailsford is my favourite Computerphile presenter.

  • @nihlovaccus9652
    @nihlovaccus9652 10 ปีที่แล้ว

    Fantastic video. You've demonstrated the relationship between postfix notation, trees and stacks very elegantly.

  • @kevins8260
    @kevins8260 8 ปีที่แล้ว +1

    You sir, are very engaging. I love the way you explain everything. I'm a junior CS student at an Oregon University and I'd listen to your lectures any day over my current professors!

  • @jdferreira
    @jdferreira 10 ปีที่แล้ว

    I really like this professor. He is so passionate about his ideas and has a very happy mood when explaining stuff! He reminds me of my grandfather, a really smart guy who loved to teach me things! Even though I already knew everything he taught us so far, I really enjoy seeing these videos, and I get thrilled when a new Computerphile video is with Prof. Brailsford! :)

  • @TheAlexagius
    @TheAlexagius 10 ปีที่แล้ว +3

    Explains it far better than my computer science teacher ever did....

  • @insect212
    @insect212 10 ปีที่แล้ว

    This is fanatic. I was trying to learn about this and failing, but then I decided to go on computerphile for fun and it explained it perfectly.

  • @NickiRusin
    @NickiRusin 10 ปีที่แล้ว

    A couple days ago I finished a project that involved parsing a string with a function in it to then get the function value for a whole bunch of x values. Literally did what's described in this video - converted it into reverse polish, then created a binary tree with all the operations. Works like a dream.

  • @thoperSought
    @thoperSought 10 ปีที่แล้ว +4

    0:46 I've never thought of RPN as hard, or something that needs converting to. infix calculators always put a much higher burden on my memory. with RPN, the only thing that gets me *sometimes,* when I haven't been using it for a while, is what's being divided into what.
    I feel like, when I look at the stack on an rpn calculator-and maybe this is the key: I never had one that didn't display at least some of the stack-I know what's going to happen next. but, with infix calculators, I was never sure if I hit '+' already, or what the last number I put in was, etc.

  • @MaxElkin
    @MaxElkin 10 ปีที่แล้ว +23

    8:35 ahhh...

  • @derejehailemariam677
    @derejehailemariam677 5 ปีที่แล้ว

    A natural teacher. Rare and amazing teaching skill.

  • @cra0kalo
    @cra0kalo 10 ปีที่แล้ว

    Great video looking forward to more by Professor Brailsford

  • @eltyo340
    @eltyo340 10 ปีที่แล้ว

    Brailsford videos are the best

  • @llewkcornosaj
    @llewkcornosaj 10 ปีที่แล้ว

    You have an amazingly soothing voice. Pure butter

  • @penfold1992
    @penfold1992 10 ปีที่แล้ว +1

    there is a problem with the 27/9/3 issue....
    27/9/3 as said in the video has two answers however what is really being asked is:
    27/1 * 1/9 * 1/3
    multiply the top's and bottoms you get 27 * 1 * 1 = 27
    1 * 3 * 9 = 27
    therefore 27/27 = 1
    so to get around that issue, just use the inverse operator

  • @Andrei5656
    @Andrei5656 10 ปีที่แล้ว

    Such a great professor. Thank you.

  • @mikelipsey8837
    @mikelipsey8837 10 ปีที่แล้ว +1

    Wish I had this gentleman when I took Comp. Sys. many years ago...

  • @robl4836
    @robl4836 10 ปีที่แล้ว

    A nice straightforward explanation. I'd just say that I find Polish already difficult, without reversing it ;)

  • @pascalcoole2725
    @pascalcoole2725 4 ปีที่แล้ว

    Thanks David, although i still have to fiddle with it your talk helps me quiet a lot further.

  • @herbertwyndham
    @herbertwyndham 10 ปีที่แล้ว

    This is great! I remember coding a postfix calculator as an exercise in school

  • @macvii2113
    @macvii2113 7 ปีที่แล้ว

    Good Job Professor. Respect !!!

  • @ButzPunk
    @ButzPunk 10 ปีที่แล้ว +1

    I thought reverse Polish notation was just something interesting which I'd never really use, but it turns out, it came in real handy because I've been writing in LaTeX and had to make some modifications to my style .bst to format my references the way the uni likes them. Thanks to your earlier lessons on it, I was able to understand how to write the little bits of code I needed and now I'm kinda in love with it... such a beautiful, logical way to write code.

  • @xanokothe
    @xanokothe 10 ปีที่แล้ว +2

    The Postscript is really simple. As Professor said "The machine let you do all the hard work" which is "assembly" the Postscript or the tree. Will have a video about the algorithm for it?

  • @Burak-pl1jl
    @Burak-pl1jl 6 ปีที่แล้ว

    Before this video, I was wondering how I would remember all this stuff, but after seeing your cheat everything has changed. Thank you a lot sir! Best cheat ever seen. Haha

  • @Tyranisaur
    @Tyranisaur 10 ปีที่แล้ว

    How to traverse the tree: draw a line (mentally) around the tree and to get the different orders, do this:
    Postfix(RPN): write the thing when you are to the right of it.
    Infix: write ( when you are to the left of a thing, write the thing when you are under it and write ) when you are to the right of it.
    Prefix: write the thing when you are to the left of it.

  • @jwilliams8210
    @jwilliams8210 7 ปีที่แล้ว

    Fascinating!

  • @Playncooler
    @Playncooler 7 ปีที่แล้ว +3

    Professor, can you explain the Shunting Yard algorithm?

  • @divanvanzyl7545
    @divanvanzyl7545 10 ปีที่แล้ว

    Professor Brailsford is awesome

  • @ReinaldoRauch
    @ReinaldoRauch 10 ปีที่แล้ว

    Amazing vid! Keep this quality o/

  • @tzkelley
    @tzkelley 10 ปีที่แล้ว +2

    These videos remind me of how much I've forgotten.

  • @PeterWalkerHP16c
    @PeterWalkerHP16c 8 ปีที่แล้ว +1

    ...added to my list of great lecturers. (In no particular order)
    David Attenborough
    Dr Jim Al Khalili
    Dr Iain Stewart
    Dr Aubrey Manning
    David Malone
    Dr Michael Mosley
    Dr Alice Roberts
    Dr Brian Cox
    Dr David Brailsford

  • @karlkastor
    @karlkastor 10 ปีที่แล้ว

    Very interesting video! Maybe I'll try programming a calculator like this using recursions like the tree he's drawn.

  • @KarnKaul
    @KarnKaul 10 ปีที่แล้ว

    Mindblowing

  • @iome87
    @iome87 10 ปีที่แล้ว +1

    wow, this could help me a lot when i had to write a BASIC interpreter...

  • @OneAndOnlyMe
    @OneAndOnlyMe 3 ปีที่แล้ว

    I always used to be amazed that the low processing power of the original HP LaserJet was able to parse and interpret something as high level as PostScript. Makes sense now.

  • @rdubb77
    @rdubb77 10 หลายเดือนก่อน

    He’s doing a Postorder Depth First traversal of the tree, you’re segregating the operands from the operators by doing the leaf nodes their root nodes.

  • @yeticusrex1661
    @yeticusrex1661 10 ปีที่แล้ว

    Excellent video....my only complaint is that they didn't show the HP15C....which really is not a complaint....just a personal nitpick....still have my 15C that I use almost daily for the last 32 years.

  • @amydebuitleir
    @amydebuitleir 10 ปีที่แล้ว

    While a good explanation of RPN per se, I think the video made *using* an RPN calculator sound harder than it actually is. My way of thinking about it is that you do the calculation in the same order you would do it with a pencil and paper. Figure out the next operation you need to do, make sure the operands are on the stack, and do the operation. So to calculate a+b*c, I would enter bc*a+, not abc*+. The result is the same, but I don't have to worry about stack overflow because I never have more operands on the stack than I need for the next operation.

  • @megamef
    @megamef 10 ปีที่แล้ว

    This guy is great

  • @michelfug
    @michelfug 10 ปีที่แล้ว

    Thx. Nice video. At 6:31 I spotted a little mistake in de diagram: the a and b operands are mislabeled.

  • @sanjaybhatikar
    @sanjaybhatikar 4 ปีที่แล้ว

    So what do loops (like for loop or while loop) look like on the stack and can RPN describe loops?

  • @Herdatec
    @Herdatec 10 ปีที่แล้ว +41

    8:35 did I spend to much time on the internet when I immediately recognize the shape?

  • @PINGPONGROCKSBRAH
    @PINGPONGROCKSBRAH 10 ปีที่แล้ว

    I'm sorry professor, but I'm way too hungover this morning to comprehend this lecture.

  • @JohnDobyns
    @JohnDobyns 10 ปีที่แล้ว +1

    Where do you guys get line printer paper? I haven't seen it in decades ;)

  • @FerroNeoBoron
    @FerroNeoBoron 10 ปีที่แล้ว +2

    If you have an old infix calculator you might be able to try this.Tell your calculator to calculate 2^3^4. I had an infix calculator that assumed that all operators were left-associated and it gave me 8^4=4096. But on a newer calculator it gave me the correct right-associative answer of 2^81=~2*10^24. It might be contrived but it's an interesting error.

    • @ib9rt
      @ib9rt 10 ปีที่แล้ว

      But I want 2^3^4 to be treated as (2^3)^4 and give me 4096, because that is the most common scenario by far when doing computations. For instance I would rather hope that e^(ln 10)^3 gives me 1000 and not 200400.18. If I had a calculator that gave the latter answer I might be inclined to reprogram it with a hammer...

    • @FerroNeoBoron
      @FerroNeoBoron 10 ปีที่แล้ว

      ib9rt
      Actually, you probably don't. If you read a typeset paper on mathematics and it has a 2 with a small 3 above it to the right and an even smaller 4 above that and to the right (or some other operand) it translates to a right-associative operation when written out without typesetting since (2^3)^4 = 2^(3*4). It's also a natural extention into tetration since it doesn't require parentheses.
      I'll admit that it's rather contrived and likely to screw up anyone not in the know which is why I nerd sniped some of my friends with it. I imagine almost all new calculators will probably use the right-associative version from now on though.
      I always wonder if the reason languages like C and Java don't have an operator for exponentiation on integral types is because of this. I find the potential that there's someone out there who implements a library that overloads the bitwise xor operator as exponentiation and a poor sap that uses that operator twice in a row without understanding the associativity of the operators in the mathematical paper they probably got the algorithm from and the ones in the language to be pretty funny.

  • @jacklewis6392
    @jacklewis6392 10 ปีที่แล้ว +1

    I wrote a browser based RPN calculator in CoffeeScript (with jquery). It's a lot of fun.

  • @General1Cal
    @General1Cal 10 ปีที่แล้ว

    You know I wondered and pondered this idea since I learned the trick in c++ for 14 years! it was so simple, so int =+1, I always knew it was reverse something just did not know why. so I would explicitly code it so I would not forget, such int a int b, int c=b+a.

    • @jannegrey593
      @jannegrey593 6 ปีที่แล้ว

      You learned polish when you were 14?? Congrats, it's a difficult language! :D

  • @vortyx090
    @vortyx090 8 ปีที่แล้ว

    thanks man!

  • @SigmundSkjelnes
    @SigmundSkjelnes 10 ปีที่แล้ว

    Most calculators got lost when people loan it, and forget to return it. Rpn calculators have the great advantage that most people can't use it there and then, and leave it.

  • @ImKingLouie
    @ImKingLouie 10 ปีที่แล้ว

    I like this. More algorithms and data structures and less sensationalism.

  • @seanodonnell2228
    @seanodonnell2228 8 ปีที่แล้ว +1

    Thanks.

  • @Merlin5007
    @Merlin5007 10 ปีที่แล้ว +1

    First to comment. Now i better watch it! Love this channel.

  • @yousorooo
    @yousorooo 10 ปีที่แล้ว

    So what's the algorithm for "walking counterclockwise"?

  • @deandrereichelle831
    @deandrereichelle831 2 ปีที่แล้ว

    Funny how this idea of the computer outsourcing the work to the human is alive and well today, in the form of Captcha

  • @booklibrary2884
    @booklibrary2884 2 ปีที่แล้ว

    do modern computers (CPU's, calculators, etc) still use this way of computing arithmetic when code is compiled to machine code?

  • @booklibrary2884
    @booklibrary2884 2 ปีที่แล้ว

    I think there is a mistake or something that should be cleared (at 6:31):
    (a+b)*c in reverse polish is: cab+x, and following the tree shape, it should be something like this:
    .......+
    ..X b
    c a

  • @fabien2430
    @fabien2430 10 ปีที่แล้ว

    Hi, anticlockwise method ok for computer, RPN users probably never use it, indeed it's much easier to type b c * a + instead of a b c * +.
    You start with inner operations (b*c) and then add a, it's nothing more than what human do when they don't have calculator.

  • @harrisonharris6988
    @harrisonharris6988 8 ปีที่แล้ว

    how does the compiler handle multiply divides in that case?
    If you gave it (in RPN): a b c / /
    You would want it to do left-associative division and so you'd want it to do a/b then divide that result by c, but if you think about how it would work with the stack b and c would be at the top and so it would not be able to access the a underneath.
    One way of possibly doing it would be as follows:
    change the notation to be: a b 1 c / / / (put a 1 before the last divider operand and add another division)
    the divide as normal and add to the stack, for example:
    27 9 1 3 / / / (stack: [])
    fill in stack = [27,9,1,3] (leftmost on bottom)
    first divide, divide 2nd to top by top
    so divide 1/3 = 1/3rd
    stack = [27, 9, 1/3]
    second divide, divide 2nd to top by top
    so divide 9/0.33333... = 27
    stack = [27,27]
    third divide, divide 2nd to top by top
    so divide 27/27 = 1
    stack = [1]
    equation solved correctly.

    • @EvgenyPakhomov
      @EvgenyPakhomov 8 ปีที่แล้ว +4

      +Harrison Harris
      For "(a / b) / c" you could just write in RPN "a b / c /"
      RPN doesn't mean that all of the operators must be at the end of an expression.

    • @blazerboy55
      @blazerboy55 8 ปีที่แล้ว +1

      +Harrison Harris There are also stack manipulation operations such as SWAP and ROT (swap and rotate).
      1 2 3 SWAP => 1 3 2
      1 2 3 ROT => 2 3 1
      So given "a b c" and trying to achieve "(a/b)/c" you can do "a b c ROT ROT / /" or just "a b / c/" like the other commenter said.

  • @edgeeffect
    @edgeeffect 8 ปีที่แล้ว +2

    I got into programming in FORTH just to do my mates heads in with RPN ;)

  • @SigurdKristvik
    @SigurdKristvik 10 ปีที่แล้ว

    Genius, I should learn to do this in the C programming language

  • @NNOTM
    @NNOTM 10 ปีที่แล้ว +5

    Walking around an actual tree doesn't really intuitively correspond to an algorithm one would actually use in practice, though, does it?
    I would just say
    function reversePolish (tree)
    if (isLeaf)
    then
    print operand
    else
    reversePolish (leftChild)
    reversePolish (rightChild)
    print operator
    end

    • @0pyrophosphate0
      @0pyrophosphate0 10 ปีที่แล้ว +2

      I think he definitely could have simplified that explanation. What he's doing is a depth-first search, which I think should have been mentioned. If you follow the path that he drew, it's much easier to say that you write down anything you find while moving back toward the top of the tree. Or, anything immediately to the left of your path while you draw it can be written down. There's no need to differentiate between the operators and operands.

    • @mcvoid1
      @mcvoid1 10 ปีที่แล้ว

      Sure. Walking a tree is actually simpler, though harder to see the bigger picture because it's recursive. It looks like this:
      visit(node):
      if isOperand(node):
      print(node)
      else:
      visit(node.right)
      visit(node.left)
      print(node)

    • @NNOTM
      @NNOTM 10 ปีที่แล้ว +2

      0pyrophosphate0
      Ah, yeah, that makes sense.
      Sean Wolcott Shouldn't you print the left node first and then the right node? Also, isn't that exactly what I wrote?

    • @mcvoid1
      @mcvoid1 10 ปีที่แล้ว +1

      NNOTM Yes, and yes. I didn't see you had more to say, and I confuse my left and right.

    • @TheElectromatter
      @TheElectromatter 10 ปีที่แล้ว

      NNOTM rewrite your algorithm as an iterative function and you will see that it is equivalent to walking around the tree.

  • @kingemocut
    @kingemocut 10 ปีที่แล้ว

    so, it's like when we use prime number trees..?

  • @arani5896
    @arani5896 หลายเดือนก่อน

    the goat

  • @ThePhatHalo
    @ThePhatHalo 5 ปีที่แล้ว +1

    I can't wait to try out the reverse polish with my gf!

  • @Markus9705
    @Markus9705 10 ปีที่แล้ว

    I loive this guy. :)

  • @AlexSimpsonsPage
    @AlexSimpsonsPage 10 ปีที่แล้ว

    Oops, the tree at 6:30 is wrong (has variables b, c, c instead of a, b, c)

  • @mcvoid1
    @mcvoid1 10 ปีที่แล้ว

    Hell yeah more CS.

  • @rjday753
    @rjday753 10 ปีที่แล้ว +10

    Animation dude he said 'looking to the left'

    • @DGCDWJ
      @DGCDWJ 10 ปีที่แล้ว

      In the animation dude's defense, the professor was being confusing. He kept saying "looking to the left", but at the same time he kept talking about a node in the tree when he passed by it, whether the node was to the left or not. If he truly only ever looked to the left, he would never see an operator before an operand, and wouldn't have to ask the question "can you write it down?".
      You can see that the professor messes up on the root node (the plus) when he asks if you can write it down when he's at the bottom of the node. From the bottom of the node while looking left, you can't see the node at all. So he shouldn't have mentioned it in the first place. When the root node finally is to the left of the walking dude, he will have passed all other nodes and the root will be the only one left to write down. So I can understand why the animation guy was confused.

    • @timotree3
      @timotree3 6 ปีที่แล้ว +2

      DGCDWJ the professor meant looking to 90 degrees anticlockwise to the direction you're currently walking.

    • @haralampiev2000
      @haralampiev2000 6 ปีที่แล้ว

      actually, the whole tree is turned 90 degrees to the left (or anticlockwise) in the professor`s paper - so he is absolutely right, but the animation is not so accurate!

    • @DreadKyller
      @DreadKyller 6 ปีที่แล้ว +1

      Not to YOUR left, to the left from the perspective of the character walking around the tree, imagine you're the character, and you're walking forward around the tree, then you look to the left. Not to the left on the paper, the left direction is dependent on what direction the character is moving.

    • @billl605
      @billl605 5 ปีที่แล้ว +1

      ahhh ty so much
      @@timotree3

  • @MegaMinerd
    @MegaMinerd 7 ปีที่แล้ว

    In the illustration starting at 7:28, you wouldn't see any of the operators prematurely if you were looking the other way.

    • @DreadKyller
      @DreadKyller 6 ปีที่แล้ว

      yeah, but you would see B before A and thus you'd end up with bac*+ which is definitely not what you want. If you're looking to the right you'd also have to then specify a limit of how far to the right can you actually see. since the example is huggint the tree to the characters left side, you only see the immediate node next to you, while for this to work with the right side would require range, and would mess up even more on more sophisticated trees.

  • @amigojapan
    @amigojapan 8 ปีที่แล้ว

    ok, and how do you generate the tree from an expression? I think this is the missing link i am missing//

    • @blazerboy55
      @blazerboy55 8 ปีที่แล้ว

      +amigojapan Basically, you start at the deepest level of your expression and chain those bits together.
      Consider the infix expression "(1+2)/(3+4)". You can see that before doing the division you need to do each addition, starting with the numerator, then divide those two results together (in the correct order, of course).
      postfix( 1 2 + ) = infix( 1 + 2 )
      postfix( 3 4 + ) = infix( 3 + 4 )
      postfix( a b / ) = infix( a / b )
      Chaining them together:
      postfix( 1 2 + 3 4 + /) = infix( ( 1 + 2 )/( 3 + 4 ) )
      Notice the number of parentheses used for each style. The RPN uses 0 parentheses, which means fewer keystrokes and faster input.

  • @AwesomeCrackDealer
    @AwesomeCrackDealer 10 ปีที่แล้ว

    This guy loves himself some Reverse Polish and STACKSSSSSSSS

  • @ConstantlyDamaged
    @ConstantlyDamaged 10 ปีที่แล้ว +4

    Discussing RPN in computing and not talking about FORTH?
    you "Please follow up with more, I love RPN" !

  • @Rizhiy13
    @Rizhiy13 10 ปีที่แล้ว +1

    Second tree is incorrect on the slideshow, c should be changed to a.

    • @Computerphile
      @Computerphile  10 ปีที่แล้ว +1

      Many thanks for spotting this, I will add an annotation - and apologies! >Sean

  • @kristover100
    @kristover100 10 ปีที่แล้ว +1

    8:26 is a bit rude.

  • @kolrabi
    @kolrabi 10 ปีที่แล้ว

    Of course, if you have a syntax tree like that you don't really need the rpn to evaluate the expression anymore. Just recursivly evaluate each node.

    • @BGBTech
      @BGBTech 10 ปีที่แล้ว

      that can be done, but a lot comes down to performance:
      directly interpreting the syntax-tree tends to make for a fairly slow interpreter (say, 1000x slower than native C);
      compiling to bytecode, it can be made a fair bit faster, more so if the bytecode already has all the type-information and variable locations worked out.
      if you then directly interpret the bytecode, often it may be instead closer to 100x (most of the time typically going into a "while/switch" loop or similar construction).
      otherwise, we can convert it to threaded-code (interpreting via a series of direct function calls), and get maybe 10x-30x of native speed;
      if converted into naive ASM / machine-code, and then run, it may be around 3x-5x of native C;
      throw a register allocator and an optimizer on it, and it may be 1x-2x.
      so, typically, static compilers will convert everything to optimized machine code, so that it runs fast (albeit the compiler is slower and the code isn't really portable). compilers for VMs will often compile to a stack-based bytecode, leaving it up to the backend what to do with the code (say, directly interpreting single-use functions, and JIT compiling frequently-used functions, ...).
      like, fast as computers are, for many things performance can still often be an issue.
      some VMs will use a register-oriented bytecode (ex: like Dalvik or LLVM), which has pros and cons vs a stack bytecode: it is more complicated to produce by a compiler, but can be higher-performance for threaded-code or JIT output (for a still relatively simple backend, mostly due to typically needing 30-60% fewer operations to accomplish a task and being able to put more of the work on the compiler). (note: "registers" in this sense are closer to temporary-variables than to CPU registers).
      also possible is to use a stack-based bytecode, but convert to a register IR internally for the threaded-code or JIT, allowing for simpler compilers at the cost of a more complicated backend (they now need to flatten the stack onto registers/temporaries and similar, include a basic optimizer, ...). this being fairly common in stack-based VMs.

    • @kolrabi
      @kolrabi 10 ปีที่แล้ว

      Yes, you're absolutely right about the performance aspect of course. I was assuming that the code is parsed and executed exactly once (like for example in some kind of calculator application), in which case recursive evaluation should be faster, I suspect. That is, however, unrealistic for most real programs out there.

    • @BGBTech
      @BGBTech 10 ปีที่แล้ว

      Björn P.
      yeah, could be.
      this is a fairly common strategy in many LISP variants IIRC.
      also fairly common though in these cases (such as in a shell or shell-script interpreter or similar) is to directly parse the code and execute commands on a token-by-token basis (without building an intermediate AST), where generally the input is a big glob of code.
      likewise, in assemblers, it is fairly common to go fairly directly from the ASM to the machine-code output (with no intermediate AST).
      in past experiments, I had observed that it was possible to feed about 10-15 MB/sec of ASM code through an assembler, which seemed "probably fast enough" at the time.
      going directly from tokens to bytecode would probably be a bad idea for an HLL compiler though (would tangle/complicate things and hinder AST-level operations, such as expression folding, ...).

  • @talideon
    @talideon 10 ปีที่แล้ว +1

    Er... RPN doesn't really have anything to do with trees. Infix does, but not RPN. RPN is a linearisation of a tree. For instance, you could linearise a tree representing a set of operations in infix using Dijkstra's shunting-yard algorithm. At best, this is kind of a complicated explanation of the shunting-yard algorithm.

  • @wkiernan
    @wkiernan 7 ปีที่แล้ว

    "It makes you do all the hard work" No! At least, not unless you are trying to get the correct answer on a test where some villain has written out an equation without guiding parentheses - and what that would be is not a test to see if you can solve a particular numerical problem, but a test to see only whether you had memorized the arbitrary rules of priority.
    If you are trying to solve an actual problem - e.g. converting Fahrenheit to Centigrade - you already know the desired order of the operations you want to perform - in that example, first subtract 32, then multiply by 5, then divide by 9. Three operations: subtract, multiply, divide, boom, done.
    Now suppose you want to write out the equation in infix format. You have to subtract, multiply, divide, and on top of that you must then pore over the equation referring to a memorized look-up table ("PEMDAS") and analyzing where its arbitrary order of operations will screw up the answer, and then selectively insert parentheses, and hope you haven't made a mistake. Then if you pass this equation to a computer or calculator, it has to do all that work in reverse. So you work harder AND the computer works harder!

  • @culwin
    @culwin 10 ปีที่แล้ว

    He is the Lorax he speaks for the trees

  • @ais4185
    @ais4185 5 ปีที่แล้ว

    1:54 Ha! He didn't say "film"!

  • @pev_
    @pev_ ปีที่แล้ว +1

    I know and understand all this, BUT I think there is a rather serious fault in this video and that is: how do you construct the tree in the first place! He just makes a tree from an expression but never explains how the tree was made, how did he decide to put operands and operators in those specific positions!

  • @JoffreyB
    @JoffreyB 6 ปีที่แล้ว

    video starts at 4:47

  • @CCcrafted
    @CCcrafted 10 ปีที่แล้ว +2

    Lol, 300th viewer. The next person won't know for sure what viewer they are

  • @martixy2
    @martixy2 8 ปีที่แล้ว

    Power is right-associative though.

  • @tatopolosp
    @tatopolosp 10 ปีที่แล้ว

    is it just a stretch.. anyway, thanks

  • @GtaRockt
    @GtaRockt 8 ปีที่แล้ว +8

    title sounds like some random English on a t-shirt they sell in Japan

    • @mahdiarnaufal567
      @mahdiarnaufal567 7 ปีที่แล้ว +1

      you mean china

    • @billl605
      @billl605 5 ปีที่แล้ว

      You don't want a reverse polish notation stain on your shirt.

  • @piwi2005
    @piwi2005 3 ปีที่แล้ว

    Not really true. Nobody was doing abc*+ on the HP48s. Actually everybody was doing bc*a+.

  • @DeviousMalcontent2
    @DeviousMalcontent2 10 ปีที่แล้ว +1

    I was about to say magic :D

  • @MaxElkin
    @MaxElkin 10 ปีที่แล้ว +4

    First!

  • @RobertKelleyPhD
    @RobertKelleyPhD 10 ปีที่แล้ว +4

    ...when computer science is taught by "tree-huggers"...

  • @Calvinatorzcraft
    @Calvinatorzcraft 6 ปีที่แล้ว

    RECURSIVE ARITHMETIC TIME

  • @yankeenobonagu6411
    @yankeenobonagu6411 2 ปีที่แล้ว

    not not is is not not not is not

  • @noswonky
    @noswonky 10 ปีที่แล้ว +2

    Tree hugger.

  • @NotMarkKnopfler
    @NotMarkKnopfler 5 ปีที่แล้ว

    Forth.

  • @willhi5
    @willhi5 ปีที่แล้ว

    YOUR left, not THE left. Hope this helps someone

  • @fletcherreder6091
    @fletcherreder6091 5 ปีที่แล้ว

    '❤ Forth?' if 'like comment' then