Skip to content

Instantly share code, notes, and snippets.

@theleoborges
Last active December 19, 2015 16:19
Show Gist options
  • Save theleoborges/5982622 to your computer and use it in GitHub Desktop.
Save theleoborges/5982622 to your computer and use it in GitHub Desktop.
;; This is short but in case of a failure, it's harder to pinpoint which bit is wrong.
(is (= (balance tree)
[:red
[:black
[:black nil "a" nil]
"x"
[:black nil "b" nil]]
"y"
[:black
[:black nil "c" nil]
"z"
[:black nil "d" nil]]]))
;; this is a lot more verbose, though if any of the assertions is incorrect
;; it's easy to tell which bit is off
(let [balanced-zp (z/zipper vector? seq (fn [_ c] c) (balance tree))
left-child (comp z/right z/down)
right-child (comp z/right z/right z/right z/down)
value (comp z/right z/right z/down)
color z/down]
;; root
(are [x y] (= x (first y))
:red (-> balanced-zp color)
"y" (-> balanced-zp value)
:black (-> balanced-zp left-child color)
"x" (-> balanced-zp left-child value)
"a" (-> balanced-zp left-child left-child value)
"b" (-> balanced-zp left-child right-child value)
:black (-> balanced-zp right-child color)
"z" (-> balanced-zp right-child value)
"c" (-> balanced-zp right-child left-child value)
"d" (-> balanced-zp right-child right-child value)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment