Tree to list transform

Thought I'd start the day with a kata to warm up. It's an in-place transform of a binary tree to a doubly linked list using a pre-order depth first traversal. Quite a mouthful but pretty straightforward. I gave this as an interview question the other day, although I'm always a bit worried that a question like this is super biased in favour of CompSci heavy people. Although you need some sort of filter I guess.

 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
def print_tree(node):
    if not node:
        return
    print node.value
    print_tree(node.left)
    print_tree(node.right)

def print_list(node, forwards=True):
    while node is not None:
        print node.value
        node = node.right if forwards else node.left

def transform_to_list(node, prev=None):
    if not node:
        return prev
    left = node.left
    right = node.right
    node.left = prev
    if prev:
        prev.right = node
    prev = transform_to_list(left, node)
    return transform_to_list(right, prev)

class Node(object):
    def __init__(self, value=None, left=None, right=None):
        self.left = left
        self.right = right
        self.value = value

root = Node(1,
    Node(2, Node(4), Node(5)),
    Node(3, Node(6), Node(7))
)

print "As tree"
print_tree(root)
print "transforming"
tail = transform_to_list(root)
print "As list"
print_list(root)
print "Backwards"
print_list(tail, False)

Working on what you believe in

When I started Guerilla Software with my friend Aidan, it was to give us the chance to work on projects that we believe on (funded by less interesting work when required).

I think we might have found such a project in idea.ly. We're going to be spending a lot of the next few months developing this, using what we've learned on the last few projects to good use. It's a bit early to reveal the details of what the project entails, but I'm sure there are going to be a lot of great challenges writing the back-end and I'll share what I learn here.

Java and Android APIs - WTF (sometimes)

I'm actually really enjoying doing some Android development, eclipse is a lot better than I remember, and Java a bit better. Some things though I just don't get.

Like why the DatePicker has no method that returns a date, and why creating a date from a year/month/date is done like this:

1
2
3
        GregorianCalendar cal = new GregorianCalendar();
        cal.set(mStartDate.getDayOfMonth(), mStartDate.getMonth(), mStartDate.getYear());
        Date startDate = cal.getTime();

(update: I now know about jodatime, which is what I'm using)

Page 1 of 35 >>