preferred approach to producing an indented table of contents is to redesign top-down recursion that includes the current depth as an additional parameter such an implementation is provided in code fragment this implementation runs in worst-case (ntime (excepttechnicallythe time it takes to print strings of increasing lengths def preorder indent(tpd) """print preorder representation of subtree of rooted at at depth ""str( element))use depth for indentation print( for in children( )child depth is + preorder indent(tcd+ code fragment efficient recursion for printing indented version of preorder traversal on complete tree the recursion should be started with form preorder indent(tt root) in the example of figure we were fortunate in that the numbering was embedded within the elements of the tree more generallywe might be interested in using preorder traversal to display the structure of treewith indentation and also explicit numbering that was not present in the tree for examplewe might display the tree from figure beginning aselectronics 'us & sales domestic international canada america this is more challengingbecause the numbers used as labels are implicit in the structure of the tree label depends on the index of each positionrelative to its siblingsalong the path from the root to the current position to accomplish the taskwe add representation of that path as an additional parameter to the recursive signature specificallywe use list of zero-indexed numbersone for each position along the downward pathother than the root (we convert those numbers to oneindexed form when printing at the implementation levelwe wish to avoid the inefficiency of duplicating such lists when sending new parameter from one level of the recursion to the next standard solution is to share the same list instance throughout the recursion at one level of the recursiona new entry is temporarily added to the end of the list before making further recursive calls in order to "leave no trace,that same block of code must remove the extraneous entry from the list before completing its task an implementation based on this approach is given in code fragment