How do you make a binary search tree in Clojure?

前端 未结 3 2086
傲寒
傲寒 2021-02-08 10:04

In Scheme, I can use define-struct to make a binary search tree, but how do you do it in Clojure?

3条回答
  •  一生所求
    2021-02-08 10:26

    You can use structmaps. To define one:

    (defstruct bintree :left :right :key)
    

    To make an instance:

    (struct-map bintree :left nil :right nil :key 0)
    

    You can then access the values in the struct like this:

    (:left tree)
    

    etc.

    Or you can create new accessor functions:

    (def left-branch (accessor bintree :left))
    

    and use it:

    (left-branch tree)
    

提交回复
热议问题