-
Notifications
You must be signed in to change notification settings - Fork 164
/
tree.q
39 lines (29 loc) · 970 Bytes
/
tree.q
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
/ trees and graphs
/ q parse tree: nested expression (f;x;..). quote data with enlist, e.g.
e:{$[-11h=t:type x;value x;not t in 0 11h;x;1=count x;first x;value e each x]}
a:2
e(,;(+;`a;3);enlist`b)
/ nested directory: use a parent vector, e.g.
/ a
/ b
/ c
/ d
/ e
p:0N 0N 1 2 1 / parent
n:`a`b`c`d`e / name
c:group p / children
n p scan 3 / full path
/ binary tree: can use a 2 column transition matrix(then 0 t\x). e.g. huffman encode
v:"abcde" / some values with weights 1 2 3 4 5
d:v!(0 1 0;0 1 1;0 0;1 0;1 1) / huffman write tree
t:(6 7;6 7;6 7;6 7;6 7;0 1;2 5;3 4;6 7) / huffman read matrix
bd:{raze d x} / bits from data
db:{v x where count[v]>x:0 t\x} / data from bits
db bd v
/ dags http://en.wikipedia.org/wiki/Adjacency_matrix
/ adjacency matrix. connect{any x&y} shortest{min x+y}
m:(1101b;0110b;0010b;0001b)
m{any x&y}\:m
/ adjacency list
v:(0 1 3;1 2;enlist 2;enlist 3)
{distinct raze v x}each v