From 9e6354db4325781946680234d944df34668c5e7e Mon Sep 17 00:00:00 2001 From: mehbark Date: Sat, 28 Dec 2024 15:05:43 -0500 Subject: [PATCH] binary tree levels bad --- bintree-levels.lisp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 bintree-levels.lisp diff --git a/bintree-levels.lisp b/bintree-levels.lisp new file mode 100644 index 0000000..d898d04 --- /dev/null +++ b/bintree-levels.lisp @@ -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)))