I’m trying to get a program to return the levels of a BT in the way of a list and I’m stuck at this:
nivel(nodo(L,Root,R)) :- nivel(nodo(L,Root,R),X),writeln(X).
nivel(vacio,[]).
nivel(nodo(L,Root,R),[Root|T]) :- nivel(L,T),nivel(R,T),writeln(T).
An example input-output of what I’m trying is:
nivel(nodo(nodo(vacio,4,vacio), t, nodo(vacio,r,vacio))
X =[t]
X =[4,r]
problem is I don’t know how to make the program get the new Roots.
Any ideas? Also, thanks in advance!
Here goes a solution, it traverses the tree once an builds a list of the items in each level.
The second clause of nivel/4 is a hack due to the fact that the algorithm does not know in advance the height of the tree.
Test case: