-
Notifications
You must be signed in to change notification settings - Fork 0
/
130-Tree reparenting.clj
23 lines (23 loc) · 1.11 KB
/
130-Tree reparenting.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(fn reparenting [node tree]
(letfn [(find-subtree [tree]
(let [root (nth tree 0 nil)
left (nth tree 1 nil)
right (nth tree 2 nil)]
(cond
(nil? root) nil
(= root node) tree
:else (or (find-subtree left)
(find-subtree right)))))
(get-parent [[root left right]]
(cond
(or (nil? root) (= node (first left)) (= node (first right))) root
:else (or (get-parent left) (get-parent right))))
(get-parent-tree [node [root & children]]
(if (nil? root)
nil
(concat (list root) (for [child children :when (not= node (first child))] (get-parent-tree node child)))))]
(let [p (get-parent tree)
q (get-parent-tree node tree)
t (find-subtree tree)]
(if (nil? p) tree
(concat t (list (reparenting p q)) )))))