I have the following binary tree
A / \ B C / \ D E
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 highest from root node to leaf depth of the tree (the depth of the binary tree that is) recursively. I'm pretty new to Lisp and I can't figure how to implement it. This is what I manage to come up with until now:
(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 doesn't work properly. I also browsed similar postings but I didn't find anything useful that could help me. Could somebody give me a suggestion to help figure this out? Thank you!
P.S. This is part of a small project that I will present at University but also my own way of getting better in Lisp (I saw that many similar posts had questions asking if the posting is related to homework). :)