Generating Tree Permutations Constant-memory Iterative Generation of Special Strings Representing Binary Trees Sebastian Smyczy«ski Faculty of Mathematics and Computer Science, Nicolaus Copernicus University, Toru«, Poland Sebastian Smyczy«ski Generating Tree Permutations Interesting combinatorical problem Enumerate all possible shapes of binary trees with the given number of nodes. Sebastian Smyczy«ski Generating Tree Permutations Natural order Natural order of binary trees The natural order of binary trees follows the recursive denition: We say that T ≺ T if: 1 2 |T1 | < |T2 |, or |T1 | = |T2 | and left (T1 ) ≺ left (T2 ), lub |T1 | = |T2 | and left (T1 ) = left (T2 ) and right (T1 ) ≺ right (T2 ), Sebastian Smyczy«ski Generating Tree Permutations Natural order Natural order of binary trees The natural order of binary trees follows the recursive denition: We say that T ≺ T if: 1 2 |T1 | < |T2 |, or |T1 | = |T2 | and left (T1 ) ≺ left (T2 ), lub |T1 | = |T2 | and left (T1 ) = left (T2 ) and right (T1 ) ≺ right (T2 ), The size matters - the smaller trees precedes the larger ones. Sebastian Smyczy«ski Generating Tree Permutations Natural order Natural order of binary trees The natural order of binary trees follows the recursive denition: We say that T ≺ T if: 1 2 |T1 | < |T2 |, or |T1 | = |T2 | and left (T1 ) ≺ left (T2 ), lub |T1 | = |T2 | and left (T1 ) = left (T2 ) and right (T1 ) ≺ right (T2 ), The size matters - the smaller trees precedes the larger ones. It is dened in natural for binary trees recursive way. Sebastian Smyczy«ski Generating Tree Permutations Natural order Figure: All shapes of binary trees with 4 nodes listed in their natural order Sebastian Smyczy«ski Generating Tree Permutations Tree permutations Sebastian Smyczy«ski Generating Tree Permutations Tree permutations We can represent the tree T with n nodes uniquely as a sequence of the integer numbers 1, 2, . . . , n, Sebastian Smyczy«ski Generating Tree Permutations Tree permutations 5 We can represent the tree T with n nodes uniquely as a sequence of the integer numbers 1, 2, . . . , n, rst labeling the nodes with their position's number as they appear in the preorder travelsal of the tree, Sebastian Smyczy«ski 2 1 Generating Tree Permutations 7 3 6 4 Tree permutations 5 We can represent the tree T with n nodes uniquely as a sequence of the integer numbers 1, 2, . . . , n, rst labeling the nodes with their position's number as they appear in the preorder travelsal of the tree, then listing those labels as they appear in the preorder traversal of the tree. Sebastian Smyczy«ski 2 1 7 3 4 5213476 Generating Tree Permutations 6 Tree permutations 5 We can represent the tree T with n nodes uniquely as a sequence of the integer numbers 1, 2, . . . , n, rst labeling the nodes with their position's number as they appear in the preorder travelsal of the tree, then listing those labels as they appear in the preorder traversal of the tree. We shall call the resulting permutation p = p , p , . . . , pn tree permutation of the T . 1 2 1 3 4 5213476 2 Sebastian Smyczy«ski 7 Generating Tree Permutations 6 Tree permutations and the natural order Figure: All shapes of binary trees with 4 nodes listed in their natural order Sebastian Smyczy«ski Generating Tree Permutations Tree permutations and the natural order 1 1 2 1 2 3 1 4 4 3 3 2 2 4 2 1 4 3 3 2 1 3 2 4 3 4 4 1 3 3 4 2 1 2 3 3 1 4 1 2 2 2 4 1 3 3 4 2 4 4 1 1 4 3 2 1 Figure: All shapes of binary trees with 4 nodes listed in their natural order Sebastian Smyczy«ski Generating Tree Permutations Tree permutations and the natural order 1 1 2 1 2 3 1 4 4 1234 3 3 2 1243 3 3 2 4 1324 2 3124 1 2 1 3 1432 4 2134 3 2143 4 1 4 3 4123 1 4 4 2 2 2 1423 3 3214 3 3 1 4 4 2 4 4 1 1 4 2 2 1 4132 4 3 3 1 3 4213 2 2 4312 1 4321 Figure: All shapes of binary trees with 4 nodes listed in their natural order Sebastian Smyczy«ski Generating Tree Permutations Tree permutations and the natural order 1 1 2 2 3 1 4 4 1234 3 3 2 1243 4 1324 4 3 3 1 2 4 2 3124 1 4 1 2 3 3214 4123 1234 1243 1234 1 12434 1324 2 1423 3 1432 1423 2134 2143 31244 1 3214 41233 4132 2 4213 4132 4312 4321 1 4 3 2 1 2 3 2 1 4 4 1432 2134 3 2143 4 4 2 1 4 3 3 1 3 4213 2 2 4312 1 4321 Figure: All shapes of binary trees with 4 nodes listed in their natural order Sebastian Smyczy«ski Generating Tree Permutations Recursive property of the tree permutations ∀p ∈ Tn ∃p 0 ∈ Tp −1 ∃p 00 ∈ Tn−p p = p1 p 0 (p 00 ⊕ p1 ) 1 1 p 00 4 3 1 2⊕ z { z }| { p = 8 4 1 3 2 6 5 7 12 11 9 10 p0 }| Sebastian Smyczy«ski z }| { Generating Tree Permutations 8 Climbing property of tree permutations Lemma A permutation p = p , p , . . . , pn of the integer numbers 1, 2, . . . , n is a tree permutation i it is 231-avoiding - there are no such indices i < j < k such that pk < pi < pj . 1 2 Sebastian Smyczy«ski Generating Tree Permutations Climbing property of tree permutations Lemma A permutation p = p , p , . . . , pn of the integer numbers 1, 2, . . . , n is a tree permutation i it is 231-avoiding - there are no such indices i < j < k such that pk < pi < pj . 1 2 pj 3 2 pi 1 Sebastian Smyczy«ski pk Generating Tree Permutations Climbing property of tree permutations Lemma A permutation p = p , p , . . . , pn of the integer numbers 1, 2, . . . , n is a tree permutation i it is 231-avoiding - there are no such indices i < j < k such that pk < pi < pj . 1 2 15 14 12 10 9 7 5 Sebastian Smyczy«ski Generating Tree Permutations Climbing property of tree permutations Lemma A permutation p = p , p , . . . , pn of the integer numbers 1, 2, . . . , n is a tree permutation i it is 231-avoiding - there are no such indices i < j < k such that pk < pi < pj . 1 2 15 14 12 10 9 7 5 Sebastian Smyczy«ski Generating Tree Permutations Working sux properties Denition Let p = p , p , . . . , pn be a tree permutation. The sux of p which makes p dierent from its successor in the lexicographic order is called working sux. 1 2 Sebastian Smyczy«ski 1 1 1 1 1 2 2 3 3 4 4 4 4 4 2 2 3 4 4 1 1 1 2 1 1 2 3 3 Generating Tree Permutations 3 4 2 2 3 3 4 2 1 2 3 1 1 2 4 3 4 3 2 4 3 4 4 3 2 3 2 1 Working sux properties Denition Let p = p , p , . . . , pn be a tree permutation. The sux of p which makes p dierent from its successor in the lexicographic order is called working sux. 1 2 Lemma Let p be a tree permutation and let i be the index of the rst position of its working sux. If tree permutation q is the successor of p in the lexicographic order, then qi = pi + 1. Sebastian Smyczy«ski 1 1 1 1 1 2 2 3 3 4 4 4 4 4 2 2 3 4 4 1 1 1 2 1 1 2 3 3 Generating Tree Permutations 3 4 2 2 3 3 4 2 1 2 3 1 1 2 4 3 4 3 2 4 3 4 4 3 2 3 2 1 Working sux properties Lemma Let p be a tree permutation and let i be the index of the rst position of its working sux, then there exist no such indices j, k such that i < j < k and pk = pj + 1. 20 19 18 17 16 15 14 13 12 11 10 9 Lemma Let p = p , p , . . . , pn be a tree permutation and i be the starting index of its working sux. For any index i ≤ k < n, pk > pk + 1 implies that pk = pk + 1 + 1. 1 2 Sebastian Smyczy«ski 8 7 6 5 4 3 2 1 Generating Tree Permutations The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 1 2 4 3 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 1 2 4 3 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 1 2 4 3 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 1 2 4 3 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 1 2 4 3 1 3 4 2 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 1 2 4 3 1 3 4 2 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 1 2 4 3 1 3 2 4 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm 1 2 3 4 1 2 4 3 1 3 2 4 Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski Generating Tree Permutations The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 2 2 3 4 Generating Tree Permutations 3 4 2 2 4 3 4 3 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 2 2 3 4 Generating Tree Permutations 3 4 2 2 4 3 4 3 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 2 2 3 4 Generating Tree Permutations 3 4 2 2 4 3 4 3 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 2 2 3 4 Generating Tree Permutations 3 4 2 2 4 3 4 3 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 1 2 2 3 4 4 Generating Tree Permutations 3 4 2 2 3 4 3 4 3 2 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 1 2 2 3 4 4 Generating Tree Permutations 3 4 2 2 3 4 3 4 3 2 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 1 2 2 3 4 4 Generating Tree Permutations 3 4 2 2 3 4 3 4 3 2 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 1 2 2 3 4 4 Generating Tree Permutations 3 4 2 2 3 4 3 4 3 2 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 1 2 2 2 3 4 4 4 Generating Tree Permutations 3 4 2 2 3 3 4 3 4 3 2 1 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 1 2 2 2 3 4 4 4 Generating Tree Permutations 3 4 2 2 3 3 4 3 4 3 2 1 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 1 2 2 2 3 4 4 1 Generating Tree Permutations 3 4 2 2 3 3 4 3 4 3 2 4 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 1 2 2 3 3 4 4 4 4 4 2 2 3 4 4 1 1 1 2 1 1 2 3 3 Generating Tree Permutations 3 4 2 2 3 3 4 2 1 2 3 1 1 2 4 3 4 3 2 4 3 4 4 3 2 3 2 1 The algorithm Generating tree permutations in lexicographic order: Start with permutation 1 2 . . . n. Find the rst pair of indices i < j starting from the end of the permutation such that pj = pi + 1. Swap the elements at found positions. Sort all but the rst elements of the working sux in ascending order. Sebastian Smyczy«ski 1 1 1 1 1 2 2 3 3 4 4 4 4 4 2 2 3 4 4 1 1 1 2 1 1 2 3 3 Generating Tree Permutations 3 4 2 2 3 3 4 2 1 2 3 1 1 2 4 3 4 3 2 4 3 4 4 3 2 3 2 1 Optimalization of the searching step , , , , , , , , , , , , , , , , , , , , , , , , 9 6 3 2 1 5 4 7 8 11 10 22 19 17 13 12 16 15 14 18 21 20 25 24 23 Sebastian Smyczy«ski Generating Tree Permutations Optimalization of the searching step , , , , , , , , , , , , , , , , , , , , , , , , 9 6 3 2 1 5 4 7 8 11 10 22 19 17 13 12 16 15 14 18 21 20 25 24 23 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Sebastian Smyczy«ski Generating Tree Permutations Optimalization of the searching step , , , , , , , , , , , , , , , , , , , , , , , , 9 6 3 2 1 5 4 7 8 11 10 22 19 17 13 12 16 15 14 18 21 20 25 24 23 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Sebastian Smyczy«ski Generating Tree Permutations Optimalization of the searching step , , , , , , , , , , , , , , , , , , , , , , , , 9 6 3 2 1 5 4 7 8 11 10 22 19 17 13 12 16 15 14 18 21 20 25 24 23 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Sebastian Smyczy«ski Generating Tree Permutations Optimalization of the sorting step , , , , , , , , , , , , , , , , , , , , , , , , 9 6 3 2 1 5 4 7 8 11 10 22 19 17 13 12 16 15 14 18 21 20 25 24 23 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Sebastian Smyczy«ski Generating Tree Permutations Optimalization of the sorting step , , , , , , , , , , , , , , , , , , , , , , , , 9 6 3 2 1 5 4 7 8 11 10 22 19 17 14 12 16 15 13 18 21 20 25 24 23 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Sebastian Smyczy«ski Generating Tree Permutations Optimalization of the sorting step , , , , , , , , , , , , , , , , , , , , , , , , 9 6 3 2 1 5 4 7 8 11 10 22 19 17 14 12 13 15 16 18 20 21 23 24 25 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Sebastian Smyczy«ski Generating Tree Permutations Time Complexity Knowing that the number of binary trees with n nodes is is given by the Catalan number 2n Cn = /(n + 1). n and using the recursive property of tree permutations we can give the formula for the summarized length of all working suxes of tree permutations for given number of nodes n n Wn = ∑ Ci − Wn−i + Wi − + (n − i )(Ci − − 1) + n(n − 1). 1 i =1 1 1 Solving this recurrence we obtain Wn = Cn+ − n − 1. Since Cn+ = (nn++ ) Cn , therefore the average-case n time-complexity of the algorithm is constant O ( W Cn ) = O (1). 1 2 2 1 1 2 Sebastian Smyczy«ski Generating Tree Permutations Thank you! Binary origami tree http://www.tsg.ne.jp/TT/origami/gallery.html Sebastian Smyczy«ski Generating Tree Permutations