The specification says:
Every node in the tree has all the values as children from the domain apart from the ones that are already covered in it or its parents
It is a little unclear, but I am assuming that covered in it or its parents means that a node with value x is allowed if the value x is not on the path from the node to the root. In that case the tree can be constructed like this (the language is Haskell):
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, given a root value "a" and a list of domain values ["b", "c", "d"], the program constructs a root node with value "a" and 3 children recursively constructed from:
- the root value
"b" and the domain ["c", "d"],
- the root value
"c" and the domain ["b", "d"],
- and the root value
"d" and the domain ["b", "c"].
In pseudo-Python this is the algorithm of the Haskell program:
def build(root_value, domain):
node = Tree(root_value)
# For each value in the domain:
for i in range(len(domain)):
child_root = domain[i]
# The child domain is equal to the original domain
# with value at position 'i' removed.
child_domain = domain[: i] + domain[i + 1:]
# Recursively build the child
child = build(child_root, child_domain)
# - and add the child to the node.
node.add_child(child)
return node
Here is a test of the build function that prints the tree of the example of the question and the 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
c->b, as I understand your question,bis already covered in the parent nodes, and as such, shouldn't have any children. If you drew a diagram of the tree structure it would make a lot more sense. Even if you just draw it by hand on paper, and take a picture with your phone, it would be clearer.b -> c -> b