My tree has the following structure: tree={'0':('1','2','3'), '1':('4'), '2':('5','6'), '3':(), '4':('7','8'), '8':('9','10','11')}
How can I wrote Python code to retrieve all given child nodes of a particular node? For example, if I give it node 4, the code should retrieve 7,8,9,10,11. For node 2, it should retrieve 5, 6 and so on.
I just started learning the basics of Python but I have no idea how to implement this for non-binary trees..
import queue
tree={'0':('1','2','3'), '1':('4'), '2':('5','6'), '3':(), '4':('7','8'), '8':('9','10','11')}
num = input("what you want ")
q = queue.Queue()
q.put(num)
while not q.empty():
n = q.get()
for s in n:
print(s)
if s in tree:
q.put(tree[s])
Note that if you have a tree tree={'0':('1'), '1':('0')}
, or any other circular reference, this code will run forever. Be careful!