How can I implement a tree without using classes in python? I can only use lists, dictionaries and queue. Obviously without the library bintree.
Advertisement
Answer
I usually use defaultdict
:
JavaScript
x
5
1
from collections import defaultdict
2
3
def Tree():
4
return defaultdict(Tree)
5
Usage:
JavaScript
1
3
1
>>> records = Tree()
2
>>> records['Artist']['Maria Callas']['Song']['O Mio Babbino Caro']['Year'] = 1965
3
bonus: Maria Callas singing O Mio Babbino Caro