non-recursive

How to rewrite Ackermann function in non-recursive style?

瘦欲@ 提交于 2019-11-28 07:35:33
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? 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; } This looks like homework, so I won't give you the answer but I will lead you in the right direction: If you want to breakdown the recursion, it might

Non-recursive os.walk()

耗尽温柔 提交于 2019-11-28 07:21:59
I'm looking for a way to do a non-recursive os.walk() walk, just like os.listdir() works. But I need to return in the same way the os.walk() returns. Any idea? Thank you in advance. next(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. Kamiccolo My a bit more parametrised solution would be this: for root, dirs, files in os.walk(path): if not

Non-recursive os.walk()

假装没事ソ 提交于 2019-11-27 01:46:47
问题 I'm looking for a way to do a non-recursive os.walk() walk, just like os.listdir() works. But I need to return in the same way the os.walk() returns. Any idea? Thank you in advance. 回答1: next(os.walk(...)) 回答2: 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

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

我与影子孤独终老i 提交于 2019-11-27 00:09:43
Well, I have spent lots of time on this issue. However, I only can find solutions with non-recursive methods for a tree: Non recursive for tree , or recursive method for the graph, Recursive for graph . And lots of tutorials(I don't provide those links here) don't provide the approaches as well. Or the tutorial is totally incorrect. Please help me. Updated: It's really hard to describe: If I have a undirected graph: 1 / | \ 4 | 2 3 / 1-- 2-- 3 --1 is a cycle. At the step: push the neighbors of the popped vertex into the stack WHAT'S THE ORDER OF THE VERTEXES SHOULD BE PUSHED? If the pushed

Post order traversal of binary tree without recursion

ε祈祈猫儿з 提交于 2019-11-26 19:35:49
What is the algorithm for doing a post order traversal of a binary tree WITHOUT using recursion? 1337c0d3r Here's a link which provides two other solutions without using any visited flags. https://leetcode.com/problems/binary-tree-postorder-traversal/ This is obviously a stack-based solution due to the lack of parent pointer in the tree. (We wouldn't need a stack if there's parent pointer). We would push the root node to the stack first. While the stack is not empty, we keep pushing the left child of the node from top of stack. If the left child does not exist, we push its right child. If it's

Post order traversal of binary tree without recursion

怎甘沉沦 提交于 2019-11-26 08:58:45
问题 What is the algorithm for doing a post order traversal of a binary tree WITHOUT using recursion? 回答1: Here's a link which provides two other solutions without using any visited flags. https://leetcode.com/problems/binary-tree-postorder-traversal/ This is obviously a stack-based solution due to the lack of parent pointer in the tree. (We wouldn't need a stack if there's parent pointer). We would push the root node to the stack first. While the stack is not empty, we keep pushing the left child