[book code] added code from sedgewicks book