What is a better way to model a treeNode?

限于喜欢 提交于 2020-02-05 02:11:17

问题


What are the pros and cons of each of the following two ways to create a tree node?

type TreeNode = | TreeNode of int * (TreeNode option) * (TreeNode option) * (TreeNode option)



type Node = | Node of int * Node * Node

        | None

回答1:


They are very close, but not entirely equivalent. You can actually reason about functional types quite nicely using mathematics (see for example my recent article), but you can see that even informally.

Given any TreeNode(num, optLeft, optRight), you can always create a Node containing the same information (None of the option type will be mapped to Node.None).

Interestingly, this does not work the other way round. If someone gives you Node.None, you cannot turn it into TreeNode, because TreeNode needs at least one integer in the root.

So, they are equivalent with the exception that the second one allows empty trees. From the practical perspective, the second type seems easier to use (because you only have to handle empty tree in one place).



来源:https://stackoverflow.com/questions/24412225/what-is-a-better-way-to-model-a-treenode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!