Update p2.cpp

Implemented traverse function