Table To Tree Algorithm

I was faced with a little riddle (kinda like some of those UVa questions) – Can I design an algorithm that can take an excel sheet like this:

2015-03-08 20_16_36-TableToTree.ods - LibreOffice Calc

And create a tree structure that a computer program can understand? I can’t assume that the number of rows and columns will be fixed. The algorithm should cater for tree with large number of branches and leaves (row count), of a practically limitless depth (column count).

I should not assume also that the first cell will be the root of the tree. Should I not be able to set any cell that is a parent as a root?

For a Saturday the algorithm simmered in my head as I was hanging out with friends. When I came home I lay down the pseudo code. By Sunday night after dinner I taken the algorithm to its completed implementation in Javascript in TestComplete 10.

Here’s the pseudocode:

What kind of pseudocode is this you ask? I don’t know myself. I didn’t use any reference to write it; just thought it looked very clear what the algorithm should be doing having written this way.

TestComplete script (JScript):

Note that you shouldn’t use the “super” keyword in javascript as it is reserved. I simply used “Super” instead. This is may be bad practice as it may confuse people a bit – just an FYI for the reader.

Here’s the output from TestComplete:

2015-03-08 20_58_08-TestComplete - C__Users_Lee_Documents_TestComplete 10 Projects_TestProject1_Test

Download the excel file I use to test it (*.xlsx): TableToTree



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.