Iterative Inorder Traversal of Binary Tree

  Рет қаралды 121,746

Tushar Roy - Coding Made Simple

Tushar Roy - Coding Made Simple

Күн бұрын

Пікірлер: 89
@euisungkim8028
@euisungkim8028 5 жыл бұрын
Really enjoy your visualizations of algorithms which facilitate learning. Thanks.
@andypaladino3711
@andypaladino3711 7 жыл бұрын
Thank you so much for making the concepts so easy to grasp!
@TheZwirek
@TheZwirek 6 жыл бұрын
Why most people "explain" the algorithm by going step by step on some example, instead of actually explaining it by explaining the whys and the hows?
@creumakuzola4227
@creumakuzola4227 5 жыл бұрын
I'm making myself the same question. This way we don't understand the algorithm, but the example.
@xenobob2773
@xenobob2773 4 жыл бұрын
What whys and hows are there for this code? Its a stack, and its in-order. Its use is printing out a Binary Search Tree in ascending order (as far as I can tell). Thats about it.
@shresthmishra9329
@shresthmishra9329 4 жыл бұрын
Criticising others for self incapability is the first step towards self destruction.
@sohamjoshi9527
@sohamjoshi9527 4 жыл бұрын
Spot on... I was wondering the same :) people who need to be given one step after other will learn well from this like @Xeno Bob and @Shresth Mishra...
@FINSuojeluskunta
@FINSuojeluskunta 3 жыл бұрын
The only thing you need to understand is how an inorder traversal works. You're just doing it with a stack instead of a call stack and recursion.
@drishyakandpal1850
@drishyakandpal1850 4 жыл бұрын
Finally understood the concept! Thank you so much Sir!
@studyaccount794
@studyaccount794 2 жыл бұрын
This channel is gold. The videos are simple yet amazing.
@נדבסלמן-ט2מ
@נדבסלמן-ט2מ 8 жыл бұрын
Thank you very much, this algorithm has helped me to solve the problem: " write function that receives a binary search tree and returns a list sorted in ascending order containing all the item int the tree(integer , the list and the tree).thank: public static Node TreeToTHEnode(BinNode t) { Stack s = new Stack(); Node L = new Node(-1); Node lastNode = L; while (true) { if (t != null) { s.Push(t); t = t.GetLeft(); } else { if (s.IsEmpty()) { break; } t = s.Pop(); lastNode.SetNext(new Node(t.GetValue())); lastNode = lastNode.GetNext(); t = t.GetRight(); } } L = L.GetNext(); return L; }
@pruthvigowda7950
@pruthvigowda7950 8 жыл бұрын
Yes, that's true. You can do in order traversal for BST (not binary tree) to get the elements in sorted (ascending order). The converse is also true, you can do in-order traversal and check if the elements are sorted (ascending order) in order to determine if the given binary tree is a BST.
@juhabach6371
@juhabach6371 3 жыл бұрын
most clear explanation on KZbin.. thanks for such an amazing content
@anujdwivedi5299
@anujdwivedi5299 3 жыл бұрын
thanks for the explanation of traversing tree unsing sthack
@kumarc4853
@kumarc4853 Жыл бұрын
this video doesnt explain the intuition behind the algo but only explains the steps of the algorithm
@lizdenhup
@lizdenhup 4 жыл бұрын
I finally understand thanks to this video!! Thank you!
@kumarakash5219
@kumarakash5219 3 жыл бұрын
It is wrong code in case of right skewed tree it will not work
@TriforceTech_385
@TriforceTech_385 5 жыл бұрын
Simple and clear explanation. Thank you
@mukulupadhyay4656
@mukulupadhyay4656 3 жыл бұрын
very well explained...
@poojask6166
@poojask6166 4 жыл бұрын
Very well explained. Thank you
@HarshaVardhan-jf9sd
@HarshaVardhan-jf9sd 6 жыл бұрын
Hi tushar...is there no other way to do an inorder traversal iteratively without using a stack and morris algorithm?
@ethanhuang2127
@ethanhuang2127 4 жыл бұрын
how to check one node is visited or not? Using hashset? but that may only work for no duplicates case, right?
@abhigyanshrivastava1255
@abhigyanshrivastava1255 4 жыл бұрын
"my name is -char"
@allenllewellynkra
@allenllewellynkra 4 жыл бұрын
lol u wrong for that
@Laoguang09
@Laoguang09 5 жыл бұрын
This is easy to understand, NICE VIDEO!!!
@samisaacvicliph
@samisaacvicliph 7 жыл бұрын
Excellent explanation. Just a small doubt. Is -1 suppose to be the left child of 2? I'm not sure about 11 as well.
@vinaypatel1106
@vinaypatel1106 7 жыл бұрын
It's for General Binary Tree..not for specific Binary Search Tree..
@starfire_xi
@starfire_xi 6 жыл бұрын
Very very helpful! Thank you so much
@JasmeetSingh-oh8fq
@JasmeetSingh-oh8fq 3 жыл бұрын
my brain start glitching after seeing this video..
@zineddinelouzani7069
@zineddinelouzani7069 3 жыл бұрын
this guy is a machine
@josephstrauss9653
@josephstrauss9653 7 жыл бұрын
It's pretty clear! Thanks a lot
@llliuyu
@llliuyu 7 жыл бұрын
Very clear explanation, thanks!
@rohithbharathi3664
@rohithbharathi3664 Жыл бұрын
thank you
@thisisthebestid
@thisisthebestid 6 жыл бұрын
Can you answer this in interview without knowing the answer beforehand?
@xenobob2773
@xenobob2773 4 жыл бұрын
Kinda... its just a stack. And you implicitly use a stack in the recursive version. For 90% of questions though, no.
@vinayak186f3
@vinayak186f3 4 жыл бұрын
You actually don't need to , just inform them of your approach ,if they feel like you can get to the solution , they'll guide you if you get stuck
@sumeetsood232
@sumeetsood232 4 жыл бұрын
What is you tube link to see similar questions? he shared it in end of video but i am not able to understand it
@avisha92
@avisha92 5 жыл бұрын
Generally speaking, while(true) is a bad practice (in case you accidentally leave no exit routes) and I can imagine the interview calling that out.
@holyshit922
@holyshit922 5 жыл бұрын
Conversion binary tree to doubly linked list is similar to the traversal (inorder is good when we have binary search tree and want sorted doubly linked list ) but instead of printing key value of nodes we append it into doubly linked list Can we convert binary tree to doubly linked list without recursion and without creating new nodes in time O(n) ?
@ankurshah3112
@ankurshah3112 2 жыл бұрын
Yes def treeToDoublyList(self, root: 'Optional[Node]') -> 'Optional[Node]': if not root: return None stack = [] temp = root head = Node(0, None, None) last = head final = root while True: if temp: stack.append(temp) temp = temp.left else: if not stack: break temp = stack.pop() final = temp last.right = temp temp.left = last last = temp temp = temp.right first = head.right final.right = first first.left = final return head.right
@kuro7249
@kuro7249 7 жыл бұрын
Thank you so much, this video is really helpful.
@codemarshal655
@codemarshal655 Жыл бұрын
Did you know, you can do iterative in / pre / post order traversals using same code?? Checkout how I did it in just 20 lines, in my new video kzbin.info/www/bejne/bKjbf5ZunKidbqc
@gabriellerosa6453
@gabriellerosa6453 4 жыл бұрын
love your videos , im from brazil thx
@deathbombs
@deathbombs 3 жыл бұрын
recursive so easy and iterative so tough
@yangwang24
@yangwang24 8 жыл бұрын
Thanks a lot.
@CrazySkillz15
@CrazySkillz15 5 жыл бұрын
Thanks. Your video made it so easy to understand. Much Appreciated :) :D
@nocturn2002
@nocturn2002 4 жыл бұрын
awesome video
@supremoluminary
@supremoluminary 4 жыл бұрын
How did you figure this out?
@haiphan9181
@haiphan9181 5 жыл бұрын
bruh what you smoked send me some
@snlagr
@snlagr 4 жыл бұрын
why? i dont get the joke
@anjushajoshi8497
@anjushajoshi8497 7 жыл бұрын
I am getting an error when I do this :stacks=new stacksp;
@dhruvyadav1159
@dhruvyadav1159 4 жыл бұрын
Always try to avoid dynamic allocation of STL
@rishabhjain2404
@rishabhjain2404 8 жыл бұрын
Thank you!
@junjiezhang4523
@junjiezhang4523 5 жыл бұрын
great!
@cagrsahin4132
@cagrsahin4132 5 жыл бұрын
how is -1 bigger than 11?
@sadhlife
@sadhlife 3 жыл бұрын
this is not a binary search tree, it's not sorted data, it's just a random binary tree
@arthur6892
@arthur6892 8 жыл бұрын
Thank you so much for sharing this. It's so clear and helps me a lot!
@hemsagarpatel8992
@hemsagarpatel8992 5 жыл бұрын
nice
@sumodmadhavan
@sumodmadhavan 5 жыл бұрын
I don't think it is a valid BST because it is not sorted. I hope idea is to only explain Inorder using stack.
@nishantkaushik7161
@nishantkaushik7161 4 жыл бұрын
no where it is written that its a "binary search tree".... just a binary tree..
@vishfulthinker
@vishfulthinker 5 жыл бұрын
Isn't the time complexity Big O of n and and not 'h'?
@xGXLx
@xGXLx 3 жыл бұрын
I know this is from two years ago, but h can equal n in it's worst case
@SomnathMukherjee1112
@SomnathMukherjee1112 8 жыл бұрын
please make a video on Inorder Tree Traversal without recursion and without stack! Morris Traversal!
@jbejyz
@jbejyz 9 жыл бұрын
I think you make some thing wrong in the Binary search tree. The left children of root 10 should less than it and right children should greater than their root
@TheAmonkeys
@TheAmonkeys 9 жыл бұрын
+Buer Jiang This was a traversal of a binary tree not a BST.
@algorithmimplementer415
@algorithmimplementer415 5 жыл бұрын
Nice explanation :)
@kunalmahajan7142
@kunalmahajan7142 7 жыл бұрын
Tushar your videos are lovely. Can you please start making videos on codechef challenges if possible.... It would be really beneficial for us in understanding complex questions. ThankYou! :)
@deathbombs
@deathbombs 3 жыл бұрын
umm you just jump into algorithm explaining how it works... but no explanation of how to arrive at it?
@kumarakash5219
@kumarakash5219 3 жыл бұрын
This code will not work in case of right-skewed tree
@kritisingh3194
@kritisingh3194 3 жыл бұрын
It works fine in case of right-skewed tree. Suppose, root = 15 root->right = 25 root->right->right = 35 root=15 root!=NULL, we will push 15 to stack. root=root->left = NULL So, now as root==NULL, we go to else condition and print 15 Now, root = root->right = 25 And then, you continue following the same process.
@kumarakash5219
@kumarakash5219 3 жыл бұрын
@@kritisingh3194 yes ty for explanation
@vinayak186f3
@vinayak186f3 4 жыл бұрын
REVISION NO 3 😶
@shikharjain6357
@shikharjain6357 5 жыл бұрын
Toosharp!!
@adeled8833
@adeled8833 4 жыл бұрын
i need subtitles
@juanflores2226
@juanflores2226 3 жыл бұрын
In python please 🙂
@anoopnayak8106
@anoopnayak8106 7 жыл бұрын
Iterative sika na bp😡
@reviewterrain9835
@reviewterrain9835 6 жыл бұрын
whats "sthack" lol
@konarkverma9102
@konarkverma9102 5 жыл бұрын
have you smoked weed?
@adityadarekar3963
@adityadarekar3963 7 жыл бұрын
why u r trying an American akcent
@Boarky
@Boarky 7 жыл бұрын
Why ask? He's really easy to understand, is he not for you?
@adityadarekar3963
@adityadarekar3963 7 жыл бұрын
Boarky Chup be lvde
@simrandotdev
@simrandotdev 7 жыл бұрын
He has been living in US from last 9 years. So you tend to start speaking certain words how American speaks so they can understand.
@VidyaranyaJayaPrakash
@VidyaranyaJayaPrakash 6 жыл бұрын
why don't you start off by learning spellings lvde
@mightbeLara
@mightbeLara 5 жыл бұрын
Aditya Darekar Why shouldn’t he ?
@Kushagra_21
@Kushagra_21 4 жыл бұрын
why do you teach as if we are here to mugg up everything.
@dermot864
@dermot864 4 жыл бұрын
thank you
@alvxlopez
@alvxlopez 4 жыл бұрын
thank you
Level by Level Printing of Binary Tree
15:37
Tushar Roy - Coding Made Simple
Рет қаралды 66 М.
Fenwick Tree or Binary Indexed Tree
22:43
Tushar Roy - Coding Made Simple
Рет қаралды 238 М.
SISTER EXPOSED MY MAGIC @Whoispelagheya
00:45
MasomkaMagic
Рет қаралды 13 МЛН
Кәсіпқой бокс | Жәнібек Әлімханұлы - Андрей Михайлович
48:57
Human vs Jet Engine
00:19
MrBeast
Рет қаралды 125 МЛН
哈哈大家为了进去也是想尽办法!#火影忍者 #佐助 #家庭
00:33
火影忍者一家
Рет қаралды 130 МЛН
Lowest Common Ancestor Binary Tree
11:08
Tushar Roy - Coding Made Simple
Рет қаралды 252 М.
Iterative Postorder Traversal of Binary Tree
8:29
Tushar Roy - Coding Made Simple
Рет қаралды 79 М.
Level Order Traversal of Binary Tree
7:57
Tushar Roy - Coding Made Simple
Рет қаралды 67 М.
Iterative Postorder traversal of binary tree using one stack
14:05
Tushar Roy - Coding Made Simple
Рет қаралды 116 М.
Morris Inorder Tree Traversal
11:44
Tushar Roy - Coding Made Simple
Рет қаралды 146 М.
Knuth-Morris-Pratt(KMP) Pattern Matching(Substring search)
12:50
Tushar Roy - Coding Made Simple
Рет қаралды 1,1 МЛН
Largest BST in Binary Tree
9:43
Tushar Roy - Coding Made Simple
Рет қаралды 89 М.
SISTER EXPOSED MY MAGIC @Whoispelagheya
00:45
MasomkaMagic
Рет қаралды 13 МЛН