Created
June 1, 2016 01:18
-
-
Save alexandergunnarson/a559ef53defeb1354229de3d0dc4a978 to your computer and use it in GitHub Desktop.
An immutable trie idea
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 alexandergunnarson.immutable-trie) | |
(defrecord TrieNode | |
[val complete? a b c d e f g h i j k l m n o p q r s t u v w x y z]) | |
(defn node | |
([val] (node val false)) | |
([val complete?] | |
(TrieNode. val complete? | |
nil nil nil nil nil | |
nil nil nil nil nil | |
nil nil nil nil nil | |
nil nil nil nil nil | |
nil nil nil nil nil nil))) | |
; ti:me | |
; --:cket | |
(assoc (node \t) | |
:i (assoc (node \i) | |
:m (assoc (node \m) | |
:e (node \e true)) | |
:c (assoc (node \m) | |
:k (assoc (node \k) | |
:e (assoc (node \e) | |
:t (node \t true)))))) | |
; We could have represented the Trie using arrays, | |
; but not if we wanted to have the Trie behave immutably and | |
; be postwalkable etc., then it's not a good idea |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment