Implementation of breadth first traversal