Stevan Apter
Guest
Registered: Not Yet
Location:
Posts: N/A |
Re: Fun APL/J Programming Problem
here's the k version of roger's solution:
<(,,0),/(+k _vs!:)'_k^!m
and a more general version of the problem:
you've got an arbitrary tree in l-r-breadth-first order, and you want to convert it to l-r
depth-first order.
e.g. you've got b:
0 -1 --3 --4 -2 --5 ---8 ---9 --6 --7
and you want d:
0 -1 --2 --3 -4 --5 ---6 ---7 --8 --9
represent b thus:
f: 0 0 1 1 2 2 2 5 5
b[0], the root, has itself as parent; the value of b[i] is the parent of i.
you want:
g: 0 1 1 0 4 5 5 4 4
the function to convert b's to d's is:
ga:{*|i\x i:<|:'x\'!#x}
db b 0 1 1 0 4 5 5 4 4
tracing through the function, with b for x:
!#b / iota count b
1 2 3 4 5 6 7 8 9
b\'!#b / tricky: get all paths to the root (,0 1 0 2 0 3 1 0 4 1 0 5 2 0 6 2 0 7 2 0 8 5 2
0 9 5 2 0)
|:'b\'!#b / reverse each
(,0 1 2 1 3 1 4 2 5 2 6 2 7 2 5 8 2 5 9)
<|:'b\'!#b / grade up -> the preordering of b 1 3 4 2 5 8 9 6 7
h:<|:'b\'!#b / i gets that
i\b i / tricky: start with b i, index i with the result until b i ( 0 1 1 0 2 5 5 2 2 0 1 1 0 3 5
5 3 3 0 1 1 0 4 5 5 4 4)
*|i\b i / d = the last of that 0 1 1 0 4 5 5 4 4
"Paul Chapman" <paul@CHOPigblanCHOP.com> wrote in message [url="news:8QVT9.12323$4k6.1046479@wards"]news:8QVT9.12323$4k6.1046479@wards[/url]... The
following was posted within the last hour on comp.theory. Seems to me a perfect candidate for an APL
or J solution. My J is rusty, so I'm handing it over to you guys, and meanwhile heading for the
coffee shop to scratch out the answer.
Cheers, Paul
----- Original Message ----- From: "Martijn Faassen" <m.faassen@vet.uu.nl> Newsgroups: comp.theory
Sent: Saturday, January 11, 2003 1:31 PM [UT] Subject: tree numbering schemes
[q1]> Hi there,[/q1]
[q1]>[/q1]
[q1]> In the course of some programming I ran into the following problem.[/q1]
[q1]>[/q1]
[q1]> Imagine I have a k-ary tree of a certain depth with no missing nodes (I've heard that referred to[/q1]
[q1]> as a 'perfect k-ary tree', I think).[/q1]
[q1]>[/q1]
[q1]> I also assigned each node a number based on a level order traversal scheme. Take for instance a[/q1]
[q1]> binary tree of depth 3:[/q1]
[q1]>[/q1]
[q1]> 0[/q1]
[q1]> / \[/q1]
[q1]> 1 2[/q1]
[q1]> / \ / \[/q1]
[q1]> 3 4 5 6[/q1]
[q1]>[/q1]
[q1]> Is there a way to get from the level order numbers to the preorder numbers without traversing the[/q1]
[q1]> tree? i.e. I want to get to a tree numbered like this:[/q1]
[q1]>[/q1]
[q1]> 0[/q1]
[q1]> / \[/q1]
[q1]> 1 4[/q1]
[q1]> / \ / \[/q1]
[q1]> 2 3 5 6[/q1]
[q1]>[/q1]
[q1]> So, I'd like a function that given the level order number, the depth of the tree and k returns the[/q1]
[q1]> preorder number.[/q1]
[q1]>[/q1]
[q1]> I realize that in the case of non-perfect trees this is not possible as there is missing[/q1]
[q1]> information.. But it should be if you have a perfect tree, and in constant time, right? But I[/q1]
[q1]> can't figure it out, so I thought I'd try asking some experts. Is there any literature I can look[/q1]
[q1]> into? Anything online? I searched for a while but only found a lot of introductions to tree[/q1]
[q1]> traversal algorithms but nothing about this.[/q1]
[q1]>[/q1]
[q1]> My real problem is a actually a bit more involved as I'm not dealing with k-ary trees but trees[/q1]
[q1]> which have a different amount of child nodes on each level (but the same for each node on that[/q1]
[q1]> level). Each level has the amount of child nodes associated with it. Still I hope[/q1]
[q1]> level_order->preorder is still possible in O(n) where n is the depth of the tree.. Is that true?[/q1]
[q1]> Any suggestions?[/q1]
[q1]>[/q1]
[q1]> Thanks,[/q1]
[q1]>[/q1]
[q1]> Martijn[/q1]
Report this post to a moderator | IP: Logged
|