CS代写 When run, your programs (in all languages other than Prolog) should read a

When run, your programs (in all languages other than Prolog) should read a single integer n from standard input, and then output the appropriate trees to standard output (in parenthesized form), one per line,
in arbitrary order. For Prolog, please arrange for trees (n, L) to produce successive trees (values for L) in response to a semicolon prompt.

openParen(RemainingNodes, Depth, OldTree) :- append(OldTree, [‘(‘], NewTree),

Copyright By PowCoder代写 加微信 powcoder

R is RemainingNodes – 1,
D is Depth + 1,
buildTree(R, D, NewTree).

closeParen(RemainingNodes, Depth, OldTree) :- append(OldTree, [‘)’], NewTree),
D is Depth – 1,
buildTree(RemainingNodes, D, NewTree).

depthCheck(RemainingNodes, Depth, TempTree) :- Depth > 1, closeParen(RemainingNodes, Depth, TempTree).
depthCheck(_, Depth, _) :- Depth =< 1. printTree([]). printTree([R|D]) :- write(R), printTree(D). buildTree(RemainingNodes, Depth, TempTree) :- RemainingNodes is 0, Depth is 0, printTree(TempTree), nl, !. buildTree(RemainingNodes, Depth, TempTree) :- RemainingNodes is 0, Depth > 0, closeParen(RemainingNodes, Depth, TempTree), !.
buildTree(RemainingNodes, Depth, TempTree) :- openParen(RemainingNodes, Depth, TempTree), depthCheck(RemainingNodes, Depth, TempTree), !.

write(‘please enter your input nodes:’),
read(Number),
buildTree(Number, 0, []).

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com