In Haskell, you can make filters, amounts, etc. in infinite lists in a constant space, because Haskell only creates list nodes if necessary, and garbage collects them with which it ends.
I would like this to work with endless trees.
Below is a pretty dumb program that generates an infinite binary tree with nodes representing natural numbers.
Then I wrote a function that makes the first pass through the depth of this tree, spitting out nodes at a certain level.
Then I made a quick sum on nodes shared by 5.
Theoretically, this algorithm can be implemented in the space O(n) for a tree of depth n nodes O(2^n) . Just generate the tree on the fly by deleting the nodes that you have already completed.
Haskell generates a tree on the fly, but not garbage collects nodes that it seems to be.
Below is the code, I would like to see code with a similar effect, but this does not require O(2^n) space.
import Data.List (foldl') data Tree = Tree Int Tree Tree tree n = Tree n (tree (2 * n)) (tree (2 * n + 1)) treeOne = tree 1 depthNTree nx = go nx id [] where go :: Int -> Tree -> ([Int] -> [Int]) -> [Int] -> [Int] go 0 (Tree x _ _) acc rest = acc (x:rest) go n (Tree _ left right) acc rest = t2 rest where t1 = go (n - 1) left acc t2 = go (n - 1) right t1 main = do x <- getLine print . foldl' (+) 0 . filter (\x -> x `rem` 5 == 0) $ depthNTree (read x) treeOne
source share