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

data structure tree #576

Open
minnieshi opened this issue Oct 23, 2019 · 0 comments
Open

data structure tree #576

minnieshi opened this issue Oct 23, 2019 · 0 comments

Comments

@minnieshi
Copy link

minnieshi commented Oct 23, 2019

I think the datastructure for tree has been defined 'wrong'.
sealed trait Tree[+A] case class Leaf[A](value: A) extends Tree[A] case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]

The problem with this current definition is it can have different 'size' depending on how you count the tree.
Currently the tree can have node without value. it is purely node. e.g.
Branch(Branch(Leaf(2), Branch(Leaf(3), Leaf(3))), Leaf(1))
If you draw it on the paper, it will have 3 'branch' without value, the first one is the root; and it has 3 leaves with values.
So do you count the tree size as 4(only leaves) or 7(leaves and nodes)? or even 5?
using the answer provided in the current master branch ends up with 5.

Instead, after quick search on the internet, the answer on this stackexchange gives better data structure for the tree https://codereview.stackexchange.com/questions/102470/binary-tree-implementation-in-scala But of course, it does not need to be binary tree (so the 'isValid' part is not needed)

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

2 participants