binary tree levels

bad
This commit is contained in:
mehbark 2024-12-28 15:05:43 -05:00
parent 468cfad8b4
commit 9e6354db43

36
bintree-levels.lisp Normal file
View file

@ -0,0 +1,36 @@
(defstruct node
data
(l nil :type (or null node))
(r nil :type (or null node)))
(defun node (data &optional l r)
(make-node :data data :l l :r r))
;; 1
;; / \
;; 2 3
;; / \
;; 4 5
(defparameter example
(node 1
(node 2
(node 4)
(node 5))
(node 3)))
(defun maplonger (f alist blist)
(loop for a = alist then (cdr a)
for b = blist then (cdr b)
while (or (car a) (car b))
collecting (funcall f (car a) (car b))))
(defun levels (node)
(if node
(with-slots (data l r) node
(cons (list data)
(maplonger #'nconc (levels l) (levels r))))
nil))
(defun print-levels (node)
(loop for level in (levels node)
do (format t "~{~a~^ ~}~%" level)))