我正在尝试使用一种计算每个节点高度的方法在F#中实现一个trie结构。
这是我到目前为止所提出的:
type TrieNode =
| SubNodes of char * bool * TrieNode list
| Nil
member this.Char = match this with | Nil -> ' '
| SubNodes(c,weh,subnodes) -> c
member this.GetChild(c:char) = match this with | Nil -> []
| SubNodes(c,weh,subnodes) -> if subnodes.Length > 0 then [ (List.filter(fun (this:TrieNode) -> this.Char = c) subnodes).Head ] else []
member this.AWordEndsHere = match this with | Nil -> false
| SubNodes(c,weh,subnodes) -> weh
module TrieFunctions =
let rec insertWord (wordChars:char list) = function
| Nil -> SubNodes(' ', false, (insertWord wordChars Nil)::[])
//daca aici inca nu e cel putin un nod atunci fa nodul radacina standard adica nodul care nu e sfarsit de cuvant si
//are caracterul ' ' si incepe sa ii construiesti lui subnodurile
| SubNodes(c, weh, subnodes) as node ->
if(wordChars.Length = 1) then
SubNodes(wordChars.Head,true,[])
else
let child = node.GetChild(wordChars.Head)
if child = [] then
SubNodes(wordChars.Head,false,(insertWord wordChars.Tail node)::subnodes )
else
SubNodes(wordChars.Head,false,(insertWord wordChars.Tail child.Head)::subnodes )
let stringToCharList(s:string) = List.ofSeq s
type Trie(inner : TrieNode) =
member this.InsertWord(wordChars:char list) = Trie(TrieFunctions.insertWord wordChars inner)
member this.InsertWord(str:string) = Trie(TrieFunctions.insertWord (TrieFunctions.stringToCharList str) inner)
let trie = Trie(SubNodes(' ',false,List.empty))
.InsertWord("abc")
.InsertWord("abcd")
.InsertWord("abcd")
.InsertWord("abcde")
.InsertWord("abcdef")
.InsertWord("ab123cd")
.InsertWord("abc123d")
.InsertWord("abc132d")
现在我正在尝试编写高度计算功能。如果这是一个二叉树,这将非常容易编写,但在这个树中,每个节点都有一个子节点列表,所以我不知道如何在F#中反复遍历该事物。
这是我到目前为止使用list fold操作得出的,但它没有编译:
module NodeFunctions =
let rec nextLevel(node:TrieNode,curlevel:int) = function
| Nil -> curlevel
| SubNodes(_,_,subnodes) ->
List.fold (fun acc (node:TrieNode,_,_) -> let res = nextLevel(node,curlevel+1) and
if( acc > res) then acc else res) curlevel subnodes
我是否可以重写此功能以使其有效? 或者任何关于如何实现我的目标的想法应该是不正确的想法?
提前谢谢
答案 0 :(得分:2)
您的代码似乎几乎是正确的。以下编译对我来说(我没有尝试运行它,因为我在创建trie
值时遇到异常,但递归方案听起来没错):
let rec nextLevel(node:TrieNode,curlevel:int) =
match node with
| Nil -> curlevel
| SubNodes(_,_,subnodes) ->
List.fold (fun acc (node:TrieNode) ->
let res = nextLevel(node,curlevel+1)
if (acc > res) then acc else res) curlevel subnodes
我所做的改变:
你写了nextLevel(...) = function ...
。 function
构造创建一个函数,在其上进行一些值和模式匹配(所以你编写了两个TrieNode
参数的函数)。我用简单的match
替换了它。
在你的lambda中你有let res = nextLevel(node,curlevel+1) and
- and
关键字不属于那里(你可以写let res = .. in
但是因为缩进而没有必要。)< / p>
您的lambda函数使用模式匹配来提取元组(node:TrieNode,_,_)
的元素,但subnodes
不是元组列表 - 只列出TrieNode
值。
答案 1 :(得分:2)
这是一个功能更强大的代码布局。与您的代码逻辑相同。我正在研究一种有效的解决方案。
module Trie =
type Node = Node of (char * bool * Node list) Option
let char = function
| Node(None) -> ' '
| Node(Some(c, _, _)) -> c
let getChild (c:char) = function
| Node(None) -> None
| Node(Some(c, weh, subnodes)) ->
List.tryFind (fun (node:Node) -> (char node) = c) subnodes
let rec insertWordChars (wordChars:char list) = function
| Node(None) -> Node(Some(wordChars.Head, false, [insertWordChars wordChars.Tail (Node(None))]))
| Node(Some(c, weh, subnodes)) as node ->
if wordChars.Length = 1 then
Node(Some(wordChars.Head, true, []))
else
match getChild (wordChars.Head) node with
| None -> Node(Some(wordChars.Head, false, (insertWordChars wordChars.Tail node)::subnodes))
| Some(child) -> Node(Some(wordChars.Head, false, (insertWordChars wordChars.Tail child)::subnodes))
let insertWord (s:string) = insertWordChars (List.ofSeq s)
打印高度。
let rec nextLevel (curlevel:int) = function
| Trie.Node(None) -> curlevel
| Trie.Node(Some(_, _, subnodes)) ->
List.fold (fun acc (node:Trie.Node) ->
let res = nextLevel (curlevel + 1) node
if (acc > res) then acc else res) curlevel subnodes
let trie =
Trie.Node(Some(' ', false, []))
|> Trie.insertWord("abc")
|> Trie.insertWord("abcd")
|> Trie.insertWord("abcd")
|> Trie.insertWord("abcde")
|> Trie.insertWord("abcdef")
|> Trie.insertWord("ab123cd")
|> Trie.insertWord("abc123d")
|> Trie.insertWord("abc132d")
printf "%A" (nextLevel 0 trie)
答案 2 :(得分:1)
我采取不同的方法:
let rec depth = function
| Nil -> 0
| SubNodes(_,_,l) ->
let d = l |> List.map depth |> List.max
d + 1
对我来说,这比使用fold的版本更容易阅读,但它会遍历子节点列表两次。
答案 3 :(得分:1)
考虑一下Trie,你的初步方法很接近,但要求所有内容都以相同的字母开头。这是一个使用chars的工作特里,就像你原来的想法而不是字符串。我将其留给读者作为实施string nodes and leafs.
的练习module Trie =
type Node = Node of (char * bool * Node) list
let empty = Node([])
let partition c = function
| Node(nodes) -> List.partition (fun (ct, _, _) -> ct = c) nodes
let rec insert wordChars node =
match wordChars, node with
| c::cx, Node([]) -> Node([c, cx.IsEmpty, (insert cx empty)])
| c::cx, _ ->
match partition c node with
| (ct, weh, children)::_, others ->
Node((c, (weh || cx.IsEmpty), insert cx children)::others)
| [] , others ->
Node((c, cx.IsEmpty, insert cx empty)::others)
| [], _ -> node
let insertWord (s:string) node = insert (List.ofSeq s) node
和一些测试
let rec nextLevel (curlevel:int) = function
| Trie.Node([]) -> curlevel
| Trie.Node(nodes) ->
List.fold (fun acc (_, _, node) ->
let res = nextLevel (curlevel + 1) node
if (acc > res) then acc else res) curlevel nodes
let rec print acc = function
| Trie.Node(nodes) ->
List.iter (fun (c, w, node) ->
let str = acc + c.ToString()
if w then printfn "%s" str
print str node) nodes
let trie =
Trie.empty
|> Trie.insertWord("abc")
|> Trie.insertWord("abcd")
|> Trie.insertWord("abcd")
|> Trie.insertWord("abcde")
|> Trie.insertWord("abcdef")
|> Trie.insertWord("ab123cd")
|> Trie.insertWord("abc123d")
|> Trie.insertWord("abc132d")
printf "%d\n" (nextLevel 0 trie)
print "" trie
输出
7
abc
abc132d
abc123d
abcd
abcde
abcdef
ab123cd