Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node_number_at_depth broken for binary trees #29134

Closed
mwageringel opened this issue Feb 1, 2020 · 13 comments
Closed

node_number_at_depth broken for binary trees #29134

mwageringel opened this issue Feb 1, 2020 · 13 comments

Comments

@mwageringel
Copy link

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

CC: @tscrim @jm58660 @darijgr

Component: combinatorics

Author: Travis Scrimshaw

Branch/Commit: aaa0581

Reviewer: Markus Wageringel

Issue created by migration from https://trac.sagemath.org/ticket/29134

@fchapoton
Copy link
Contributor

comment:1

For compactness, the usual ascii_art plot does not display the leaves, which nevertheless are nodes.

sage: ascii_art([t.to_full() for t in BinaryTrees(3)])
[   _o__       ,   __o___     ,     __o__    ,      __o___  ,        _o__   ]
[  /    \         /      \         /     \         /      \         /    \  ]
[ o     _o_      o       _o_      o       o      _o_       o      _o_     o ]
[      /   \            /   \    / \     / \    /   \            /   \      ]
[     o     o          o     o  o   o   o   o  o     o          o     o     ]
[          / \        / \                           / \        / \          ]
[         o   o      o   o                         o   o      o   o         ]

@fchapoton
Copy link
Contributor

comment:2

I therefore suggest to close as invalid. All the conventions are clearly explained in the documentation.

@fchapoton fchapoton removed this from the sage-9.1 milestone Feb 2, 2020
@mwageringel
Copy link
Author

comment:3

Replying to @fchapoton:

For compactness, the usual ascii_art plot does not display the leaves, which nevertheless are nodes.

Indeed, that explains the output I got. It is still a bit confusing that node_number_at_depth counts nodes including leaves, while node_number does not include the leaves.

sage: [t.node_number() for t in BinaryTrees(3)]
[3, 3, 3, 3, 3]

In my opinion, there is an inconsistency that should be fixed.

The documentation is not that clear about this. If anything, I am inclined to interpret it as saying that leaves are not considered nodes in binary trees:

    Binary trees contain nodes and leaves, where each node has two
    children while each leaf has no children. The number of leaves
    of a binary tree always equals the number of nodes plus `1`.

@tscrim
Copy link
Collaborator

tscrim commented Feb 2, 2020

Author: Travis Scrimshaw

@tscrim
Copy link
Collaborator

tscrim commented Feb 2, 2020

Commit: aaa0581

@tscrim

This comment has been minimized.

@tscrim
Copy link
Collaborator

tscrim commented Feb 2, 2020

@tscrim
Copy link
Collaborator

tscrim commented Feb 2, 2020

comment:4

I think it is slightly misleading to say ascii_art does not display the leaves. I would consider there to be two distinct sets of objects:

1 - binary trees with n nodes and
2 - full binary trees with n internal nodes.

There is a definite inconsistency here between node_number and node_number_at_depth. The problem is this corner case:

sage: T = BinaryTrees(3)[3]
sage: Tp = T[1]
sage: Tp
.
sage: Tp.is_empty()
True
sage: Tp.node_number_at_depth(0)
1

In particular, it says an empty tree has 1 node, which is wrong. This branch fixes this.


New commits:

aaa0581Fixing node_number_at_depth for empty trees.

@tscrim tscrim added this to the sage-9.1 milestone Feb 2, 2020
@mwageringel
Copy link
Author

comment:5

Thanks for fixing this so quickly. It works exactly as I expect.

Frédéric, if you agree, I think this can be set to positive.

@mwageringel
Copy link
Author

Reviewer: Markus Wageringel

@fchapoton
Copy link
Contributor

comment:6

do whatever you want, I have no time

@mwageringel
Copy link
Author

comment:7

Ok, no problem.

@vbraun
Copy link
Member

vbraun commented Feb 11, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants