Skip to content

Instantly share code, notes, and snippets.

@orend
Last active August 3, 2017 02:11
Show Gist options
  • Save orend/f7ea6dab5328d23de2bdcd031a459096 to your computer and use it in GitHub Desktop.
Save orend/f7ea6dab5328d23de2bdcd031a459096 to your computer and use it in GitHub Desktop.
(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