Difference Lists for visiting trees (Prolog)

November 15th, 2006

This is are some (simple) exercises in Prolog using difference lists.
They should be correct, but I have to say I’m a beginning Prolog programmer… so…

inorder_dl(tree(X, L, R), Xs-Ys) :-
	inorder_dl(L, Xs-[X|Zs]),
	inorder_dl(R, Zs-Ys).
inorder_dl(void, Xs-Xs).

preorder_dl(tree(X, L, R), [X|Xs]-Ys) :-
	preorder_dl(L, Xs-Zs),
	preorder_dl(R, Zs-Ys).
preorder_dl(void, Xs-Xs).

postorder_dl(tree(X, L, R), Xs-Zs) :-
	postorder_dl(R, Ys-[X|Zs]),
	postorder_dl(L, Xs-Ys).
postorder_dl(void, Xs-Xs).

Prolog | Comments | Trackback

Leave a Reply

  1.  
  2.  
  3.  
  4. XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
You can keep track of new comments to this post with the comments feed.

Recent Posts

Blogroll

Siti amici

Misc

Recent Comments

Categories

Enrico Franchi graduated in Maths and Computer Science and is now studying for a Computet Science MSc (though because of italian bureaucracy that very course is to be cancelled).

RiK0's Tech Temple is using WP-Gravatar