Skip to content

Instantly share code, notes, and snippets.

@bruceoutdoors
Created March 8, 2015 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bruceoutdoors/f983a9ce589bb6393065 to your computer and use it in GitHub Desktop.
Save bruceoutdoors/f983a9ce589bb6393065 to your computer and use it in GitHub Desktop.
Table To Tree Algorithm Pseudocode
TABLE TO TREE ALGORITHM PSEUDOCODE
The algorithm can traverse and read an infinite number of cells of an
infinite number of depth, as it reads the cells in a recursive fashion
with the base case as the leaf of the cells. Note that you should not
think this will work for graphs (there's simply no way you could
represent graphs in a table structure anyway).
Some Notes:
- A cell is either a leaf or a branch.
- A branch whose parent branch is null is the root.
- It is assumed any cell you place in PROCESS CELL is not an empty cell.
- Eventually all PROCESS BRANCH calls will return a cell from PROCESS
LEAF, after it has processed its own leafs; this cell is the cell
to the bottom of branch cell (in the same column) after all its leaf
cells.
Start with a call PROCESS CELL(root branch, starting cell). The root
branch will contain the entire tree structure. The PROCESS CELL call
will return the cell right below the tree structure (same column as
the root cell).
PROCESS CELL(branch, cell):
1. Set var: returned cell
2. If IS LEAF(cell),
- call PROCESS LEAF(branch, cell) -> returned cell.
Else:
~ it is a branch
- call PROCESS BRANCH(branch, cell) -> returned cell.
3. If returned cell's column == root cell's column,
~ finished processing the whole tree!
@Return returned cell
4. If returned cell is empty,
~ no more cells to process for this branch
@Return cell to the left of that returned cell.
Else:
~ there's still a cell to process
@Return PROCESS CELL(branch, returned cell).
PROCESS LEAF(branch, cell):
add itself to its branch
@Return the cell to its bottom.
PROCESS BRANCH(branch, cell):
add itself to its branch
@Return PROCESS CELL(itself, bottom right cell).
~ just check leaf or branch, and assumes cell is not empty
IS LEAF(cell):
If the bottom right cell is empty,
@Return true
Else:
@Return false.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment