First, notice that we're setting "ptr->parent = top". 'top' is the current node we're processing, and ptr represents the node to the right of the current node. The current node is *not* the parent of the node to the right. I believe that line should be "ptr->parent = top->parent".
I think same.
Second, we're adding the entry for the right sibling to the end of the list of nodes to visit. But when we process entries from the list, we exit immediately when we see a leaf page. That means that the right sibling can get queued up behind leaf pages, and thus never visited.
I think possible solution is to save right sibling immediatly after current page . Thus, this code fragment should looks like this: