Binary tree traversal with stack data structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sankar2011
    New Member
    • Nov 2011
    • 18

    Binary tree traversal with stack data structure

    This is an eclipse based project. So the attachment may be unzipped and run in eclipse.
    eclipse Juno and Java 7 has been used. First the code should be inspected and run. Then the blog may be reffered.
    =============== =============== ========

    Stack based implementation of Binary Tree traversal. The ArrayList<Integ er> result variable will have the traversal result
    In this example I have added the data from a node to the ArrayList.
    Entry.java should be run as Java Application. This class basically tests the functionalities .

    A close inspection to the code will make it easy to understand that left branch of a root is always explored before the right branch. For example: The order of stack insertion of nodes in a sub tree is the same for all three traversals.
    1) Insert Root --> 2) insert until the last left child --> 3)pop until the top node of the stack has a right child--> 4) Insert Right child (This child is the new Root now, call this node as "new Root 1") -->5) Follow all the steps from 2 to 4 recursively till the top node of the stack is again "new Root 1" --> pop "new Root 1" --> pop Root


    InOrder
    =======
    The Node data will be added to the ArrayList only in the following cases

    1> When any of the leafe nodes pops, it's data/node will be added
    2> Immidiately after a left child is popped, the parent's data/node will be added
    3> if the parent does not have a left child, it's data/node will be added immidiately before it's right child is pushed into the stack.

    PreOrder
    ========
    After every push() operation to the stack, the data/node of the pushed node will be added.

    Post Order
    ==========

    The Node data will be added to the ArrayList only in the following cases

    1> If a node does not have a right child, but has a left child, then immidiately after the left child is popped, parent's data/node
    will be added (after popping the parent).

    2> Immidiately after popping out the right son, the parent's data/node will be added (after popping the parent).

    3> A leaf is always popped and it's data/node is added.

    There should be one change and the following line in italics will be replaced by the statement in bold.
    Attached Files
    Last edited by zmbd; Sep 29 '14, 10:51 PM. Reason: [z{per op change to article/ Kept the original phrase and added conjunctive to tie new material with the old}]
  • sankar2011
    New Member
    • Nov 2011
    • 18

    #2
    Modification

    Hi ZMBD, could you please remove the following line in italics completely?

    "A close inspection to the code will make it easy to understand that left branch of a root is always explored before the right branch. For example:"

    I am unable to do it.
    Thanks in advance!

    Comment

    • sankar2011
      New Member
      • Nov 2011
      • 18

      #3
      For theory portion please follow my another article


      Also please ignore the following portion in this current article

      A close inspection to the code will make it easy to understand that left branch of a root is always explored before the right branch. For example: The order of stack insertion of nodes in a sub tree is the same for all three traversals.
      1) Insert Root --> 2) insert until the last left child --> 3)pop until the top node of the stack has a right child--> 4) Insert Right child (This child is the new Root now, call this node as "new Root 1") -->5) Follow all the steps from 2 to 4 recursively till the top node of the stack is again "new Root 1" --> pop "new Root 1" --> pop Root

      Comment

      Working...