Efficient Generation, Ranking, and Unranking of (km)-Ary Trees in B-Order

  • M. Amani
  • A. Nowzari-DaliniEmail author
Original Paper


In this paper, we present a new generation algorithm with corresponding ranking and unranking algorithms for (km)-ary trees in B-order. (km)-ary tree is introduced by Du and Liu. A (km)-ary tree is a generalization of k-ary tree, whose every node of even level of the tree has degree k and odd level of the tree has degree 0 or m. Up to our knowledge no generation, ranking or unranking algorithms are given in the literature for this family of trees. We use Zaks’ encoding for representing (km)-ary trees and to generate them in B-order. We also prove that, to generate (km)-ary trees in B-order using this encoding, the corresponding codewords should be generated in reverse-lexicographical ordering. The presented generation algorithm has a constant average time and O(n) time complexity in the worst case. Due to the given encoding, both ranking and unranking algorithms are also presented taking O(n) and \(O(n\log n)\) time complexity, respectively.


(k, m)-ary trees Tree generation Ranking Unranking 

Mathematics Subject Classification

Primary 05c05 Secondary 68w32, 05c85 



  1. 1.
    Ahmadi-Adl, A., Nowzari-Dalini, A., Ahrabian, H.: Ranking and unranking algorithms for loopless generation of \(t\)-ary trees. Logic J. IGPL 19, 33–43 (2011)MathSciNetCrossRefGoogle Scholar
  2. 2.
    Ahrabian, H., Nowzari-Dalini, A.: Parallel generation of \(t\)-ary trees in A-order. Comput. J. 50, 581–588 (2007)CrossRefGoogle Scholar
  3. 3.
    Amani, M., Nowzari-Dalini, A., Ahrabian, H.: Generation of neuronal trees by a new three letters encoding. Comput. Inform. J. 33(6), 1428–1450 (2014)MathSciNetzbMATHGoogle Scholar
  4. 4.
    Amani, M., Nowzari-Dalini, A.: Ranking and unranking algorithm for neuronal trees in B-order. J. Phys. Sci. 20, 19–34 (2015)MathSciNetGoogle Scholar
  5. 5.
    Amani, M., Nowzari-Dalini, A.: Generation, ranking and unranking of ordered trees with degree bounds, in Proc. DCM 2015. Electron. Proc. Theor. Comput. Sci. 204, 31–45 (2015)CrossRefGoogle Scholar
  6. 6.
    Amani, M.: Gap terminology and related combinatorial properties for AVL trees and Fibonacci-isomorphic trees. AKCE Int. J. Graphs Comb. 15(1), 14–21 (2018)MathSciNetCrossRefGoogle Scholar
  7. 7.
    Du, R.R.X., Liu, F.: \((k, m)\)-Catalan numbers and hook length polynomials for plane trees. Eur. J. Comb. 28, 1312–1321 (2007)MathSciNetCrossRefGoogle Scholar
  8. 8.
    Durocher, S., Li, P.C., Mondal, D., Ruskey, F., Williams, A.: Cool-lex order and k-ary Catalan structures. J. Discrete Algorithms 16, 287–307 (2012)MathSciNetCrossRefGoogle Scholar
  9. 9.
    Heubach, S., Li, N., Mansour, T.: Staircase tilings and \(k\)-Catalan structures. Discrete Math. 308, 5954–5964 (2008)MathSciNetCrossRefGoogle Scholar
  10. 10.
    Joichi, J.I., White, D.E., Williamson, S.G.: Combinatorial Gray codes. SIAM J. Comput. 9, 130–141 (1980)MathSciNetCrossRefGoogle Scholar
  11. 11.
    Korsh, J.F., LaFollette, P.: Loopless generation of Gray codes for \(k\)-ary trees. Inf. Process. Lett. 70, 7–11 (1999)MathSciNetCrossRefGoogle Scholar
  12. 12.
    Kreher, D.L., Stinson, D.R.: Combinatorial Algorithms, 2nd edn. CRC Press, New York (1999)zbMATHGoogle Scholar
  13. 13.
    Li, L.: Ranking and unranking AVL trees. SIAM J. Comput. 15, 1025–1035 (1986)MathSciNetCrossRefGoogle Scholar
  14. 14.
    Pallo, J.: Enumerating, ranking and unranking binary trees. Comput. J. 29, 171–175 (1986)MathSciNetCrossRefGoogle Scholar
  15. 15.
    Pallo, J.: Generating trees with \(n\) nodes and \(m\) leaves. Int. J. Comput. Math. 21, 133–144 (1987)CrossRefGoogle Scholar
  16. 16.
    Pallo, J.: A simple algorithm for generating neuronal dendritic trees. Comput. Methods Program Biomed. 33, 165–169 (1990)CrossRefGoogle Scholar
  17. 17.
    van Baronaigien, D.R.: A loopless algorithm for generating binary trees sequences. Inf. Process. Lett. 39, 189–194 (1991)MathSciNetCrossRefGoogle Scholar
  18. 18.
    van Baronaigien, D.R., Ruskey, F.: Generating \(t\)-ary trees in A-order. Inf. Process. Lett. 27, 205–213 (1988)MathSciNetCrossRefGoogle Scholar
  19. 19.
    van Baronaigien, D.R.: A loopless Gray code algorithm for listing \(k\)-ary trees. J. Algorithms 35, 100–107 (2000)MathSciNetCrossRefGoogle Scholar
  20. 20.
    Ruskey, F.: Generating \(t\)-ary trees lexicographically. SIAM J. Comput. 7, 424–439 (1978)MathSciNetCrossRefGoogle Scholar
  21. 21.
    Seyedi-Tabari, E., Ahrabian, H., Nowzari-Dalini, A.: A new algorithm for generation of different types of RNA. Int. J. Comput. Math. 87, 1197–1207 (2010)MathSciNetCrossRefGoogle Scholar
  22. 22.
    Solomon, M., Finkel, R.A.: A note on enumerating binary trees. J. ACM 27, 3–5 (1980)MathSciNetCrossRefGoogle Scholar
  23. 23.
    Stanley, R.P.: Enumerative Combinatorics, vol. 2. Cambridge University Press, Cambridge (1999)CrossRefGoogle Scholar
  24. 24.
    Stojmenovic, I.: Listing combinatorial objects in parallel. Int. J. Parallel Emerg. Distrib. Syst. 21, 127–146 (2006)MathSciNetCrossRefGoogle Scholar
  25. 25.
    Vajnovszki, V.: Listing and random generation of neuronal trees coded by six letters. Autom. Comput. Appl. Math. 4, 29–40 (1995)MathSciNetGoogle Scholar
  26. 26.
    Vajnovszki, V., Pallo, J.: Generating binary trees in A-order from codewords defined on four-letter alphabet. J. Inf. Optim. Sci. 15, 345–357 (1994)MathSciNetzbMATHGoogle Scholar
  27. 27.
    Wu, R., Chang, J., Wang, Y.: A linear time algorithm for binary tree sequences transformation using left-arm and right-arm rotations. Theor. Comput. Sci. 335, 303–314 (2006)MathSciNetCrossRefGoogle Scholar
  28. 28.
    Wu, R., Chang, J., Chang, C.: Ranking and unranking of non-regular trees with a prescribed branching sequence. Math. Comput. Modell. 53, 1331–1335 (2011)MathSciNetCrossRefGoogle Scholar
  29. 29.
    Zaks, S.: Lexicographic generation of ordered trees. Theor. Comput. Sci. 10, 63–82 (1980)MathSciNetCrossRefGoogle Scholar

Copyright information

© Iranian Mathematical Society 2018

Authors and Affiliations

  1. 1.School of Mathematics, Statistics, and Computer Science, Colleague of Science University of TehranTehranIran
  2. 2.Department of Computer Science (IDI)Norwegian University of Science and Technology (NTNU)TrondheimNorway

Personalised recommendations