0

Given (M, F) - With every step of a certain process, the tuple becomes either (M + F, F) or (M, M + F). So, starting with (1, 1), the following are possible:

enter image description here The algorithm I've followed while drawing the tree is -

If not root, 
left_ child = (parent0 + parent1, parent1)

and right_child = (parent0, parent0 + parent1)

where parent0 is referring to the first element of the parent tuple and parent1 second.

I am completely unsure on how I would create a tree that would follow the above algorithm for creation of the tree after n steps given any starting values to M and F.

When I decided to search online, I got something along the lines of :

class Tree(object):
    def __init__(self):
        self.left = None
        self.right = None
        self.data = None

root = Tree()
root.data = "root"
root.left = Tree()
root.left.data = "left"
root.right = Tree()
root.right.data = "right"

or

class Tree:
    def __init__(self, cargo, left=None, right=None):
        self.cargo = cargo
        self.left  = left
        self.right = right

    def __str__(self):
        return str(self.cargo)

tree = Tree(1, Tree(2), Tree(3))

I couldn't think of a way to use the above piece of code to build the state space tree I wanted to. That is, how do I implement a tree that automatically creates subtrees with the calculated values when the given input is (M, F) and n which is the number of steps in the process (i.e. the number of levels in the tree).

cs95
  • 379,657
  • 97
  • 704
  • 746
  • For anyone interested, I thought of this problem while doing the Google Foobar challenge solving the "Bomb baby" problem. It has a much simpler solution, but I realized I did not know how to make the kind of tree I wanted in python while thinking about the problem. – Apoorva Anand Jul 02 '17 at 22:18
  • I'm not sure if I understand the problem. As I see it, all you have to do is put `(M, F)` tuples into your Tree instead of numbers. Or are you trying to implement a tree that automatically creates subtrees with the calculated values? – Aran-Fey Jul 02 '17 at 22:21
  • Yes. I'm sorry if that wasn't clear. – Apoorva Anand Jul 02 '17 at 22:25
  • 1
    @ApoorvaAnand Where exactly are you stuck? Start with a wrapper function that calls a recursive helper. Pass n (the depth) and the tuple (M, F) and let the tree create itself. – cs95 Jul 02 '17 at 22:37

1 Answers1

1

All you need is a Tree that holds two values instead of just one, plus some properties that automatically generate subtrees:

class SpaceTree:
    def __init__(self, M=1, F=1):
        self.M= M
        self.F= F
        self._left= None
        self._right= None

    @property
    def left(self):
        if self._left is None:
            self._left= SpaceTree(self.M+self.F, self.F)
        return self._left

    @property
    def right(self):
        if self._right is None:
            self._right= SpaceTree(self.M, self.F+self.M)
        return self._right

    def __repr__(self):
        return 'SpaceTree({}, {})'.format(self.M, self.F)

Now you can create a tree and traverse it as far as you want:

>>> SpaceTree()
SpaceTree(1, 1)
>>> SpaceTree().left
SpaceTree(2, 1)
>>> SpaceTree().left.right
SpaceTree(2, 3)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • This is elegant and _works_, but if I'd want to create a tree to a certain depth, I'd either have to do it manually or write another function. – cs95 Jul 02 '17 at 23:03
  • @cᴏʟᴅsᴘᴇᴇᴅ Not really. There's no point creating the tree up to a certain depth if you're not going to do anything with it. And if you have code that does something with it, then the required nodes will be created automatically. – Aran-Fey Jul 02 '17 at 23:05
  • @Rawing Correct me if I am wrong/tell me if there's a better way to do this, but if I wanted the left most element on the 3rd level of the tree then I'd go : `a = SpaceTree() for whatever in range (3): a = a.left print(a)` – Apoorva Anand Jul 02 '17 at 23:22
  • @ApoorvaAnand Yes, that's how you'd do it. – Aran-Fey Jul 02 '17 at 23:26