Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
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, node.name))

Udo

├── Marc

│   └── Lian

└── Dan

    ├── Jet

    ├── Jan

    └── Joe

print(dan.children)

(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 

self.data = None

You can use it like this:

root = Tree() 

root.data = "root" 

root.left = Tree() 

root.left.data = "left" 

root.right = Tree() 

root.right.data = "right"

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...