-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.clj
57 lines (50 loc) · 1.41 KB
/
bst.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(defn node [x y]
{:left nil :right nil :key x :val y})
(defn search [bt key]
(if (not= nil bt)
(if (= (:key bt) key)
(:val bt)
(if (> (:key bt) key)
(search (:right bt) key)
(search (:left bt) key)
)
)
-1
)
)
(defn add [bt key]
(insert bt key 1)
)
(defn insert [bt key val]
(if (not= bt nil)
(if (> key (:key bt))
{:left (:left bt) :key (:key bt) :val (:val bt) :right (insert (:right bt) key val)}
{:left (insert (:left bt) key val) :key (:key bt) :val (:val bt) :right (:right bt)}
)
(node key val)
)
)
(defn size [f]
(if (nil? f)
0
(+ 1 (+ (size (:left f)) (size (:right f))))
)
)
(defn increment [bt key]
(if (not= bt nil)
(if (= (:key bt) key)
{:left (:left bt) :key key :val (inc (:val bt)) :right (:right bt) }
(if (> key (:key bt))
{:left (:left bt) :key (:key bt) :val (:val bt) :right (increment (:right bt) key)}
{:left (increment (:left bt) key) :key (:key bt) :val (:val bt) :right (:right bt)}
)
)
)
)
(defn in-order [bt]
; (println bt)
(if (nil? bt)
nil
(concat (in-order (:left bt)) (list {:key (:key bt) :val (:val bt)}) (in-order (:right bt)))
)
)