Skip to content

Instantly share code, notes, and snippets.

@sudodoki
Last active March 6, 2017 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudodoki/7141ce03f934a47fe13ff41df51387f2 to your computer and use it in GitHub Desktop.
Save sudodoki/7141ce03f934a47fe13ff41df51387f2 to your computer and use it in GitHub Desktop.
(defn dfs
[tree]
(if (nil? tree)
nil
(concat
(dfs (.l tree))
(list (.val tree))
(dfs (.r tree)))))
(defn leftmost-in
[tree]
(cond
(nil? tree) nil
(nil? (.l tree)) (.val tree)
:else (leftmost-in (.l tree))))
(deftype TreeNode [val l r]
clojure.lang.ISeq
(first [this] (leftmost-in this))
(next [this] (drop 1 (dfs this)))
(more [this] (drop 1 (dfs this)))
(cons [this v]
(cond
(nil? this) (TreeNode. v nil nil)
(< v (.val this)) (TreeNode.
(.val this),
(.-cons (.l this) v),
(.r this))
:else (TreeNode.
(.val this),
(.l this),
(.-cons (.r this) v))))
clojure.lang.Seqable
(seq [this] (dfs this)))
(def test-tree (conj)
(TreeNode. 5
(TreeNode. 2
(TreeNode. 1 nil nil)
(TreeNode. 3 nil nil))
(TreeNode. 7
(TreeNode. 6 nil nil)
(TreeNode. 8 nil nil))))
(map println test-tree)
; 1
; 2
; 3
; 5
; 6
; 7
; 8
; (nil nil nil nil nil nil nil)
@sudodoki
Copy link
Author

sudodoki commented Mar 6, 2017

🤔 how to make into/conj work with this?

@sudodoki
Copy link
Author

sudodoki commented Mar 6, 2017

с дефтайпами значения всегда читаються из аргументов конструктора, методы протокола как бы замыкаються вокруг аргументов

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment