Created
April 12, 2012 19:28
-
-
Save ianbarber/2370348 to your computer and use it in GitHub Desktop.
Binary tree validator thingy
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
(defn is-valid? | |
[node left-val right-val] | |
(if (= node nil) | |
true | |
(if | |
(and | |
(> (:key node) left-val) | |
(<= (:key node) right-val) | |
(is-valid? (:left node) left-val (:key node)) | |
(is-valid? (:right node) (:key node) right-val) | |
) | |
true | |
false | |
) | |
)) | |
(def badtree | |
{:key 3 | |
:left {:key 2 :left {:key 1 :left nil :right nil} :right {:key 5 :left nil :right nil}} | |
:right {:key 6 :left {:key 4 :left nil :right nil} :right {:key 7 :left nil :right nil}} } | |
) | |
(def goodtree | |
{:key 4 | |
:left {:key 2 :left {:key 1 :left nil :right nil} :right {:key 3 :left nil :right nil}} | |
:right {:key 6 :left {:key 5 :left nil :right nil} :right {:key 7 :left nil :right nil}} } | |
) | |
(is-valid? badtree 0 999) | |
(is-valid? goodtree 0 999) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment