Close only when open.
transcript
List every balanced text of brackets. Never build an invalid one.
Open a bracket while some are left. Close a bracket only while one is open. Those two tests are the whole prune.
Three pairs make five balanced texts. We ran the search and counted its calls. The search makes 22 calls. Every text of six brackets has 127 calls.
Ten pairs make 16,796 texts. The search makes 82,499 calls. The full tree has 2,097,151.
Add the bracket, and explore. Then remove the bracket. The next branch starts clean.
A bad text is cut at its first mistake. Everything below that mistake is skipped. That is where the savings come from.
Try the open bracket first. The texts come out in dictionary order. A test can compare two runs exactly. State the order in the task.
Invalid texts are never built. No call is wasted on them. Refuse the choice before you make it. Full breakdown in the lecture.