Generating balanced parentheses and binary trees by prefix shifts

  • Authors:
  • Frank Ruskey;Aaron Williams

  • Affiliations:
  • University of Victoria, Victoria BC, Canada;University of Victoria, Victoria BC, Canada

  • Venue:
  • CATS '08 Proceedings of the fourteenth symposium on Computing: the Australasian theory - Volume 77
  • Year:
  • 2008

Quantified Score

Hi-index 0.00

Visualization

Abstract

We show that the set Bn of balanced parenthesis strings with n left and n right parentheses can be generated by prefix shifts. If b1, b2, ..., b2n is a member of Bn, then the k-th prefix shift is the string b1, bk, b2, ..., bk-1, bk+1, ..., b2n. Prefix shift algorithms are also known for combinations, and permutations of a multiset; the combination algorithm appears in fascicles of Knuth vol 4. We show that the algorithm is closely related to the combination algorithm, and like it, has a loopless implementation, and a ranking algorithm that uses O(n) arithmetic operations. Additionally, the algorithm can be directly translated to generate all binary trees by a loopless implementation that makes a constant number of pointer changes for each successively generated tree.