The problem you're seeing is that you need to "remember" the last place you were iterating at. When doing recursion, the program internally uses "the stack" to remember where to go back to. But when doing iteration, it doesn't.Although... does that give you an idea?
I know it has got something to do with pushing the things on stack and then popping them, but I really cant figure this out, like what to push, when to pop, and how. – Karan Sep 25 at 19:44 ok, I can start by pushing all the nodes, till I reach the leftmost leaf, now after that I start popping,now after popping first one, I push the right child on stach, and keep pushing till I reach the leftmost leaf of this subtree, now I go to the right tree, and reach the right most leaf of that subtree, but now how would I know how much pops do I need to do so that I reach the parent of the leftmost leaf of the original tree? – Karan Sep 25 at 19:58 kindly help me out a bit more with the stack thing, if you can.
– Karan Sep 25 at 20:10 @Karan: I'm not sure if I quite followed what you mentioned, but what you want to do is to do this: (1) push the current node and keep pushing its left children as many times as you can (i.e. Until there is no left node), (2) pop the last node (this has the return value), (3) repeat step 1 with the right child, if any, of the last node you popped. – Mehrdad Sep 25 at 20:14 kindly check the edits.
– Karan Sep 25 at 20:19.
I can't think of a really elegant way to do this iteratively off-hand. One possibility might be using a 'mark algorithm', where you start out with all nodes 'unmarked' and 'mark' nodes as they're handled. The markers can be added to the object model or kept in a seperate entity.
Pseudocode: for (BinaryTree currentNode = leftmostNode(root); currentNode! = null; currentNode = nextNode(currentNode)): print currentNode; currentNode. Seen = true; sub nextNode(BinaryTree node): if (!node.left.
Seen): return leftmostNode(node. Left) else if (!node. Seen) return node else if (!node.right.
Seen) return leftmostNode(node. Right) else return nextUnseenParent(node) sub leftmostNode(BinaryTree node): while (node. Left!
= null) node = node. Left return node; sub nextUnseenParent(BinaryTree node): while (node.parent. Seen) node = node.
Parent return node.parent.
– Karan Sep 25 at 20:01 also, I don't think this is an inorder traversal, as for the matter of fact, you started printing from the root. – Karan Sep 25 at 20:04 The 'iteration step' is a bit more complicated than usual, but it contains only for loops, while loops and no recursive calls, so I'd say it should qualify as iterative. – Arnout Engelen Sep 25 at 20:04 (edited to start from the leftmost node instead of the root) – Arnout Engelen Sep 25 at 20:05 thanks............! – Karan Sep 25 at 20:08.
I take it for granted, that iterating down from the parent nodes to the left nodes is not a problem. The problem is to know what to do when going up from one node to the parent: should you take the right child node or should you go up one more parent? The following trick will help you: Before going upwards remember the current node.
Then go upwards. Now you can compare: Have you been in the left node: Then take the right node. Otherwise go up one more parent node.
You need only one reference/pointer for this.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.