Tree representation using list in python -


for tree,

enter image description here

as beginner, below list representation,

tree = [           [               [                  [                    [], 3, []                  ],                   10,                  [                    [], 17, []                  ]               ],               25,               [                  [                    [], 30, []                  ],                  32,                  [                    [], 38, []                  ]               ]           ],           40,           [              [                [], 50, []              ],               78,              [                [], 93, []              ]           ]        ] 

is representation correct using python list?

can avoid empty list [] in representation?

you representation makes lot of sense since uses 2 simple rules. tree list can be:

[]                          #empty tree [list, number, list]        #non-empty tree 

this simplicity makes easy write code , treat things in uniform way (and without lot of if statements).

another equally simple representation use tuples instead of lists and/or use none empty tree.

another representation put numbers in leaf nodes directly in parent node (e.g. code subtree under 10 [3, 10, 17]). more compact , gets rid of empty lists logic more complex, , tree represented 5 different ways:

[] [list, number, list] [list, number, number] [number, number, list] [number, number, number] 

the representation being more complex means need write more code.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -