0

I need to do a program in prolog which prints out a list(it has to be a list) of all the nodes of a tree on a level N given. I just can't manage to put the nodes into a list. Someone told me to use the function findall but it still won't work. I would like to do not change the predicate levelNodes but a solution without findall will be good to.

domains
element=integer
treetype=tree(element,treetype,treetype);nil
list=element*
predicates
create_tree(element,treetype)
insert_left(treetype,treetype,treetype)
insert_right(treetype,treetype,treetype)
levelNode(treetype,element)
levelNodes(element,treetype,element)
goal
create_tree(10,Ten),
create_tree(11,Eleven),
create_tree(3,Three),
create_tree(5,Five),
create_tree(8,Eight),
create_tree(4,Four),
create_tree(1,One),

insert_left(Ten,Eleven,Eleven1),
insert_right(Three,Eleven1,Eleven2),
insert_left(Five,Eight,Eight1),
insert_right(Four,Eight1,Eight2),
insert_left(Eleven2,One,One1),
insert_right(Eight2,One1,FinalTree),
levelNode(FinalTree,1),
nl.
clauses
levelNode(tree(I,L,R),N):-
    findall(X,levelNodes(X,tree(I,L,R),N),O),
    write(O).
levelNodes(X,tree(I,_,_),0):-
    X=I,
    !.
levelNodes(X,tree(I,L,R),N):-
    N>0,
    N2=N-1,
    levelNodes(X,L,N2),
    levelNodes(X,R,N2). 
create_tree(A,tree(A,nil,nil)).

insert_left(X,tree(A,_,B),tree(A,X,B)).

insert_right(X,tree(A,B,_),tree(A,B,X)).
Ioana
  • 7
  • 4

1 Answers1

0

If i rephrase the assignment i get:

  • start a recursive visit, pass down the Level rightly initialized,
  • if Level is 0 -> stop recursion ('return' the data, i use head' last argument),
  • decrement Level before recursion, and cons the data returned.

Here a sketch, complete with the recursive calls and the consing.

levelNode(tree(N,_,_), 0, [N]).
levelNode(tree(_,L,R), Level, Ns) :-
    Level > 0,
    Sub is Level - 1, % maybe you'll need Sub=Level-1
    % recurse left, ...

OT Note that recursive data structures in Prolog are very easy to describe, and an important component in problem modelling. Consider instead of

create_tree(10,Ten),
create_tree(11,Eleven),
create_tree(3,Three),
create_tree(5,Five),
create_tree(8,Eight),
create_tree(4,Four),
create_tree(1,One),

insert_left(Ten,Eleven,Eleven1),
insert_right(Three,Eleven1,Eleven2),
insert_left(Five,Eight,Eight1),
insert_right(Four,Eight1,Eight2),
insert_left(Eleven2,One,One1),
insert_right(Eight2,One1,FinalTree),
levelNode(FinalTree, 1, Nodes),
...

After such rather hard to read code we have FinalTree = tree(1, tree(11, tree(10, nil, nil), tree(3, nil, nil)), tree(8, tree(5, nil, nil), tree(4, nil, nil))).

Switching to this other (equivalent) representation make it more readable, don't you think?

...
Tree = t(1, t(11, t(10, t, t), t(3, t, t)), t(8, t(5, t, t), t(4, t, t))),
levelNode(Tree, 1, Nodes),
...

edit the code is

levelNode(t(N,_,_), 0, [N]).
levelNode(t(_,L,R), Level, Ns) :-
    Level > 0,
    Sub is Level - 1, % maybe you'll need Sub=Level-1
    levelNode(L, Sub, A),
    levelNode(R, Sub, B),
    append(A, B, Ns).

test:

?- T=t(1,t(2,t,t),t(3,t,t)),levelNode(T,1,L).
T = t(1, t(2, t, t), t(3, t, t)),
L = [2, 3] ;
false.
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Regarding the representation of the tree yes it is more readable the representation you have given. But regarding the recursion given it doesn't work. I have already tried it. It works only if the level is 0(the level of the root), for anything else it doesn't do the list at all. – Ioana Nov 07 '12 at 22:07
  • of course we need to use t instead of tree in levelNode/3. Did you completed the sketch? need more help? – CapelliC Nov 07 '12 at 22:24
  • yes.How do we add a node to the list of already existing nodes? I don't understand where you do that in the sketch that you wrote. – Ioana Nov 07 '12 at 22:33
  • when you have the data, after recursing on Left and Right, append/3 them. Then 'return' the result (the third argument of append/3). If you need, i'll post the entire code.... – CapelliC Nov 07 '12 at 23:01
  • I think I understand what you explained but I'm not entirely sure. It will be very helpful if you could post the entire code. – Ioana Nov 07 '12 at 23:26