0

I'm struggling with printing data from a Mtree in a visual way, more specifically this mtree https://github.com/tburette/mtree

I have a distance function here:

def d_int(x, y):
    return abs(x - y)

I create and populate the MTree here

p_tree = mtree.MTree(d_int, max_node_size=2)
p_tree.add_all([1,2,6,5,0,24,19,11,80,31,32,3,4,16,71,89,9,10])

The p_tree.__dict__ looks like this: a root with two entries (stored in a set), but with no first node. Each of the two entries then contain subtrees and so on

{'d': <function _main_.d_int(x, y)>,
 (... left out data ...)
 'size': 18,
 'root': InternalNode(parent_node: None, parent_entry: None, 
  entries:[
  Entry(obj: 24, dist: None, radius: 65, subtree: 'InternalNode()'), 
  Entry(obj: 1, dist: None, radius: 10, subtree: 'InternalNode()')
  ])}

I've been trying to print the tree by recursivly looping subtrees like this, but i end up with a print that does not make sence to me.

def printTree(root, level=0):
    
    print("  " * level, root.obj)

   if(root.subtree == None): return root.obj
    
    for child in root.subtree.entries:
        printTree(child, level + 1)
printTree(p_tree.root.entries.pop()) //pop() because entries is a set

returns

0
   11
     11
       11
         11
           11
11
           10
10
   0
     3
       3
         3
           4
4
           3
3
       6
         6
           6
6
           5
5
         9
           9
9
     0
       0
         1
           1
1
           2
2
         0
           0
0

I would also like not to have to destroy my tree (pop()) in order to print it, but I see no other way, when the root-node isn't a node it self.

** So my questions: How do I print a Mtree? (more specifically this https://github.com/tburette/mtree) and how do I print a tree recursively without a topnode? **

Suhr415
  • 35
  • 6
  • 1
    Regarding `pop`: [How to retrieve an element from a set without removing it?](https://stackoverflow.com/questions/59825/how-to-retrieve-an-element-from-a-set-without-removing-it). One way is `printTree(next(iter(p_tree.root.entries)))` – Stef Sep 23 '22 at 09:05
  • Have you tried `print(p_tree.root)` directly? – Stef Sep 23 '22 at 09:13
  • `print(p_tree.root)` returns the information from the dict relation to root, namely: ` 'root': InternalNode(parent_node: None, parent_entry: None, entries:[Entry(obj: 24, dist: None, radius: 65, subtree: 'InternalNode()'), Entry(obj: 1, dist: None, radius: 10, subtree: 'InternalNode()')])} ` – Suhr415 Sep 23 '22 at 10:50
  • @Stef `printTree(next(iter(p_tree.root.entries)))` - amazing! Thank you, that's the answer to half of my trouble! – Suhr415 Sep 23 '22 at 10:52
  • 1
    Okay, how about `def tree_as_tuple(root): return (root.obj, tuple(map(tree_as_tuple, root.subtree.entries)))`; and then you print with `print(tree_as_tuple(p_tree.root))` – Stef Sep 23 '22 at 13:09
  • Then I get an error saying that "the internalNode() does not have an obj". The obj-value is placed at the objects stored in the entries-set list, so it cant be accessed without accessing the entries-set list, I think? – Suhr415 Sep 24 '22 at 05:42
  • @Stef if I do `def tree_as_tuple(root): if(root.subtree == None): return root.obj return (root.obj, tuple(map(tree_as_tuple, root.subtree.entries)))` and print with `print(tree_as_tuple(next(iter(p_tree.root.entries))))` i get the following `(24, ((24, ((80, ((89, )), (80, (71,80)))),)), (24, ((16, ((16, ((16,19)),)), (24, ((24, (24,)), 31, (23, 31)))))),)))` – Suhr415 Sep 24 '22 at 05:50
  • Yes? Is that not what you want? – Stef Sep 24 '22 at 08:38

1 Answers1

0
def tree_as_tuple(root): 
    if root.subtree == None:
        return root.obj 
    return (root.obj, tuple(map(tree_as_tuple, root.subtree.entries))) 

and print one part of the tree at a time with

print(tree_as_tuple(next(iter(p_tree.root.entries)))) 

returns the following, which is acceptable

(24, ((24, ((80, ((89, )), (80, (71,80)))),)), (24, ((16, ((16, ((16,19)),)), (24, ((24, (24,)), 31, (23, 31)))))),)))

Thanks to @Stef for the feedback

Maxim Kukhtenkov
  • 734
  • 1
  • 7
  • 20
Suhr415
  • 35
  • 6