The specification states:
Each node in the tree has all the values ββas children of the domain, except for those that have already been covered in it or its parents.
A bit unclear, but I assume that covered by him or his parents means that a node with a value of x allowed if the value of x not in the path from node to root. In this case, the tree can be constructed in this way (Haskell language):
import List data Tree = Tree String [Tree] build x xs = Tree x children where children = map (\x -> build x (delete x xs)) xs
For example, taking into account the root value "a" and the list of domain values ["b", "c", "d"] , the program creates the root directory node with the value "a" and 3 children recursively constructed from:
- root value
"b" and domain ["c", "d"] , - root value
"c" and domain ["b", "d"] , - and the root value of
"d" and the domain ["b", "c"] .
In pseudo python, this is the Haskell program algorithm:
def build(root_value, domain): node = Tree(root_value)
Here is a test of the build function that prints a tree of sample question and example above:
pretty level (Tree x children) = do mapM_ putStr [indent, x, "\n"] mapM_ (pretty (level + 3)) children where indent = replicate level ' ' main = do putStrLn "Tree for a -> b, c:" pretty 0 (build "a" ["b", "c"]) putStrLn "\nTree for a -> b, c, d:" pretty 0 (build "a" ["b", "c", "d"])
The test uses indentation to show the depth of each node in the tree:
Tree for a -> b, c: a b c c b Tree for a -> b, c, d: a b c d d c c b d d b d b c c b