开发者

Binary Tree Double Order Traversal

Can anyone explain me the double order traversal?

        A
      /   \
     B     E
   /  \   /  \
  C   D  F    G

Double order Traversal output : ABCCBDDAEFFEGG

I'm interested in exp开发者_StackOverflowlanation rather than the code.

Thanks


Assuming you're interested in an explanation of what a double-order traversal does:

For each traversal, you

  • Visit Node
  • Traverse Left Child
  • Visit Node
  • Traverse Right Child

That's all there is to it. In cases where you don't have a left child (like C, for example), the two "visit node" operations happen back to back, which is why you see two Cs in your output.

Just to visualize it (with the output in bold):

  • Visit A: A
  • Traverse left child B
    • Visit B: AB
    • Traverse left child C
      • Visit C: ABC
      • No left child
      • Visit C: ABCC
      • No right child
    • Traverse right child D
      • Visit D: ABCCD
      • No left child
      • Visit D: ABCCDD
      • No right child
  • Visit A: ABCCDDA
  • Traverse right child E
    • Visit E: ABCCDDAE

etc.


Imagine you are walking, beginning at the root.

  1. You are at A;
  2. Walk to left child, you get to B;
  3. Walk to left child, C;
  4. Dead end, you turn back, still C;
  5. Back at B;
  6. Walk to right child, to D;
  7. Dead end, turn back still D;

etc.

This is just a traversal that kind of count both in and outs.

Between the visit of left and right children in a preorder traversal, you visit the root (because you must come back to it to walk further), and you can think of leaves as roots having no children, and null will just make you go back in no time (hence the two consecutive visits to leaves, and nodes only have right children).


Recurse through:

1. Visit root of (sub)tree.
2. Visit left subtree.
3. Revisit root of (sub)tree.
4. Visit right subtree.

Offhand I can't think of an application for it, though I have done some truly freakish things with a pile of nodes that are in several trees (and linked lists) at once.


Mainly it is a recursive approach to traverse a tree (here we are dealing with Binary tree)

Here is an algorithm for Double Order Traversal :

Algorithm DoubleOrder(root)

  1. if(root is not NULL)

    1. print(root)
    2. DoubleOrder(leftSubtree) //recursive call
    3. print(root)
    4. DoubleOrder(rightSubtree) // recursive call
  2. endif

  3. end DoubleOrder

Explanation :

  1. Visit A & print it output : A
  2. A have left subtree
    1. Visit B & print it output : AB
    2. B have left subtree
      1. visit C and print it output : ABC
      2. C have no left subtree
      3. Visit C again and print it output : ABCC
      4. C have no subtree
      5. return to B (all 4 statements have executed in case of C)
    3. Visit B again and print it output : ABCCB
    4. B have right subtree
      1. visit D and print it output : ABCCBD
      2. D have no left subtree
      3. Visit D again and print it output : ABCCBDD
      4. D have no subtree
      5. return to B (all 4 statements have executed in case of D)
    5. return to A (all 4 statements have executed in case of B)
  3. Visit A again and print it output : ABCCBDDA
  4. A have right subtree
    1. Visit E & print it output : ABCCBDDAE
    2. E have left subtree
      1. visit F and print it output : ABCCBDDAEF
      2. F have no left subtree
      3. Visit F again and print it output : ABCCBDDAEFF
      4. F have no subtree
      5. return to E (all 4 statements have executed in case of F)
    3. Visit E again and print it output : ABCCBDDAEFFE
    4. E have right subtree
      1. visit G and print it output : ABCCBDDAEFFEG
      2. G have no left subtree
      3. Visit G again and print it output : ABCCBDDAEFFEGG
      4. G have no subtree
      5. return to E (all 4 statements have executed in case of G)
    5. return to A (all 4 statements have executed in case of E)
  5. Stop execution as we return back to main root of tree(as all 4 statement have executed in case of A)

Final output : ABCCBDDAEFFEGG

I hope it may helps you to understand overall concept! I am very happy to answer to stack overflow for the first time :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜