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
:
from collections import defaultdict def Tree(): return defaultdict(Tree)
Usage:
>>> records = Tree() >>> records['Artist']['Maria Callas']['Song']['O Mio Babbino Caro']['Year'] = 1965
bonus: Maria Callas singing O Mio Babbino Caro