Abstract. A binary string B of length n = kt is a k-ary Dyck word if it contains t copies of 1, and the number of 0s in every prefix of B is at most k−1 times the number of 1s. We provide two loopless algorithms for generating k-ary Dyck words in cool-lex order: (1) The first requires two index variables and assumes k is a constant; (2) The second requires t index variables and works for any k. We also efficiently rank k-ary Dyck words in cool-lex order. Our results generalize the "coolCat" algorithm by Ruskey and Williams (Generating balanced parentheses and binary trees by prefix shifts in CATS 2008) and provide the first loopless and ranking applications of the general cool-lex Gray code by Ruskey, Sawada, and Williams (Binary bubble languages and cool-lex order under review).
Background
k-ary Dyck WordsLet B(n, t) be the set of binary strings of length n containing t copies of 1. A string B ∈ B(kt, t) is a k-ary Dyck word if the number of 0s in each prefix is at most k−1 times the number of 1s. Let D k (t) be the set of k-ary Dyck words of length kt. For example, the k-ary Dyck words with k = t = 3 are given below D 3 (3) = {111000000, 110100000, 101100000, 110010000, 101010000, 100110000, 110001000, 101001000, 100101000, 110000100, 101000100, 100100100}.The k-ary Dyck words of length kt have simple bijections with a number of combinatorial objects including k-ary trees with t internal nodes [2] [3]. The 2-ary Dyck words are known as balanced parentheses when 1 and 0 are replaced by '(' and ')' respectively, and the cardinality of D 2 (t) is the tth Catalan number. A simple property of k-ary Dyck words is that they can be "separated" according to the following remark. We let αβ denote the concatenation of the binary strings α and β, and we say that α and β have the same content if they have equal length and an equal number of 1s. Remark 1. If αβ, γδ ∈ D k (t) and α and γ have the same content, then αδ, βγ ∈ D k (t). In other words, prefixes (or suffixes) of k-ary Dyck words with the same content can be separated and recombined.