Inorder tree traversal: Which definition is correct?

前端 未结 14 1236
轮回少年
轮回少年 2020-12-24 03:42

I have the following text from an academic course I took a while ago about inorder traversal (they also call it pancaking) of a binary tree (not BST):

相关标签:
14条回答
  • 2020-12-24 04:02
    void
    inorder (NODE root)
    {
      if (root != NULL)
        {
          inorder (root->llink);
          printf ("%d\t", root->info);
          inorder (root->rlink);
        }
    }
    

    This the most simplest approach to recursive definition of in-order traversal, just call this function in the main function to get the in-order traversal of a given binary tree.

    0 讨论(0)
  • 2020-12-24 04:02

    It is correct for preorder,nt for inorder

    0 讨论(0)
  • 2020-12-24 04:10

    Forget the definitions, it's so much easier to just apply the algorithm:

    void inOrderPrint(Node root)
    {
      if (root.left != null) inOrderPrint(root.left);
      print(root.name);
      if (root.right != null) inOrderPrint(root.right);
    }
    

    It's just three lines. Rearrange the order for pre- and post- order.

    0 讨论(0)
  • 2020-12-24 04:10

    I personally found this lecture quite helpful.

    0 讨论(0)
  • 2020-12-24 04:10

    Hey according to me as mentioned in wiki is correct the sequence for a inorder traversal is left-root-right.

    Till A, B, C, D, E, F i think you have understood already. Now after root F the next node is G that doesn't hav a left node but a right node so as per the rule (left-root-right) its null-g-right. Now I is the right node of G but I has a left node hence the traversal would be GHI. This is correct.

    Hope this helps.

    0 讨论(0)
  • 2020-12-24 04:12

    this may be late but it could be useful for anyone later .. u just need not to ignore the dummy or null nodes e.g the Node G has a left null node .. considering this null node will make every thing alright ..

    0 讨论(0)
提交回复
热议问题