6-Parenthesis and Binary Trees

# 6-Parenthesis and Binary Trees - Parenthesis and Binary...

• Notes
• 5

This preview shows pages 1–2. Sign up to view the full content.

Parenthesis and Binary Trees Consider the 4 node binary tree: 1 / \ 2 4 / \ / \ 3 / \ If traverse the tree in preorder then the numbers assigned to the nodes reflect the order in which they are visited. Now, suppose create a sequence of 1’s and 0’s by traversing the tree in preorder and writing down a 1 when we visit a node and a 0 when we encounter a null subtree; except for the last null subtree . For this tree we get the sequence 1 1 0 1 0 0 1 0 Now, suppose we substitute “(“for each 1 and “)” for each 0 of the sequence. We get: ( ( ) ( ) ) ( ) - a valid sequence of parentheses. In general, we can represent any n node binary tree uniquely by such a sequence of n1’s and n 0’s or n left and n right parentheses. Now, following Knuth, let A pq be a lexicographic listing of all strings α composed of p left and q > p right parentheses for which the string, ( q-p α, is a valid sequence of parentheses, where 0 < p < q and q >0. We take A 00 to be the null string and A pq to be the null list when p<0 or q<p. Then, the first string in the listing A pq must be ) q-p ( ) . . . ( ) where there are p of the pairs ( ) and the last string in the listing must be ( p ) q . Why? Can describe the listing A pq as) Apq -1 + (A p-1q where )A pq means every string of A pq is preceded by ) and similarly for (A pq . Why is this recursive description correct?

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.
• Fall '16
• James Korsh
• Catalan number

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern