Python Forum
Convert tree to list of lists
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert tree to list of lists
#1
I have a tree structure which I need to convert to a list of lists.
The class for a node is defined as (irrelevant detail removed):
class TMoveTree:
   # The nodes of the valid moves tree
   def __init__(self, parent, data):
       self._parent = parent
       self._children = []
       self._data = data # a tuple (start, target) 
The tree represents the valid moves for a board game. The game has three dice, so the maximum depth of the tree is 4 (1 level for the root node, and 1 for each die,) and each node can have up to 15 children. After generating and pruning the tree, I have a tree like below:

ROOT
|_____ (0, 3)
         |_______ (0, 4)
         |          |_____ (0, 5)
         |          |_____ (3, 8)
         |          |_____ (4, 9)
         |
         |_______ (3, 7)
                     |_____ (0, 5)
                     |_____ (7, 12)

Each node contains a move in the form of a tuple representing the start and end board positions for a game piece  (apart from ROOT, which has (-1,-1).) I need to convert this tree into a list of lists as follows:
  • Each branch should be a list of the tuples in that branch, ordered from leaf to root
  • The lists generated from each branch are collected into a list, the order is irrelevant
For the tree above, this should be:

[
[(-1,-1), (0,3), (0,4), (0,5)]
[(-1,-1), (0,3), (0,4), (3,8)]
[(-1,-1), (0,3), (0,4), (4,9)]
[(-1,-1), (0,3), (3,7), (0,5)]
[(-1,-1), (0,3), (3,7), (7,12)]
]

I have the following function so far:
def tree2list(self):
       if len(self._children) == 0:
           return [self._data]
       else:
           node_list = [[self._data] + child.tree2list() for child in self._children]
           return node_list
Which outputs:
Output:
[[(-1, -1), [(0, 3), [(0, 4), (0, 5)], [(0, 4), (3, 8)], [(0, 4), (4, 9)]], [(0, 3), [(3, 7), (0, 5)], [(3, 7), (7, 12)]]]]
Reformatted for clarity:

[inline][
[(-1, -1), [(0, 3), [(0, 4), (0, 5)],
                    [(0, 4), (3, 8)],
                    [(0, 4), (4, 9)]
           ],
           [(0, 3), [(3, 7), (0, 5)],
                    [(3, 7), (7, 12)]
           ]
]
][/inline]

How can I get my 'tree2list' function to output the desired list of lists?

Thanks for any help

Dylan
Reply
#2
You need to pass the data down the recursion and use it at the end. Something like this:

def tree2list(self, prev_data = []):
    if self._children:
        child_data = []
        for child in self.children:
            child_data.extend(child.tree2list(prev_data + self._data))
        return child_data
    else:
        return [prev_data + self._data]
Untested code. Use at your own risk.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you!
I'll put that to the test and update the post with results.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,556 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  List all possibilities of a nested-list by flattened lists sparkt 1 921 Feb-23-2023, 02:21 PM
Last Post: sparkt
  convert string to float in list jacklee26 6 1,914 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  user input values into list of lists tauros73 3 1,073 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  convert a list to links Pir8Radio 3 1,099 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  returning a List of Lists nafshar 3 1,068 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  convert this List Comprehensions to loop jacklee26 8 1,512 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,638 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  Convert list to interger Clives 5 1,634 May-09-2022, 12:53 PM
Last Post: deanhystad
  How to efficiently average same entries of lists in a list xquad 5 2,124 Dec-17-2021, 04:44 PM
Last Post: xquad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020