In the introduction to the Roadmapping project we created a rudimentary procedure and class that read in our data and created the basic structure we will need to produce generalized road-maps from simple tabular data.
The next step is actually the key activity, and the reason why recursion is a necessary technique for this project.

Assigning parents to each data row

Each data row has as ID and Target as below
We need to create this structure from it.

For example Vespa is the parent of both Skates and Bike, and is the child of Harley. So far we have assigned all items to be children of Frame. Our class cShapeContainer has now been enhanced to allow this capability, and our Roadmapper program has been updated to make it happen. Replace your current code with the code at the bottom of the page and lets see how this works.

Within our dotheMap procedure, once the data is read in – we have to execute a new method to ‘rearrange our data items from the current flat structure, to a hierarchical one.
    ‘ sort out the parent/child relationships
That is split into 2 parts – first find all the the items who have the current item as their target and make the link, then break the link with the frame.

Recursion beginnings

Here we are seeing the first recursion execution. Our debug report needs to call itself since the data structure now has an unknown depth of parent/child relationships. This is typical structure of a recursive procedure. Think of it like this – First deal with me, then call myself for each of my children. 
Sometimes you need to do it the other way round – deal with the children first – thats very easy, just reverse the order . It really is that simple.
Here is our debug report that will now recurse since we have organized our data
…and here is the output – see if you can relate the order it comes out in to the Deal with me, then my children recursion order.

Next steps

Next we are going to start plotting and creating shapes. But before we can do that we need to be able to calculate things like the height of the shape, which will be dependent on how many children an item has, and how many grandchildren etc. etc. However before even we can do that, one of the key things we are going to need is a way to provide parameter information such as colors, size and so on. Luckily, the cDataSet set of classes has a very straightforward capability for collecting parameters, so read on and we’ll see how to get parameter values into our program.

Version 2: Full updated code – replace your current project with this code

cShapeContainer – Version 2
Roadmapper Module – Version 2
For help and more information join our community,  follow the blog or follow me on twitter.