F#中如何实现二叉搜索树的add操作?
How to implement add operation in Binary Search Tree in F#?
我正在尝试实现 Algorithms 4th Edition
中的以下代码
private Node put(Node x, Key key, Value val)
{
if (x == null) return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if (cmp < 0) x.left = put(x.left, key, val);
else if (cmp > 0) x.right = put(x.right, key, val);
else x.val = val;
x.N = size(x.left) + size(x.right) + 1;
return x;
}
我在 F# 中提出了以下实现:
type Node = {
mutable Left : Node option
mutable Right : Node option
mutable Value : int
mutable Count : int
}
type BST() =
let root : Node option = None
member x.Put (value : int) =
let rec Add (node:Node option) value =
match node with
| None -> Some { Left = None; Right = None; Value = value; Count = 1 }
| Some t ->
match t with
| _ when t.Value < value -> t.Right <- Add t.Right value
| _ when t.Value > value -> t.Left <- Add t.Left value
| _ ->
t.Value <- value
t.Count <- (x.Size t.Left) + (x.Size t.Right) + 1
Some t
()
我收到错误:预期有类型节点选项,但这里作为单元,在以下行中:
| _ when t.Value < value -> t.Right <- Add t.Right value
| _ when t.Value > value -> t.Left <- Add t.Left value
上面的代码有没有更好的实现方式?我在复制函数式方法中的程序样式代码时是否犯了错误?
你收到错误是因为 None
匹配 returns Some Node
,所以你 有 来匹配 return 输入所有其他分支。
您可以在其他匹配中通过 return 匹配节点后解决该问题:
let rec Add (node:Node option) value =
match node with
| None -> Some { Left = None; Right = None; Value = value; Count = 1 }
| Some t ->
match t with
| _ when t.Value < value ->
t.Right <- Add t.Right value
Some t
| _ when t.Value > value ->
t.Left <- Add t.Left value
Some t
| _ ->
t.Value <- value
//t.Count <- (x.Size t.Left) + (x.Size t.Right) + 1
Some t
(您可能注意到我注释掉了倒数第二行,因为 x
没有 Size
成员。)
Am I making a mistake by copying a procedural style code as it is in functional approach?
可能吧。这取决于你的目标是什么。如果你想学习 F# 的 syntax,这可能是一个很好的练习。如果你想学习函数式编程,那么这样做是没有意义的。
我正在尝试实现 Algorithms 4th Edition
中的以下代码private Node put(Node x, Key key, Value val)
{
if (x == null) return new Node(key, val, 1);
int cmp = key.compareTo(x.key);
if (cmp < 0) x.left = put(x.left, key, val);
else if (cmp > 0) x.right = put(x.right, key, val);
else x.val = val;
x.N = size(x.left) + size(x.right) + 1;
return x;
}
我在 F# 中提出了以下实现:
type Node = {
mutable Left : Node option
mutable Right : Node option
mutable Value : int
mutable Count : int
}
type BST() =
let root : Node option = None
member x.Put (value : int) =
let rec Add (node:Node option) value =
match node with
| None -> Some { Left = None; Right = None; Value = value; Count = 1 }
| Some t ->
match t with
| _ when t.Value < value -> t.Right <- Add t.Right value
| _ when t.Value > value -> t.Left <- Add t.Left value
| _ ->
t.Value <- value
t.Count <- (x.Size t.Left) + (x.Size t.Right) + 1
Some t
()
我收到错误:预期有类型节点选项,但这里作为单元,在以下行中:
| _ when t.Value < value -> t.Right <- Add t.Right value
| _ when t.Value > value -> t.Left <- Add t.Left value
上面的代码有没有更好的实现方式?我在复制函数式方法中的程序样式代码时是否犯了错误?
你收到错误是因为 None
匹配 returns Some Node
,所以你 有 来匹配 return 输入所有其他分支。
您可以在其他匹配中通过 return 匹配节点后解决该问题:
let rec Add (node:Node option) value =
match node with
| None -> Some { Left = None; Right = None; Value = value; Count = 1 }
| Some t ->
match t with
| _ when t.Value < value ->
t.Right <- Add t.Right value
Some t
| _ when t.Value > value ->
t.Left <- Add t.Left value
Some t
| _ ->
t.Value <- value
//t.Count <- (x.Size t.Left) + (x.Size t.Right) + 1
Some t
(您可能注意到我注释掉了倒数第二行,因为 x
没有 Size
成员。)
Am I making a mistake by copying a procedural style code as it is in functional approach?
可能吧。这取决于你的目标是什么。如果你想学习 F# 的 syntax,这可能是一个很好的练习。如果你想学习函数式编程,那么这样做是没有意义的。