Last active
August 3, 2017 02:11
-
-
Save orend/f7ea6dab5328d23de2bdcd031a459096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns try) | |
;; find the height of a tree | |
;; try> (height nil) | |
;; 0 | |
;; try> (height {:L {:R {:L {:L nil} :R nil}}}) | |
;; 4 | |
(defn height-simple [t] | |
(if (nil? t) | |
0 | |
(-> (max (-> t :L height-simple) | |
(-> t :R height-simple)) | |
inc))) | |
(defn height [t] | |
(if (nil? t) | |
0 | |
(-> (reduce max (map height (-> t | |
(select-keys [:L :R]) | |
vals))) | |
inc))) | |
(defn height-destruct [{:keys [L R] :as t}] | |
(if (nil? t) | |
0 | |
(-> (max (height-destruct L) | |
(height-destruct R)) | |
inc))) | |
(defn height-red [{:keys [L R] :as t}] | |
(if (nil? t) | |
0 | |
(->> (map height-red [L R]) | |
(reduce max) | |
inc))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment