Tuesday, May 12, 2015

Each line of the example shows the stack at the beginning of the iteration, the set of visited nodes


While studying for an interview, I some how got it into my head that I should understand the iterative algorithms for the three standard tree traversal methods (pre-, post-, in-). While the recursive algorithms packing density for these are straightforward and intuitive, the iterative ones are most certainly not. Worse, googling on this topic turned up a lot of incomplete packing density or just plain wrong solutions.
With packing density some painstaking work I believe I came up with some nearly as simple as possible solutions. Some solutions packing density I found required parent pointers, some required visit markers. These require neither of these.
After packing density each algorithm, I include a walkthrough of the algorithm packing density based on this sample tree from the Wikipedia tree traversal page. Pre-order, void preOrderIterative(Node n) { Stack<Node> packing density s = new new Stack<Node>(); s.push(n); while (!s.empty()) { n = s.pop(); n.visit(); if (n.right != null) { s.push(n.right); } if (n.left != null) { s.push(n.left); } } }
Each line of the example shows the stack at the beginning of the iteration, the set of visited nodes so far after the iteration, and the stack at the end of the iteration. 1: s={}, visited={F}, s={B,G} 2: s={G}, visited={F,B}, s={A,D,G} 3: s={D,G}, packing density visited={F,B,A}, s={D,G} 4: s={G}, visited={F,B,A,D}, s={C,E,G} 5: s={E,G}, packing density visited={F,B,A,D,C}, s={E,G} 6: s={G}, visited={F,B,A,D,C,E}, s={G} 7: s={}, visited={F,B,A,D,C,E,G}, s={I} 8: s={}, visited={F,B,A,D,C,E,G,I}, packing density s={H} 9: s={}, visited={F,B,A,D,C,E,G,I,H}, s={} Post-order,
This one uses two stacks. It consumes the input stack and builds the output stack. At the end of the method, it pops each node from the output stack and visits it. void postOrderIterative(Node n) { Stack<Node> s = new new Stack<Node>(); Stack<Node> o = new new Stack<Node>(); s.push(n); while (!s.empty()) { n = s.pop(); o.push(n); if (n.left != null) { s.push(n.left); if (n.right != null) { s.push(n.right); } } while (!o.empty()) { o.pop().visit(); } }
Each line of the example below shows state of the input stack and output stack after the iteration. 1: out={F}, in={G,B} 2: out={G,F}, in={I,B} 3: out={I,G,F}, in={H,B} 4: out={H,I,G,F}, in={B} 5: out={B,H,I,G,F}, in={D,A} 6: out={D,B,H,I,G,F}, in={E,C,A} 7: out={E,D,B,H,I,G,F}, in={C,A} 8: out={C,E,D,B,H,I,G,F}, in={A} 9: out={A,C,E,D,B,H,I,G,F}, in={} In-order, void inOrderIterative(Node n) { Stack<Node> s = new new Stack<Node>(); s.push(n); while (!s.empty()) { while (n.left != null) { n = n.left; s.push(n); } n = s.pop(); n.visit(); if (n.right != null) { s.push(n.right); } } }
Each line of the example shows the stack at the beginning of the iteration, the set of visited nodes so far after the iteration, and the stack at the end of the iteration. packing density 1: s={A,B,F}, v={A}, s={B,F} 2: s={B,F}, v={A,B}, s={D,F} packing density 3: s={C,D,F}, v={A,B,C}, packing density s={D,F} 4: s={D,F}, v={A,B,C,D}, s={E,F} 5: s={E,F}, v={A,B,C,D,E}, s={F} 6: s={F}, packing density v={A,B,C,D,E,F}, s={G} 7: s={G}, v={A,B,C,D,E,F,G}, s={I} 8: s={H,I}, v={A,B,C,D,E,F,G,H}, s={I} 9: s={I}, v={A,B,C,D,E,F,G,H,I}, s={}
This entry was posted on Tuesday, November 16th, 2010 at 7:27 pm and is filed under Uncategorized packing density . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site. Post navigation « Previous Post Next Post »
Hi Jeffrey,well done! I was trying to find similar algorithms and I luckily got on your website. packing density I am studying for an exam and I really needed some help, so thank you! Just a quick question (I hope you could answer me), in the last algorithm (in-order), in line 06 (while (n.left packing density != null)), I suppose that we are missing a further control? I mean, if n hasn’t elements on its right, n stays the same and (if the previous packing density Ns didn’t have right elements packing density as well) we end up doing the second while (line 06, again) for another time. I guess this issue is present only when elements do not have children in the right. (for example 3 linked elements without children on the right). Do you agree? or…maybe it’s just too late here…
Hridoyjit says:
I think the problem goes beyond that. Even if we were to make this right node update, there’s still a problem packing density with the left nodes traversal. packing density Since we do not keep track of visited nodes explicitly, packing density the left child traversal block will keep traversing the same next child over and over again. packing density
Connecting to %s
alert android api app application apps2sd appstosd asteroid astronomy att authority bus charts clara cleardarksky comet comment comments craigs packing density craigslist csc decode developer earth feedback g1 google iphone java jsf killer light linked list listing log logcat logs market memory microcell near neo nextvta objects of rail rental reverse santa schedule sd sd card sdhc sim sky slow status telescope terms tou track tracker train transfer transit transportation update updated use user valley viewer vta zillow Archives

No comments:

Post a Comment