varatis
varatis

Reputation: 14740

Printing a tree with branches recursively

I'm trying to print a tree recursively in Python. For some reason, the indentation isn't working (perhaps I'm too tired at this point to see an obvious flaw). Here's the structure / class definitions that I'm working with:

class Tree(object):
  def __init__(self, data):
    self.data = data
    self.branches = []

class Branch(object):
  def __init__(self, value):
    self.label = value
    self.node = None

As you see, each tree has Branches, which have a label and point to another Tree (that's the node value you see there). Here's how I'm trying to print out the tree:

  def __str__(self):
    return self.tree_string(0)

  def tree_string(self, indent):
    indentation = indent * " "
    result = indentation + str(self.data) + "\n";
    for branch in self.branches:
      result += indentation + branch.label + ": \n" + branch.node.tree_string(indent + 2)
    return result

This is giving me:

4
Somewhat: 
  Yes
Fuller: 
  3
Correct: 
  8
Caribbean: 
  2
Wrong: 
  Wrong
Correct: 
  Correct

Italian: 
  Wrong
Burger: 
  Correct

Wrong: 
  Wrong

Nothing: 
  Wrong

When it should be giving me something like

4
Somewhat: 
  Correct
Fuller: 
  3
  Correct: 
    8
    Caribbean: 
      2
      Wrong: 
        Wrong
      Correct: 
        Correct
    Italian: 
      Wrong
    Burger: 
     Correct
  Wrong: 
    Wrong
Nothing: 
  Wrong

What's causing my code to have those extra newlines and not have the proper indents?

Update

Pretty sure the data is ok. Here's a modified version that shows it's ok:

  def tree_string(self, indent):
    indentation = indent * " "
    result = str(self.data);
    if len(self.branches) > 0:
      result += "["
      for branch in self.branches:
        result += branch.label + ":" + branch.node.tree_string(indent + 2) + " "
      result += "]"
    return result

..which gives the output

4[Somewhat:Correct Fuller:3[Correct:8[Caribbean:2[No:No Correct:Correct ] Italian:Wrong Burger:Correct ] Wrong:Wrong ] Nothing:Wrong ]

However, the indent values are for some reason always 0 or 2.

Upvotes: 1

Views: 2553

Answers (1)

unutbu
unutbu

Reputation: 879361

Looks like it should work to me:

class Tree(object):
  def __init__(self, data):
    self.data = data
    self.branches = []
  def __str__(self):
    return self.tree_string(0)

  def tree_string(self, indent):
    indentation = indent * " "
    result = indentation + str(self.data) + "\n";
    for branch in self.branches:
      result += indentation + branch.label + ": \n" + branch.node.tree_string(indent + 2)
    return result

class Branch(object):
  def __init__(self, value):
    self.label = value
    self.node = None

tree = Tree(4)
b1 = Branch('Somewhat')
b1.node = Tree('Yes')
b2 = Branch('Fuller')
b2.node = Tree(3)
tree.branches = [b1, b2]
b3 = Branch('Correct')
b3.node = Tree(8)
b2.node.branches = [b3]
print(tree)

yields

4
Somewhat: 
  Yes
Fuller: 
  3
  Correct: 
    8

Upvotes: 3

Related Questions