Iterative Inorder Traversal of Binary Tree

Поделиться
HTML-код
  • Опубликовано: 17 ноя 2024

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

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

    This channel is gold. The videos are simple yet amazing.

  • @andypaladino3711
    @andypaladino3711 7 лет назад +6

    Thank you so much for making the concepts so easy to grasp!

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

    Really enjoy your visualizations of algorithms which facilitate learning. Thanks.

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

    most clear explanation on RUclips.. thanks for such an amazing content

  • @drishyakandpal1850
    @drishyakandpal1850 4 года назад +5

    Finally understood the concept! Thank you so much Sir!

  • @TheZwirek
    @TheZwirek 6 лет назад +58

    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 лет назад +3

      I'm making myself the same question. This way we don't understand the algorithm, but the example.

    • @xenobob2773
      @xenobob2773 4 года назад +7

      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 года назад +5

      Criticising others for self incapability is the first step towards self destruction.

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

      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 4 года назад +1

      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.

  • @נדבסלמן-ט2מ
    @נדבסלמן-ט2מ 8 лет назад +2

    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.

  • @thisisthebestid
    @thisisthebestid 6 лет назад +11

    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

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

    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

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

    this video doesnt explain the intuition behind the algo but only explains the steps of the algorithm

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

    thanks for the explanation of traversing tree unsing sthack

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

    Simple and clear explanation. Thank you

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

    This is easy to understand, NICE VIDEO!!!

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

    Thanks. Your video made it so easy to understand. Much Appreciated :) :D

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

    how to check one node is visited or not? Using hashset? but that may only work for no duplicates case, right?

  • @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

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

    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
    ruclips.net/video/6wxNc8gCj8E/видео.html

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

    Thank you so much for sharing this. It's so clear and helps me a lot!

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

    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.

  • @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?

  • @josephstrauss9653
    @josephstrauss9653 7 лет назад +3

    It's pretty clear! Thanks a lot

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

    this guy is a machine

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

    Very clear explanation, thanks!

  • @mukulupadhyay4656
    @mukulupadhyay4656 3 года назад +1

    very well explained...

  • @abhigyanshrivastava1255
    @abhigyanshrivastava1255 4 года назад +4

    "my name is -char"

  • @JasmeetSingh-oh8fq
    @JasmeetSingh-oh8fq 3 года назад

    my brain start glitching after seeing this video..

  • @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

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

    Very very helpful! Thank you so much

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

    Thank you so much, this video is really helpful.

  • @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 лет назад +1

      It's for General Binary Tree..not for specific Binary Search Tree..

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

    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

  • @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

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

    love your videos , im from brazil thx

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

    please make a video on Inorder Tree Traversal without recursion and without stack! Morris Traversal!

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

    recursive so easy and iterative so tough

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

    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 лет назад +19

      +Buer Jiang
      This was a traversal of a binary tree not a BST.

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

    How did you figure this out?

  • @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

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

    awesome video

  • @haiphan9181
    @haiphan9181 5 лет назад +13

    bruh what you smoked send me some

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

      why? i dont get the joke

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

    Nice explanation :)

  • @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

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

    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..

  • @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! :)

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

    thank you

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

    Thank you!

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

    Thanks a lot.

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

    great!

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

    Toosharp!!

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

    nice

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

    In python please 🙂

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

    REVISION NO 3 😶

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

    whats "sthack" lol

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

    i need subtitles

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

    Iterative sika na bp😡

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

    have you smoked weed?

  • @adityadarekar3963
    @adityadarekar3963 7 лет назад +11

    why u r trying an American akcent

    • @Boarky
      @Boarky 7 лет назад +23

      Why ask? He's really easy to understand, is he not for you?

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

      Boarky Chup be lvde

    • @simrandotdev
      @simrandotdev 7 лет назад +21

      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 лет назад +6

      why don't you start off by learning spellings lvde

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

      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

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

    thank you