Hello I am learning how to construct recursive functions in python.
I have been struggling for along time now on how to print out a tree that is represented lists.
The tree is represented as lists. The first element of a list is the data or value of the node, the following elements in the same list are children of the node there can be multiple children. If an element of the list is itself a list this starts a new subtree at that position.
Basically what I'm wanting to do is print a binary tree list like this with formatting that makes it easier to read.
A list like this: ['a',['b', 'c', ['d', 'i'], 'e'], 'f', ['g', 'h', ['j', 'k', 'l', 'm']]]
And print it out by level recursively so it looks like this.
a
+---b
+---c
+---d
+---i
+---e
+---f
+---g
+---h
+---j
+---k
+---l
+---m
And here's another example: ['a', ['b', 'c', 'd'], ['e', 'f'], 'g'] Prints out like this:
a
+---b
+---c
+---d
+---e
+---f
+---g
So each child appears on a line with “+---” before the child's value. This string starts directly beneath the parent value. The levels are further indented by 4 extra spaces for each level.