how to tell which book node is the root node programatically
You can try to build the menu tree for your book. Book nodes do not have direct parents. The relationship is defined by the menu structure.

This will give you the structure of the book menu.

$book = $node-&gt;book;
$tree = menu_tree_all_data(book_menu_name($book['bid']));