How would you encode this structure in python? how in R?
Lists
Lists are a superset of all data structures
allow multiple data formats
allow items that are lists
Lists are python’s basic data form:
[4, [2, [10, 13, [5, 6]], 7]]
(needs convention of [ starts children nodes and ] finishes list of children)
In R there are list objects list()
Tree in R
tree <-list(value =4, children =c(list(value =2, children =c(list(value =10, children =NULL), list(value =13, children =c(list(value =5, children =NULL),list(value =6, children =NULL))))), list(value =7, children =NULL)))str(tree)
List of 2
$ value : num 4
$ children:List of 4
..$ value : num 2
..$ children:List of 4
.. ..$ value : num 10
.. ..$ children: NULL
.. ..$ value : num 13
.. ..$ children:List of 4
.. .. ..$ value : num 5
.. .. ..$ children: NULL
.. .. ..$ value : num 6
.. .. ..$ children: NULL
..$ value : num 7
..$ children: NULL
Trees
there are no native ‘tree’ objects in R or python (there are packages for sure), i.e. no standard implementation for trees