WALTHAM, Mass. – Doc Rivers agreed to a five-year contract extension as Celtics coach on Friday, a deal that would keep him in Boston to help rebuild the franchise when the Big Three era is done.

“I think Doc is the best coach in the league. So it’s great for us,” General Manager Danny Ainge said Friday. “Doc wants to be here. It’s not all because he thinks that over the next five years we will have the best team in the NBA.

Rivers contract was set to expire — he had an option for next season — and he said after the Celtics were eliminated by the Miami Heat on Wednesday night that he was “leaning heavily” toward coming back. But he was expected to return on a short-term deal to make one more run at a title with the Big Three of Kevin Garnett, Paul Pierce and Ray Allen.

Instead, the five-year contract worth a reported $35 million would give him the chance to lead the franchise with a rebuilt roster that probably won’t include any of the thirtysomething stars who led Boston to its 18th NBA title — and Rivers’ first — in 2008.

Ainge promised changes, refusing to even promise that the team’s four core players — the Big Three and point guard Rajon Rondo — would be back. If they are, there could be changes in the way they’re used, with an eye toward keeping the older stars fresh for the playoffs.

And the roster will be different, he said, acknowledging that the team got little help from the bench in the postseason — especially on offense.

“There were too many scoring droughts at crucial times in games,” Ainge said. “We need to improve our team to have a crack at it next year.”

AINGE AGAIN defended the midseason trade that sent Kendrick Perkins to Oklahoma City for Nenad Krstic and Jeff Green. Neither of the players Boston got were productive in the playoffs, but Ainge said Krstic was recovering from bruises in both knees and Green might have needed more time to adjust to his role.

AINGE SAID he would like to re-sign Glen “Big Baby” Davis, a free agent, but Davis has indicated he wants to go somewhere and be a star — or at least a starter.

JERMAINE O’NEAL will have “pretty serious surgery” on broken bones in his left wrist, Ainge said, which could involved having pins or screws implanted. O’Neal, who is left-handed, was injured when he took a charge in the Knicks series and landed on his wrist.

AINGE ALSO said that he was waiting to hear from Shaquille O’Neal whether the future Hall of Famer wants to return. O’Neal played a total of just 18 minutes since Feb. 1, making just two, short appearances in the playoffs.