Skip to content

Commit 7f13d0f

Browse files
authored
exit early from canonical_representation! for small trees (#43)
1 parent c541d15 commit 7f13d0f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/RootedTrees.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ one with lexicographically biggest level sequence.
306306
See also [`canonical_representation`](@ref).
307307
"""
308308
function canonical_representation!(t::RootedTree, buffer=similar(t.level_sequence))
309+
# Since we use a recursive implementation, it is useful to exit early for
310+
# small trees. If there are at most 3 vertices in a valid rooted tree, its
311+
# level sequence must already be in canonical representation.
312+
if order(t) <= 3
313+
return RootedTree(t.level_sequence, true)
314+
end
315+
309316
# First, sort all subtrees recursively. Here, we use `view`s to avoid memory
310317
# allocations.
311318
# TODO: Assume 1-based indexing in the following

0 commit comments

Comments
 (0)