building an expression tree from infix then walking it to produce postfix

  Рет қаралды 71,091

HurrayBanana

HurrayBanana

Күн бұрын

Пікірлер: 74
@villancikos
@villancikos 8 жыл бұрын
I don't understand why every professor on earth won't look at this video and explain it like that. Most of my professors are always teaching these trivial information in a really complicated way. Thanks a lot!
@kamathprajna
@kamathprajna 3 жыл бұрын
@Daniel Madden wow 😳😲
@Farrukhw
@Farrukhw 6 жыл бұрын
PostFix conversion starts at 5:40 ... Thanks for such a good explanation...
@cakebakeproject
@cakebakeproject 6 жыл бұрын
👌
@gautam1940
@gautam1940 4 жыл бұрын
Left Right Root! Left Right Root! Left Right Root! I just lost track with that .... This is a great explanation!!! Thank you
@superkimsay
@superkimsay 8 жыл бұрын
one word: brilliant! Thank you very much. The Internet is full of crap, but people like you remind me why it is a great invention!
@ryanjensen4232
@ryanjensen4232 4 жыл бұрын
Wow, you're explanation/method was infinitely better than my CS professors. Thanks so much!
@HurrayBanana
@HurrayBanana 4 жыл бұрын
Thanks
@kamathprajna
@kamathprajna 3 жыл бұрын
Simple yet effective explanation..don't know why my professor makes it complex Thank you
@alfredoperez4436
@alfredoperez4436 4 жыл бұрын
Thank you very much! only video that made sense
@Kuro_Alalibo
@Kuro_Alalibo Жыл бұрын
Brilliant illustration. Loved it
@mohammaddcheema9417
@mohammaddcheema9417 9 жыл бұрын
You are a Genius, my very good friend Haadee recommended you to me. I shall remember you forever when I pass my degree. You are a legend !
@MegaGamervids
@MegaGamervids 9 жыл бұрын
You sir helped understand this easily. This is a great approach.
@HurrayBanana
@HurrayBanana 9 жыл бұрын
no problem mate, that was the idea
@Atma505
@Atma505 9 жыл бұрын
Wish I had teachers like you, very nice and clear
@GagandeepSingh-tl7zg
@GagandeepSingh-tl7zg 8 жыл бұрын
That line technique was wonderful, i sorted out the rest myself i.e. preorder and inorder. Keep up the good work. Many thanks
@KishanRaval00
@KishanRaval00 9 жыл бұрын
Oh my God. I didn't thought this approach.Thank you.
@TVChinks
@TVChinks 9 жыл бұрын
u sir have helped me for this exam greatly u shall be be friend in the next life
@HurrayBanana
@HurrayBanana 9 жыл бұрын
nice
@OthmanAlikhan
@OthmanAlikhan 8 жыл бұрын
Really clear explanation and high video quality, thanks a lot =)
@HurrayBanana
@HurrayBanana 8 жыл бұрын
+Othman Alikhan Happy to be of some help
@Venuhmz
@Venuhmz 6 жыл бұрын
this guy is awesome. i wish he was my teacher
@feverdelrey
@feverdelrey 8 жыл бұрын
my teacher taught me another method of how to traverse nodes, but this one was epic! I am taking computing 9691 CIE, and even in the course book, the method is not as good as yours. hats off to you! :)
@HurrayBanana
@HurrayBanana 8 жыл бұрын
+Fever Del Rey Thanks very much, hope I've helped a little bit cheers
@Stxpz
@Stxpz 8 жыл бұрын
Much better than my A-Level teacher.
@BharghaviGanesan
@BharghaviGanesan 9 жыл бұрын
very very useful sir.IT is mind blogging.
@JakzAizzat
@JakzAizzat 7 жыл бұрын
Oh shit. This video totally helped me. Thanks man.
@Arjun69
@Arjun69 6 жыл бұрын
Fantastic. 💕
@w1ndro1d
@w1ndro1d 5 жыл бұрын
Excellent!
@NehaKumari-yf9bh
@NehaKumari-yf9bh 6 жыл бұрын
thank you sir !!!u really made it clear
@danishajaib1923
@danishajaib1923 7 жыл бұрын
Simple and easy to understand. Thanks
@HurrayBanana
@HurrayBanana 7 жыл бұрын
Cheers
@iamdreamerdoer
@iamdreamerdoer 9 жыл бұрын
thanks. you made it look easy
@perdio9359
@perdio9359 7 жыл бұрын
Holy that's so useful thanks.
@ohm7163
@ohm7163 11 ай бұрын
thanks for this video sir::
@TechnoDB
@TechnoDB 5 жыл бұрын
Can't we either convert infix to prefix or postfix first and then construct an expression tree from it with an easy algorithm?
@HurrayBanana
@HurrayBanana 5 жыл бұрын
A true expression tree is built by parsing the input using a grammar based around expressions, this hasn't been on the a level computer science specification since before 2000. Search for expression trees and bnf for more details on building real expression trees
@ankitaharwal5886
@ankitaharwal5886 5 жыл бұрын
i love it
@غيداءابراهيم-ض9ح
@غيداءابراهيم-ض9ح 9 жыл бұрын
thank you your video help me this part was self study
@dighechinmayt
@dighechinmayt 9 жыл бұрын
nice explanation!
@wambateufack
@wambateufack 11 күн бұрын
Thanks man
@Mission.Control
@Mission.Control 8 жыл бұрын
You saved my ass. Thank you.
@UROMSTXY
@UROMSTXY 8 жыл бұрын
Thanks Dr that helped alot
@ViddeshG
@ViddeshG 8 жыл бұрын
Thank you sir! That helped...btw I love the British accent. :D
@jirahroman2138
@jirahroman2138 8 жыл бұрын
do you have a sample code for this problem building an expression tree from infix?
@zkfdsldfjsdjfl1
@zkfdsldfjsdjfl1 5 жыл бұрын
Thank you sir
@FamousEgyptboy
@FamousEgyptboy 6 жыл бұрын
thank you sir :)
@rmp.attackerfake9446
@rmp.attackerfake9446 2 жыл бұрын
I know I'm late but, thanks a lot.
@SuryaKosaraju
@SuryaKosaraju 8 жыл бұрын
Thank you! :)
@VanBhardwaj
@VanBhardwaj 5 жыл бұрын
And to change it to prefix we traverse it the same but mark the line to the left of each node? Does it work?
@HurrayBanana
@HurrayBanana 5 жыл бұрын
Yep
@SubhashSharma-mt5tp
@SubhashSharma-mt5tp 6 жыл бұрын
can u tell how will the expression tree for the expression -a*(b-c)/d+e%f look like?
@HurrayBanana
@HurrayBanana 6 жыл бұрын
Like this, Unary minus is highest precedence, Mod is same as mult and divide drive.google.com/file/d/1tJvsiBRG3e-AylK179iXHhGIP2Wwp0dX/view?usp=sharing
@sy69sempoi
@sy69sempoi 7 жыл бұрын
how about prefix?
@mattarnold198
@mattarnold198 7 жыл бұрын
1:03 "Is everyone happy with that?" - no - the exponent should be done before since it has higher precedence than the plus and the minus in the brackets before that due to the brackets. Arbitrarily adding in brackets changes the meaning of the expression!
@HurrayBanana
@HurrayBanana 7 жыл бұрын
using that logic the exponent operator would be done before the assignment operator. It will be done before the minus but the plus comes before the minus which is at the same level of precedence and therefore left to right evaluation occurs. If you use a stack to convert the reverse polish you will get the same answer as the tree walk which is gfk+pds-^-=
@mattarnold198
@mattarnold198 7 жыл бұрын
But surely the assignment operator should be the last operator to be used right? I mean it's no good assigning when you've not calculated what it is you want to assign beforehand. I've no idea how this left-to-right evaluation is supposed to work as all I can see as the order of that equation is: 1. d - s 2. p ^ (d - s) 3. f + k 4. (f + k) - (p ^ (d - s)) 5. g = ((f + k) - (p ^ (d - s))) Also, how does having the expression in Reverse Polish Notation help equate the answer? If anything it is harder as if you have multiple digit numbers in the expression, you've just lost the operators acting as delimiters between them so you can't tell if 398- is 39 - 8 or 3 - 98.
@HurrayBanana
@HurrayBanana 7 жыл бұрын
The assignment operator is the last operator executed. A compiler cannot process infix, so the expression has to be in reverse polish so the compiler can generate the machine code to calculate the expression, as the data is seen by the time the operation is needed
@mattarnold198
@mattarnold198 7 жыл бұрын
I finally managed to write a program to evaluate infix! It just recurse-calls its own expression parsing logic whenever it encounters an open bracket then when it reaches the corresponding close bracket, it has incremented the character index to that point and can exit the recursion.
@krrrm7313
@krrrm7313 Жыл бұрын
I have a question i didn't hear what you say when you put the power in the same level with f,k you say something about it . can anyone get me this idea straight?
@HurrayBanana
@HurrayBanana Жыл бұрын
I noticed that if I tried to draw the tree with the branches the same length i would run out of room, so before drawing the power node i extended the right branch to give me more room
@blessingadu9605
@blessingadu9605 7 ай бұрын
His postfix expression is incorrect.....even his tree is not following operator precedence.... If tree construction followed operator precedence. Then the final postfix answer should be "g=f+R-p^d-s
@hugjobk578
@hugjobk578 9 жыл бұрын
you skipped the hardest part, how to add the brackets to an infix expression?
@HurrayBanana
@HurrayBanana 9 жыл бұрын
Hùng Doãn Phi I didn't skip it, taught my students this but never bothered doing a video, but it's generally not asked in our specification. It's not that hard anyway you just need to make sure you leave gaps as you go. Here's how to do it (taken from my notes on reverse polish) Converting Postfix back to Infix to preserve precedence ============================================= We can see that if we try to go from an expression tree back to Infix we lose the precedence overrides implicit in the tree. If we need to get back to the Infix expression we can use the Postfix form from the tree. Here is the process: 1. Start at the first item in the Postfix string 2. If item is an operand right it down and leave a gap go to 4 3. If item is an operator write it down in the first available gap between operands and place parenthesis around the operands go to 4 4. Move to next item if it exists and go to 2 5. Remove last brackets placed then attempt to remove all brackets where precedence is not overwritten Using the 3 examples shown we will take the postfix and generate the original Infix expressions, we will start a new line where we encounter an operator and add the parenthesis. Ex1 Postfix: a b c * + a b c a (b * c) (a + (b * c) ) a + (b * c) outside brackets not necessary Infix: a + b * c other brackets not necessary as * is higher than + Ex2 Postfix: a b + c * a b (a + b) c ((a + b) * c) outside brackets not necessary Infix: (a + b) * c final brackets necessary as these override precedence between + and * Ex3 Postfix: g h d + f e k + * - = g h d g (h + d) f e k g (h + d) f (e + k) g (h + d) (f * (e + k) ) g ( (h + d) - (f * (e + k) ) ) (g = ((h + d) - (f * (e + k) ) ) ) g = ((h + d) - (f * (e + k) ) ) outside brackets not necessary g = (h + d) - (f * (e + k) ) outside brackets only surround right hand of assignment g = h + d - (f * (e + k) ) brackets not necessary between + and - evaluate left to right g = h + d - f * (e + k) brackets not necessary between * and - as * is already higher precedence Infix: g = h + d - f * (e + k) final brackets override precedence between * and + so necessary
@hugjobk578
@hugjobk578 9 жыл бұрын
HurrayBanana wow I never thought you would mind answering my question. thank you, sir, but I think you misunderstand my question. I meant you taught us how to create an expression tree from an infix that is helpful, but to do so we need an input expression which is already added brackets in a formatted way like: ((a+b)/c)+(d/e). But when we create a program, it is not only required the accuracy but also convenience for users. And normally, when inputting an expression, people will enter (a+b)/c+d/e instead of ((a+b)/c)+(d/e). So before building an expression tree, we need to adjust the input expression to the form we need. My idea is to store the expression in an array of string (each element of the array will store an operator, an operand, or even a smaller expression which is already added brackets in the way we need). Then we will step by step combine 2 small expressions and an operand between them to bigger expression: A, B, and + become (A+B) // 3 elements become 1 element C, D, and * become (C*D) // 3 elements become 1 element (A+B), (C*D), and / became ((A+B)/(C*D)) // 3 elements become 1 element ... At last we get the first element of the array as the string of the expression we need I think my solution is quite complex, hope you can give me a better way to do this. Sorry for my bad English!
@HurrayBanana
@HurrayBanana 9 жыл бұрын
Hùng Doãn Phi No problems but you don't need to add extra brackets taking your example ( I've mentioned which symbol i am parsing as the font spacing is not predictable) (a+b)/c+d/e just use the stack to convert parse Item 1 ( ============ (a+b)/c+d/e ^ | | | | | | ------- stack empty so push ( on stack rpn currently empty | | | ( | ------- stack parse Item 2 a ============ (a+b)/c+d/e ^ write down a rpn currently a | | | ( | ------- stack parse Item 3 + ============ (a+b)/c+d/e ^ higher precedence than ( so push on stack rpn currently a | | | + | | ( | ------- stack parse Item 4 b ============ (a+b)/c+d/e ^ write down b rpn currently ab | | | + | | ( | ------- stack parse Item 5 ) ============ (a+b)/c+d/e ^ pop contents of stack up to ( and discard ( rpn currently ab+ | | | | | | ------- stack - empty parse Item 6 / ============ (a+b)/c+d/e ^ stack empty so push / on stack rpn currently ab+ | | | | | / | ------- stack parse Item 7 c ============ (a+b)/c+d/e ^ write down c rpn currently ab+c | | | | | / | ------- stack parse Item 8 + ============ (a+b)/c+d/e ^ + not higher precedence than what is on stack, so pop / from stack stack now empty so push + rpn currently ab+c/ | | | | | + | ------- stack parse Item 9 d ============ (a+b)/c+d/e ^ write down d rpn currently ab+c/d | | | | | + | ------- stack parse Item 10 / ============ (a+b)/c+d/e ^ / higher precedence than top of stack so push rpn currently ab+c/d | | | / | | + | ------- stack parse Item 11 e ============ (a+b)/c+d/e ^ write down e rpn currently ab+c/de | | | / | | + | ------- stack Final step dump the stack ========================= finished parsing input so pop entire contents of stack rpn currently ab+c/de/ | | | | | + | ------- stack rpn currently ab+c/de/+ | | | | | | ------- stack - empty
@hugjobk578
@hugjobk578 9 жыл бұрын
HurrayBanana Thanks a lot. I was thinking about how to get a postfix expression from the infix, but I could only do with the infix expression which doesn't contain brackets. Now I will try your solution. By the way, I think your lecture is easy to understand, you are very good at explaining to people, but you don't need to go too deep to the details, just give your students the way, and let them go by themselves
@pahalwanathiest
@pahalwanathiest 5 жыл бұрын
Varanasi
@subcentral5695
@subcentral5695 8 жыл бұрын
u da man xd
@Tadesan
@Tadesan 7 жыл бұрын
Oi!
Infix to reverse polish using a stack
8:59
HurrayBanana
Рет қаралды 65 М.
Real Man relocate to Remote Controlled Car 👨🏻➡️🚙🕹️ #builderc
00:24
Players vs Pitch 🤯
00:26
LE FOOT EN VIDÉO
Рет қаралды 133 МЛН
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 57 МЛН
DSA26a - Expression Trees
12:27
EZCSE
Рет қаралды 15 М.
Reverse Polish Grows on Trees - Computerphile
9:51
Computerphile
Рет қаралды 93 М.
Trees 4 Expression Trees
3:35
RobEdwards
Рет қаралды 15 М.
Binary Expression Trees
6:37
Miran Fattah
Рет қаралды 137 М.
Real Man relocate to Remote Controlled Car 👨🏻➡️🚙🕹️ #builderc
00:24