Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (20.3k points)

I am trying to construct a general tree. Are there any built-in data structures in Python to implement a tree?

2 Answers

0 votes
by (40.7k points)

For pre, fill, a node in RenderTree(udo) like this:

    print("%s%s" % (pre,


├── Marc

│   └── Lian

└── Dan

    ├── Jet

    ├── Jan

    └── Joe


(Node('/Udo/Dan/Jet'), Node('/Udo/Dan/Jan'), Node('/Udo/Dan/Joe'))

Features are as follows:

anytree  has also a powerful API with:

  • simple tree creation
  • simple tree modification
  • pre-order tree iteration
  • post-order tree iteration
  • resolve relative and absolute node paths
  • walking from one node to an other.
  • tree rendering (see example above)
  • node attach/detach hookups
0 votes
by (106k points)

To implement tree in Python you can use the below-mentioned code:-

class Tree:

def __init__(self):

self.left = None 

self.right = None = None

You can use it like this:

root = Tree() = "root" 

root.left = Tree() = "left" 

root.right = Tree() = "right"

Browse Categories