F# 我想我得到了这个BST的无限循环
我从Alexander Battisti那里得到的代码是关于如何从数据列表生成树的:F# 我想我得到了这个BST的无限循环,f#,tree,F#,Tree,我从Alexander Battisti那里得到的代码是关于如何从数据列表生成树的: let data = [4;3;8;7;10;1;9;6;5;0;2] type Tree<'a> = | Node of Tree<'a> * 'a * Tree<'a> | Leaf let rec insert tree element = match element,tree with | x,Leaf ->
let data = [4;3;8;7;10;1;9;6;5;0;2]
type Tree<'a> =
| Node of Tree<'a> * 'a * Tree<'a>
| Leaf
let rec insert tree element =
match element,tree with
| x,Leaf -> Node(Leaf,x,Leaf)
| x,Node(l,y,r) when x <= y -> Node((insert l x),y,r)
| x,Node(l,y,r) when x > y -> Node(l,y,(insert r x))
| _ -> Leaf
let makeTree = List.fold insert Leaf data
结果是没有,因为我有一个无限循环
有人能帮我吗?如果我的代码错误,请帮助我更正,谢谢让rec BinarySearch树元素=
let rec BinarySearch tree element =
match tree with
| Leaf -> false
| Node(l, v, r) ->
if v = element then
true
elif v < element then
BinarySearch r element
else
BinarySearch l element
BinarySearch makeTree 5
匹配树
|叶->假
|节点(l、v、r)->
如果v=元素,则
真的
elif v<元素
二进制搜索r元素
其他的
二进制搜索l元素
二进制搜索生成树5
让rec二进制搜索树元素=
匹配树
|叶->假
|节点(l、v、r)->
如果v=元素,则
真的
elif v<元素
二进制搜索r元素
其他的
二进制搜索l元素
二进制搜索生成树5
尹的解决方案是我也将如何编写它
无论如何,这里有一个更接近您的版本的解决方案,并且(希望)解释了出现的问题:
let rec BinarySearch tree element =
match element,tree with
| x, Leaf ->
// You originally called 'BinarySearch' here, but that's wrong - if we reach
// the leaf of the tree (on the path from root to leaf) then we know that the
// element is not in the tree so we return false
false
| x, Node(l,y,r) when x<y ->// This needs to be 'x<y', otherwise the clause would be
// matched when 'x=y' and we wouldn't find the element!
BinarySearch l element // Your recursive call was 'BinarySearch l y' but
// that's wrong - you want to search for 'element'
| x, Node(l,y,r) when x>y ->
BinarySearch r element
| x,Node(l,y,r) -> // You can simplify the code by omitting the 'when'
true // clause (because this will only be reached when
// x=y. Then you can omit the last (unreachable) case
让rec二进制搜索树元素=
将元素、树与
|x,叶->
//您最初在这里称为“BinarySearch”,但这是错误的-如果我们达到
//树的叶子(在从根到叶的路径上),然后我们知道
//元素不在树中,因此返回false
假的
|x,Node(l,y,r)当x//这需要是'x由Yin提出的解决方案也是我写它的方式
无论如何,这里有一个更接近您的版本的解决方案,并且(希望)解释了出现的问题:
let rec BinarySearch tree element =
match element,tree with
| x, Leaf ->
// You originally called 'BinarySearch' here, but that's wrong - if we reach
// the leaf of the tree (on the path from root to leaf) then we know that the
// element is not in the tree so we return false
false
| x, Node(l,y,r) when x<y ->// This needs to be 'x<y', otherwise the clause would be
// matched when 'x=y' and we wouldn't find the element!
BinarySearch l element // Your recursive call was 'BinarySearch l y' but
// that's wrong - you want to search for 'element'
| x, Node(l,y,r) when x>y ->
BinarySearch r element
| x,Node(l,y,r) -> // You can simplify the code by omitting the 'when'
true // clause (because this will only be reached when
// x=y. Then you can omit the last (unreachable) case
让rec二进制搜索树元素=
将元素、树与
|x,叶->
//您最初在这里称为“BinarySearch”,但这是错误的-如果我们达到
//树的叶子(在从根到叶的路径上),然后我们知道
//元素不在树中,因此返回false
假的
|x,节点(l,y,r),当x//这需要是'x
let rec BinarySearch tree element =
match element,tree with
| x, Leaf ->
// You originally called 'BinarySearch' here, but that's wrong - if we reach
// the leaf of the tree (on the path from root to leaf) then we know that the
// element is not in the tree so we return false
false
| x, Node(l,y,r) when x<y ->// This needs to be 'x<y', otherwise the clause would be
// matched when 'x=y' and we wouldn't find the element!
BinarySearch l element // Your recursive call was 'BinarySearch l y' but
// that's wrong - you want to search for 'element'
| x, Node(l,y,r) when x>y ->
BinarySearch r element
| x,Node(l,y,r) -> // You can simplify the code by omitting the 'when'
true // clause (because this will only be reached when
// x=y. Then you can omit the last (unreachable) case