#include "cs.h"
/* depth-first search and postorder of a tree rooted at node j */
int cs_tdfs (int j, int k, int *head, const int *next, int *post, int *stack)
{
    int i, p, top = 0 ;
    if (!head || !next || !post || !stack) return (-1) ;    /* check inputs */
    stack [0] = j ;		    /* place j on the stack */
    while (top >= 0)		    /* while (stack is not empty) */
    {
	p = stack [top] ;	    /* p = top of stack */
	i = head [p] ;		    /* i = youngest child of p */
	if (i == -1)
	{
	    top-- ;		    /* p has no unordered children left */
	    post [k++] = p ;	    /* node p is the kth postordered node */
	}
	else
	{
	    head [p] = next [i] ;   /* remove i from children of p */
	    stack [++top] = i ;	    /* start dfs on child node i */
	}
    }
    return (k) ;
}
