I have a hierarchically nested associative array. It looks like this:
A = {
B = {
C = {},
D = {},
},
E = {
F = {
G = {}
}
},
H = {}
}
I would like to write a function that returns the "ancestors" of each key.
So:
f("A") = {"A"}
f("B") = {"B","A"}
f("C") = {"C","B","A"}
f("G") = {"G","F","E","A"}
f("fake") = {}
I've worked out I need to use recursion, but I'm having difficulty writing the function. Could someone give me some pointers on how to write such a function?
(Please don't refer me to http://xkcd.com/138/!)
Just apply a recursive depth-first search to find your specific element and return the path.
Recursive steps to construct path for element X.
X: return {X}If current element is not X:
nil.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With