1

I have 2 classes: One which is a Tree which can have N subtrees, and BinaryTree can have at most 2 subtrees.

The classes are defined like so:

data Tree a = EmptyTree | Tree a [Tree a] deriving (Show, Ord, Eq)
data BinTree a = EmptyBin | Node a (BinTree a) (BinTree a) deriving (Show, Ord, Eq)

Is there a way I could convert a BinaryTree into a Tree?

Thanks

1
  • 3
    These are datatypes, not classes. Commented Feb 14, 2015 at 6:44

1 Answer 1

5

Sure. The structure of the Tree allows us to put the two subtrees into the list:

convert EmptyBin = EmptyTree
convert (Node a l r) = Tree a [convert l,convert r]

If you wanted to convert the other way, that might be more complex, depending on how you wanted to branch a long list of subtrees, but you could use an Ord a context to help you there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.