如何找到下面给出的BST的最小值和最大值?

发布于 2025-01-22 19:34:20 字数 837 浏览 0 评论 0原文

以下是我当前拥有的代码。不确定它是否正确,但是数据类型行必须保持不变。我正在尝试创建一个getmin和getmax功能。以下是对此的要求。

datatype tree = leaf | node of int * tree * tree;

fun isEmpty leaf = if leaf=nil then true else false;

fun inorder leaf = nil
  | inorder (node(key,left,right)) = inorder left @ [key] @ inorder right;

fun preorder leaf = nil
  | preorder (node(key,left,right)) = preorder left @ preorder right @ [key];

fun postorder leaf = nil
  | postorder (node(key,left,right)) = [key] @ preorder right @ preorder left;

(* Inputs a BST. Returns the smallest value in the tree. If the tree is empty, the function should return NONE. *)
    getMin : tree -> int option

(* Inputs a BST. Returns the largest value in the tree. If the tree is empty, the function should return NONE. *)
    getMax : tree -> int option

对此的任何帮助将不胜感激。谢谢!

Below is the code I currently have. Not sure if it is even right, but the datatype line MUST remain the same. I am trying to create a getMin and getMax function. Below will be the requirements for it.

datatype tree = leaf | node of int * tree * tree;

fun isEmpty leaf = if leaf=nil then true else false;

fun inorder leaf = nil
  | inorder (node(key,left,right)) = inorder left @ [key] @ inorder right;

fun preorder leaf = nil
  | preorder (node(key,left,right)) = preorder left @ preorder right @ [key];

fun postorder leaf = nil
  | postorder (node(key,left,right)) = [key] @ preorder right @ preorder left;

(* Inputs a BST. Returns the smallest value in the tree. If the tree is empty, the function should return NONE. *)
    getMin : tree -> int option

(* Inputs a BST. Returns the largest value in the tree. If the tree is empty, the function should return NONE. *)
    getMax : tree -> int option

Any help with this will be greatly appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水晶透心 2025-01-29 19:34:20
fun isLeaf (node(_, _, _)) = false
  | isLeaf leaf = true;

fun getMin(node(x, left, _)) =
    if isLeaf left then x
    else getMin left
  | getMin leaf = raise Empty;

fun getMax(node(x,_,right)) =
    if isLeaf right then x
    else getMax right
  | getMax leaf = raise Empty;
fun isLeaf (node(_, _, _)) = false
  | isLeaf leaf = true;

fun getMin(node(x, left, _)) =
    if isLeaf left then x
    else getMin left
  | getMin leaf = raise Empty;

fun getMax(node(x,_,right)) =
    if isLeaf right then x
    else getMax right
  | getMax leaf = raise Empty;
年少掌心 2025-01-29 19:34:20

通过这个思考。 leafnode。 A 节点包含一个值和两个其他tree s。

的最大值是多少?它没有值,因此是

节点的最大值是多少?好吧,这是两个的最大值:

  • 它的价值。
  • 其左分支的最大值。
  • 或者,其右分支的最大值。

您如何找到三个int选项值的最大值?一种方法可能是:

fun max(NONE,   NONE,   NONE)   = NONE
  | max(SOME a, NONE,   NONE)   = SOME a
  | max(NONE,   SOME b, NONE)   = SOME b
  | max(NONE,   NONE,   SOME c) = SOME c
  | max(SOME a, SOME b, NONE)   = if a > b then SOME a else SOME b
  | max(SOME a, NONE,   SOME c) = if a > c then SOME a else SOME c
  | max(NONE,   SOME b, SOME c) = if b > c then SOME b else SOME c
  | max(SOME a, SOME b, SOME c) = 
    if a > b andalso a > c then SOME a
    else if b > a andalso b > c then SOME b
    else SOME c

Think this through. A tree is either a leaf or a node. A node contains a value and two other trees.

What is the maximum value of a leaf? It doesn't have a value, so it's NONE.

What's the maximum value of a node? Well, it's the maximum of either:

  • Its value.
  • The maximum value of its left branch.
  • Or, the maximum value of its right branch.

How do you find the maximum value of three int option values? One way might be:

fun max(NONE,   NONE,   NONE)   = NONE
  | max(SOME a, NONE,   NONE)   = SOME a
  | max(NONE,   SOME b, NONE)   = SOME b
  | max(NONE,   NONE,   SOME c) = SOME c
  | max(SOME a, SOME b, NONE)   = if a > b then SOME a else SOME b
  | max(SOME a, NONE,   SOME c) = if a > c then SOME a else SOME c
  | max(NONE,   SOME b, SOME c) = if b > c then SOME b else SOME c
  | max(SOME a, SOME b, SOME c) = 
    if a > b andalso a > c then SOME a
    else if b > a andalso b > c then SOME b
    else SOME c
寻找我们的幸福 2025-01-29 19:34:20

更加惯用地,使用更精确的模式匹配,并遵循int选项规范,而不是使事物复杂化,而异常:

fun getMin (node (x, leaf, _)) = SOME x
  | getMin (node (_, left, _)) = getMin left
  | getMin leaf = NONE

fun getMax (node (x, _, leaf)) = SOME x
  | getMax (node (_, _, right)) = getMax right
  | getMax leaf = NONE

More idiomatically, using more precise pattern matching, and following the int option specification instead of complicating things with exceptions:

fun getMin (node (x, leaf, _)) = SOME x
  | getMin (node (_, left, _)) = getMin left
  | getMin leaf = NONE

fun getMax (node (x, _, leaf)) = SOME x
  | getMax (node (_, _, right)) = getMax right
  | getMax leaf = NONE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文