this is from Princeton algs4