Skip to content

Commit

Permalink
Trac #29134: node_number_at_depth broken for binary trees
Browse files Browse the repository at this point in the history
{{{
sage: ascii_art(BinaryTrees(3).list())
[ o    , o  ,   o  ,   o,     o ]
[  \      \    / \    /      /  ]
[   o      o  o   o  o      o   ]
[    \    /           \    /    ]
[     o  o             o  o     ]
sage: [t.node_number_at_depth(0) for t in BinaryTrees(3)]  # correct
[1, 1, 1, 1, 1]
sage: [t.node_number_at_depth(1) for t in BinaryTrees(3)]  # expected
[1, 1, 2, 1, 1]
[2, 2, 2, 2, 2]
sage: [t.node_number_at_depth(2) for t in BinaryTrees(3)]  # expected
[1, 1, 0, 1, 1]
[2, 2, 4, 2, 2]
}}}

This comes from the fact that the empty tree thinks it has 1 node at
depth 0:
{{{
sage: T = BinaryTree()
sage: T
.
sage: T.is_empty()
True
sage: T.node_number_at_depth(0)
1
}}}

URL: https://trac.sagemath.org/29134
Reported by: gh-mwageringel
Ticket author(s): Travis Scrimshaw
Reviewer(s): Markus Wageringel
  • Loading branch information
Release Manager committed Feb 10, 2020
2 parents 6344f4f + aaa0581 commit e8fccdf
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/sage/combinat/abstract_tree.py
Expand Up @@ -822,7 +822,21 @@ def node_number_at_depth(self, depth):
o
sage: [T.node_number_at_depth(i) for i in range(6)]
[1, 3, 4, 2, 1, 0]
TESTS:
Check that the empty tree has no nodes (:trac:`29134`)::
sage: T = BinaryTree()
sage: T
.
sage: T.is_empty()
True
sage: [T.node_number_at_depth(i) for i in range(3)]
[0, 0, 0]
"""
if self.is_empty():
return Integer(0)
if depth == 0:
return Integer(1)
return sum(son.node_number_at_depth(depth - 1) for son in self)
Expand Down

0 comments on commit e8fccdf

Please sign in to comment.