0 votes
1 view
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.4k points)

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

    print("%s%s" % (pre, node.name))


├── 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 (107k 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"

Welcome to Intellipaat Community. Get your technical queries answered by top developers !