Skip to content

Fair Tree Enumeration? #2483

Closed Answered by triska
jjtolton asked this question in Q&A
Aug 14, 2024 · 3 comments · 10 replies
Discussion options

You must be logged in to vote

With the following building block, we can fairly enumerate the trees with iterative deepening, by constraining the length of the list of nodes:

nodes(nil) --> [].
nodes(node(Node,L,R)) --> [Node], nodes(L), nodes(R).

Yielding:

?- length(Ls, _), phrase(nodes(Tree), Ls).
   Ls = [], Tree = nil
;  Ls = [_A], Tree = node(_A,nil,nil)
;  Ls = [_A,_B], Tree = node(_A,nil,node(_B,nil,nil))
;  Ls = [_A,_B], Tree = node(_A,node(_B,nil,nil),nil)
;  ... .
?- length(Ls, _), phrase(nodes(Tree), Ls), num_leaves(Tree, N).
   Ls = [], Tree = nil, N = 1
;  Ls = [_A], Tree = node(_A,nil,nil), N = 2
;  Ls = [_A,_B], Tree = node(_A,nil,node(_B,nil,nil)), N = 3
;  Ls = [_A,_B], Tree = node(_A,node(_B,nil,nil)…

Replies: 3 comments 10 replies

Comment options

You must be logged in to vote
9 replies
@jjtolton
Comment options

@jjtolton
Comment options

@bakaq
Comment options

@jjtolton
Comment options

@jjtolton
Comment options

Answer selected by jjtolton
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@jjtolton
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants