Help me understand Inorder Traversal without using recursion

Start with the recursive algorithm (pseudocode) : traverse(node): if node != None do: traverse(node.left) print node.value traverse(node.right) endif This is a clear case of tail recursion, so you can easily turn it into a while-loop. traverse(node): while node != None do: traverse(node.left) print node.value node = node.right endwhile You’re left with a recursive call. What … Read more

How to implement depth first search for graph with a non-recursive approach

A DFS without recursion is basically the same as BFS – but use a stack instead of a queue as the data structure. The thread Iterative DFS vs Recursive DFS and different elements order handles with both approaches and the difference between them (and there is! you will not traverse the nodes in the same … Read more

Non-recursive os.walk()

Add a break after the filenames for loop: for root, dirs, filenames in os.walk(workdir): for fileName in filenames: print (fileName) break #prevent descending into subfolders This works because (by default) os.walk first lists the files in the requested folder and then goes into subfolders.

Post order traversal of binary tree without recursion

Here’s the version with one stack and without a visited flag: private void postorder(Node head) { if (head == null) { return; } LinkedList<Node> stack = new LinkedList<Node>(); stack.push(head); while (!stack.isEmpty()) { Node next = stack.peek(); boolean finishedSubtrees = (next.right == head || next.left == head); boolean isLeaf = (next.left == null && next.right == … Read more

How does Tortoise’s non recursive commit work?

Found by Google how to fix it: press F5 in the commit window (not in the “warning popup”) See http://tortoisesvn.tigris.org/ds/viewMessage.do?dsForumId=757&dsMessageId=2831045 for details. On 26.08.2011 22:39, Ryan J Ollos wrote: For several months now I’ve been seeing the following dialog box appear when initiating Commit. It frequently happens when attempting to commit following a merge. The … Read more

tech