I have the following binary tree
A
/ \
BC
/ \
DE
is represented as a list in Lisp (A 2 B 0 C 2 D 0 E 0), where the letters are node names and the numbers are the number of child nodes (0 for none, 1 one node, 2 two nodes). I need to find the maximum from the root node to the depth of the tree leaf (the depth of the binary tree that is) recursively. I am new to Lisp and I cannot figure out how to implement it. This is what I still manage to find:
(defun depth (tree)
"Returns the depth of the argument tree."
(check-type tree list)
(if (= (second tree) 0)
0
(1+ (get-btree-max-depth (cddr tree)))))
(defun get-btree-max-depth (btree)
"Returns the maximum depth
of the argument tree. "
(check-type btree list)
(if (= (second btree) 0)
0
(max (depth (cddr btree))
(get-btree-max-depth (cddr btree)))))
but it does not work correctly. I also looked at similar posts, but I did not find anything useful that could help me. Can someone give me a suggestion to help figure this out? Thanks!
PS This is part of a small project that I will be presenting at the University, but also my own way of improving in Lisp (I saw that many similar posts had questions related to whether the message was related to homework). :)
source share