non-recursive

Help me understand Inorder Traversal without using recursion

烂漫一生 提交于 2019-12-18 09:59:07
问题 I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion. This is what I've tried so far: def traverseInorder(node): lifo = Lifo() lifo.push(node) while True: if node is None: break if node.left is not None: lifo.push(node.left) node = node.left continue prev = node while True: if node is None: break print node.value prev = node node =

How to rewrite Ackermann function in non-recursive style?

筅森魡賤 提交于 2019-12-17 18:28:15
问题 I have function public static int func(int M,int N){ if(M == 0 || N == 0) return M+N+1; return func(M-1, func(M, N-1)); } How to rewrite it in non-recursive style ? Maybe, is it implementation some algorithm? 回答1: Not quite O(1) but definitely non-recursive. public static int itFunc(int m, int n){ Stack<Integer> s = new Stack<Integer>; s.add(m); while(!s.isEmpty()){ m=s.pop(); if(m==0||n==0) n+=m+1; else{ s.add(--m); s.add(++m); n--; } } return n; } 回答2: This looks like homework, so I won't

Recursive vs non-recursive [duplicate]

戏子无情 提交于 2019-12-13 08:08:50
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Recursion and Iteration What is the difference between a recursive and a non-recursive function? Fibonacci to be exact. I looking for answers that relate towards the time and memory. 回答1: "Recursive" simply means that a function calls itself. This may or may not be intentional (unintentional recursion is responsible for lots of crashes). Intentional recursion, where a function performs part of an operation, then

Least common ancestor search in binary tree non recursive version - Java

南楼画角 提交于 2019-12-08 18:11:23
I am searching a non recursive algorithm version of finding the least common ancestor in a sorted binary tree written in Java. Everything I found is only the recursive version (even on stackoverflow and on other websites). Can some one write or direct me to the non recursive version (using while loop)? Also write if this version is more efficient in terms of time complexity? Just happened to see this long forgotten question. Do you mean something like, if you are given a tree: A B C D E F G H I J K L M N O commonAncestor(L,G) = C commonAncestor(H,O) = A commonAncestor(B,H) = B something like

Non-recursive add function in a binary tree using c++

断了今生、忘了曾经 提交于 2019-12-06 14:46:03
I am writing an Add function to add nodes to a binary tree non recursively. I have run into the problem of only being able to produce one level deep binary tree. I debugged it and I know where the problem is but can't figure out how to fix it. Maybe fresh pair of eyes will see something i dont... The problem is that my temp node is being reset to the root value every new function call and hence, adding nodes linearly. Anyways, here is the function: void BinaryTreeNonRec::add(int num){ treeNode *temp, *parent; if (root==NULL) { root = new treeNode; root->data=num; root->left=0; root->right=0;

SVN not updating recursively

心不动则不痛 提交于 2019-12-04 08:26:34
问题 A few weeks ago, I checked out our whole SVN repo in --non-recursive mode. Now it seems that when I do a svn up , it does not update the folder recursively. It's a problem because I'd like to get the changes from my colleagues without having to go through each directory and do a svn up manually... How can I force the update to be recursive ? 回答1: on next svn update command provide the parameter --depth : svn up --depth infinity on checking out a non-recursive checkout, subversion remembers

Retrieving a Binary-Tree node's depth non-recursively

早过忘川 提交于 2019-11-30 04:08:09
Can anyone point out a way of getting the depth of a Node in a Binary Tree (not a balanced one, or BST) without using recursion ? Ideally in Java/C/C# The node is represented as: class Node { Node Left; Node Right; string Value; int Depth; } Using Level Order with a FIFO list was my first thought, but I was stumped at detecting when the level changes, particular for unbalanced trees. I assume you mean filling in the Depth value on node, and/or finding max depth. One way to do this would be using two lists, and doing the level order as suggested. It'd be akin to: int level=0; List<Node>

Help me understand Inorder Traversal without using recursion

99封情书 提交于 2019-11-29 18:53:05
I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion. This is what I've tried so far: def traverseInorder(node): lifo = Lifo() lifo.push(node) while True: if node is None: break if node.left is not None: lifo.push(node.left) node = node.left continue prev = node while True: if node is None: break print node.value prev = node node = lifo.pop() node = prev if node.right is not None: lifo.push(node.right) node = node.right else: break

Retrieving a Binary-Tree node's depth non-recursively

感情迁移 提交于 2019-11-29 01:57:06
问题 Can anyone point out a way of getting the depth of a Node in a Binary Tree (not a balanced one, or BST) without using recursion ? Ideally in Java/C/C# The node is represented as: class Node { Node Left; Node Right; string Value; int Depth; } Using Level Order with a FIFO list was my first thought, but I was stumped at detecting when the level changes, particular for unbalanced trees. 回答1: You can implement any resursive method with a stack, which is how resursion works anyways. Imagine your

Non-recursive implementation of Flood Fill algorithm?

强颜欢笑 提交于 2019-11-28 11:17:57
I'm working on a small drawing application in Java. I'm trying to create a 'bucket-fill' tool by implementing the Flood Fill algorithm. I tried using a recursion implementation, but it was problematic. Anyway, I searched around the web and it seems that for this purpose, a non-recursive implementation of this algorithm is recommended. So I ask you: Could you describe a non-recursive implementation of the Flood Fill algorithm ? An actual code example, some pseudo-code, or even a general explanation will all be welcome. I'm looking for simplest, or the most efficient implementation you can think