Tree traversal: Depth-first search

4

Write the shortest code that traverses a tree, depth-first.

Input

Any programmatical structure found in your language of choice: lists, tuples, queues etc.

Output

A list of "names" of the nodes in correct order as shown in Depth-first search article at Wikipedia.

Yasir Arsanukaev

Posted 2011-02-06T13:21:34.407

Reputation: 221

Requirement: A node should be able to contain the infinite number of child nodes. – Yasir Arsanukaev – 2011-02-06T13:40:28.370

Also, you're not required to actually print the result, just returning the correct result with the ability to use it in your program will do; I didn't really print mine below, the function simply returned a list.

– Yasir Arsanukaev – 2011-02-06T15:01:26.037

Answers

2

Haskell — 70 characters

data N a=N{v::a,c::[N a]}
d n=f[n][]
f(x:y)=f(c x++y).(++[v x])
f _=id

In a more readable format:

data Node a = Node {value::a, children::[Node a]}

dfs :: Node a -> [a]
dfs node = dfs' [node] []
  where dfs' [] result = result
        dfs' (x:xs) result = dfs' (children x ++ xs) (result ++ [value x])

Sample tree:

sampleTree = 
    Node 1 [Node 2 [Node 3 [Node 4 [],
                            Node 5 []], 
                    Node 6 []],
            Node 7 [], 
            Node 8 [Node 9 [Node 10 [],
                            Node 11 []],
                    Node 12 []]]

Sample run:

*Main> dfs sampleTree
[1,2,3,4,5,6,7,8,9,10,11,12]

Yasir Arsanukaev

Posted 2011-02-06T13:21:34.407

Reputation: 221

2

Perl, 34

Perl 5.10 or later, run with perl -E'code here':

sub f{ref()?f($_):say for@{$_[0]}}

For example, running f [1,[2,[3,[4],[5]],[6]],[7],[8,[9,[10],[11]],[12]]] produces:

1
2
3
4
5
6
7
8
9
10
11
12

J B

Posted 2011-02-06T13:21:34.407

Reputation: 9 638

It's Perl 5.10 (and I should have put it in the answer as I usually do), you can either add -M5.010 to the command line, run as -E 'code here', or insert a use 5.010; header do the .pl file. – J B – 2011-02-06T15:24:51.400

@Yasir I read the comment (now), but do consider it more convenient to me to print the result instead of returning it. – J B – 2011-02-06T15:26:19.687

For older Perl, replace say with print"$_\n". Soooo longer. – J B – 2011-02-06T15:32:05.370

Yep, it works. I hurry voted your question twice. You may edit your answer to add notes about Perl version, or even shorter variant of your code in the future, so that I'll be able to vote up ;-)

– Yasir Arsanukaev – 2011-02-06T15:55:08.337

1

Haskell, 35 bytes

data D a=T[D a]a
d(T l n)=n:(d=<<l)

Try it online!

data D a=T[D a]a defines a labelled tree data structure. Constructor T takes a list of sub-trees and same value of an arbitrary type a (which however must be consistent throughout each tree).

Here is an example of a tree labelled with integers and the way this tree can be defined using the data structure:

   4
 / | \
1  5  2
  / \
 3   0

tree = T[T[]1, T[T[]3, T[]0]5, T[]2]4

The function d takes such a tree and returns a list of the tree's labels in depth-first order: Calling d tree yields [4,1,5,3,0,2].

Laikoni

Posted 2011-02-06T13:21:34.407

Reputation: 23 676

1

PHP - 57 characters

<?function s($t){echo$t[0].'
';for(;$T=next($t);s($T));}

Test:

s(json_decode('[1,[2,[3,[4],[5]],[6]],[7],[8,[9,[10],[11]],[12]]]'));

Output:

1
2
3
4
5
6
7
8
9
10
11
12

Arnaud Le Blanc

Posted 2011-02-06T13:21:34.407

Reputation: 2 286

1

Ruby - 37 35 31 25 characters

s=->t{p t.shift;t.map &s}

Test:

s[[1,[2,[3,[4],[5]],[6]],[7],[8,[9,[10],[11]],[12]]]]

Arnaud Le Blanc

Posted 2011-02-06T13:21:34.407

Reputation: 2 286

1Save 1 by using 'map' instead of 'each' – AShelly – 2011-02-06T20:30:13.953