首页 \ 问答 \ f#访问树的根元素(f# access root element of a tree)

f#访问树的根元素(f# access root element of a tree)

我在F#中有一个规范树,即通过声明

type binaryTree = 
    | Leaf
    | Node of binaryTree * float * binaryTree

然后使用递归函数来创建树

let rec makeTree tree element = 
    match element, tree with
        | x, Leaf -> Node(Leaf,x,Leaf)
        | x, Node(l,y,r) -> Node(l,y, (makeTree r x))

这一切都很好。 现在我想对树进行排序,以便在每个节点上,节点的值小于其所有子节点的值。 我可以想象这样做。 但是,我想采用树的第一个元素。 也就是说,我想将树视为队列。 我在树上看到的唯一例子是使用高阶函数来对树做一些事情,但是当我已经对它进行排序时,这似乎是一种浪费。

如何访问此树的根节点?


I have a canonical tree in F#, i.e by declaring

type binaryTree = 
    | Leaf
    | Node of binaryTree * float * binaryTree

and then using a recursive function to make the tree

let rec makeTree tree element = 
    match element, tree with
        | x, Leaf -> Node(Leaf,x,Leaf)
        | x, Node(l,y,r) -> Node(l,y, (makeTree r x))

This is all fine. Now I want to sort the tree so that at each node, the value of the node is smaller than the value of all its children. I can imagine doing this. However, I want to then take the first element of the tree. That is, I want to treat the tree like a queue. The only examples I have seen with trees use higher-order functions to do something with the tree, but this seems like a waste when I have already sorted it.

How can I access the root node of this tree?


原文:https://stackoverflow.com/questions/5817777
更新时间:2021-11-02 16:11

最满意答案

您的问题可能与您的发送/接收大小以及JDB上面发布的Graph问题中的对象有关。 您可以将配置中的这些数字膨胀到目前可用的数据量,但是,当您最终来回传递非常大的响应时,这不是一个可行的长期解决方案。

我建议考虑从WCF服务流式传输您的序列化响应,这将允许您发送和接收长期无限量的数据。 此MSDN链接中概述了此过程: http//msdn.microsoft.com/en-us/library/ms733742.aspx

不要让文章的长度吓到你,它只是给你关于为什么和如何的背景信息。 这个过程本身并不太复杂,无法理解并将解决您未来的问题。

〜VulgarBinary


Your issue is likely with your send / receive size as well as the objects in Graph issue that JDB posted above. You can inflate these numbers in your configuration to a point where it will work for the moment with the amount of data you currently have, however, this is not a viable long term solution when you might end up passing very large responses back and forth.

I would recommended looking into streaming your serialized response from the WCF service, which will allow you a limitless amount of data longer term to be sent and received. This process is outlined in this MSDN link: http://msdn.microsoft.com/en-us/library/ms733742.aspx

Don't let the length of the article scare you, it's just giving you background information on why and how. The process itself isn't too complicated to understand and will solve your problem going into the future.

~VulgarBinary

相关问答

更多