python - Help me understand Inorder Traversal without using recursion -


I am able to understand the previous traversal without using the rearrangement, but I have been having a hard time with the Inner Traveler. I do not seem to get it, perhaps, because I have not been considered for the inner work of repetition.

This is what I have done so far:

  Def traverseInorder (node): lifo = Lifo () lifo.push (node), while true: node If no one: break the node. No left, none: lifo.push (node. Left) node = node. = Node while true: if no node is: brake print node .value prev = node node = lifo.pop () node = pre node. There is no right: lifo.push (node. Right) node = node. : Break  

The internal timing loop just does not look right. Apart from this, some elements are being printed twice; Maybe I can check that the node has been printed first, but this requires another variable, which does not seem to be correct again. Where am I going wrong?

I have not tried postorder trials, but I think it is similar and I must face the same conceptual blockage there too.

Thank you for your time!

PS: Lifo and node :

  square node: def __init __ (self, value, left Definitions = None, right = none): self.value = value self.left = left self.right = right class Lifo: def __init __ (self): self.lifo = () def Push (self, data): Self.lifo = (data, self.lifo) DRF pop (self): if LAN (self.lifo) == 0: returns are not any, self.lifo = self.lifo return ret   
  travers (node)  

This tail is a clear case of recapitulation, so you can easily put it in a time-loop

  traverse (node): while node! = None: Transges (node. Left) Print node. Resolution node = node. Right Emrat  

You push a new recursive call left with a recursive call, a new reference on the stack, run the code from the beginning, then retrieve the reference and whatever He was doing it. Therefore, you create a stack for storage, and a loop that determines each walk, whether we "first part" position (non-null node) or "return" status (null node, non-empty stack) and suitable Code plays:

  transgresses (node): stack = [] while empty (stack) || Node! = None: If the node! = None: // This is a normal call, Rickers push (stack, node) node = node. Rest / We are back now: pop and print node node to print = pop (stack) node.Viewment node = node. It is difficult to understand the "return" part: You have to set in your loop, whether the code is entering you again in "function" or "returning from the call" Is in the "position, and you have any  if / else  series, as if you have non-terminal recursion in your code. 

In this specific situation, we are using nodes to keep information about the situation. Another way is to store itself in the stack (like a computer is done for recursion). With that technique, the code is less optimal, but it is easy to follow the

  traverse (node): // entry: if the node == return the null (node. Left) back / left -Traversl: Print node.value travers (node. Right) trojes (node): stack = [node, 'entry'] while the empty (stack): [node, state] = pop (stack) switch states: case ' Entry ': if node == none: break; // Return Push (Stack, [node, 'left-traversal']) // Store returns address push (stack, [node. Byte, 'entry']) // recursive call break; Case after 'left-traversal': print node. value; // tail call: no return address push (stack, [node. Right, 'entry']) // recursive call and summer  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -