首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >F#中的插入树

F#中的插入树
EN

Code Review用户
提问于 2016-11-08 05:25:27
回答 1查看 147关注 0票数 1

这是可以改进的,还是可以变得更简洁?

代码语言:javascript
复制
type Government = {
    Id : Id;
    Name : string;
    Abbreviation : string;
    ParentId : string option;
}

let govList =
    [
        {Id = "1"; Name = "United States"; Abbreviation = "USA"; ParentId = None}
        {Id = "2"; Name = "California"; Abbreviation = "CA"; ParentId = Some("1")}
        {Id = "3"; Name = "Texas"; Abbreviation = "TX"; ParentId = Some("1")}
        {Id = "4"; Name = "Houston"; Abbreviation = "HOU"; ParentId = Some("3")}
        {Id = "5"; Name = "Dallas"; Abbreviation = "DAL"; ParentId = Some("3")}
        {Id = "6"; Name = "San Antonio"; Abbreviation = "SANAN"; ParentId = Some("3")}
        {Id = "7"; Name = "El Paso"; Abbreviation = "ELP"; ParentId = Some("3")}
        {Id = "8"; Name = "Canada"; Abbreviation = "CAN"; ParentId = None}
        {Id = "9"; Name = "France"; Abbreviation = "FRN"; ParentId = None}
    ]


type GovernmentStructure<'gov> = 
| Root of Government : 'gov * SubGov : GovernmentStructure<'gov> list
| Node of Government : 'gov * SubGov : GovernmentStructure<'gov> list
| Leaf of Government : 'gov


let insertGovernment (posGov: Government) (newGov : Government)
    (currentStructure : GovernmentStructure<Government>) =

    let updateStructure (govPos : Government) (newGov : Government)
        (currentStructure : GovernmentStructure<Government>) =

        // iterate over the nodes to find position and insert. parentId is useless, will fix later. 
        let rec findPosAndInsertInto parentId (startStructure : GovernmentStructure<Government>) =
            match startStructure with
            | Node (g, sg) when g = govPos ->
                Node(g, [
                            yield Node(newGov, [])
                            yield! sg
                        ])
            | Node (g, sg) ->
                Node (g, [
                            for x in sg do
                                yield findPosAndInsertInto g.Id x
                    ])
            | Leaf(g) when g = govPos -> 
                Node (g, [Leaf(newGov)])
            // better logic needed, should not hit this
            | node -> node

        match currentStructure with
        // Insert if position is at root
        | Root(gov', subGov) when gov' = govPos ->

            Root(gov', [    
                            yield Node(newGov, [])
                            yield! subGov
                       ]) 
        // Insert if position if at node or leaf
        | node -> 
            match node with
            | Root (gov', subGov) ->
                Root(gov', [
                            for x in subGov do
                                yield findPosAndInsertInto gov'.Id x 
                        ])
            // better logic needed, shouldn't hit this level
            | node -> node

    updateStructure posGov newGov currentStructure
EN

回答 1

Code Review用户

发布于 2016-11-23 19:38:46

通过使用缩写本身作为标识符,可以简化记录类型;ISO 3166至少为国家和直属人员提供了标准代码(尽管有冲突-西澳大利亚州和华盛顿州都是“WA”-因此您可能希望由其父母对它们进行限定,例如,USA:WA诉AUS:WA)。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/146459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档