def build_process_trees(processes):
    """
    Build a nested dictionary, based on the relationships between processes.

    Args:
        processes: array of the currently-running processes

    Returns:
        trees: a multi-level dictionary.

    The keys are the ids of processes that "may" have children
    Values are arrays which hold the children (and children of children)

    Empty arrays indicate that the process has no children
    """
    trees, seen_ppids = {}, {}

    for row in processes:

        pid = row['pid']
        ppid = row['ppid']
        command = row['command']

        seen_ppids[pid] = 1

        if ppid == 0 or ppid not in seen_ppids:
            trees[pid] = []
        else:
            # Describe the child and find its parent process (ppid)
            new_process = {'pid': pid, 'command': command}
            try:
                trees[ppid].append([new_process])
            except KeyError:
                for root in trees:
                    for node in trees[root]:
                        # node should be an array holding a dict
                        for counter, process in enumerate(node, start=1):
                            if ppid == process['pid']:
                             # Success. We found the parent of the child process
                             # Append child to parent to indicate relationship
                                node[counter:0] = [new_process]
    # End of for loop
    return trees