【问题标题】:Red black trees with classes in OCamlOCaml 中带类的红黑树
【发布时间】:2019-12-25 00:50:28
【问题描述】:

有谁知道如何在 OCaml 中用类实现红黑树? 至少是类属性和初始化器?我是 OCaml 的新手。

我尝试了什么:

type data  = {key: int; value: string}

class node (data: data) = 
   object (self)
   val mutable d = data
   val mutable color = 1
   val mutable left = ref (None: node option) 
   val mutable right = ref (None: node option) 
   val mutable parent = ref (None: node option) 

   method getLeft = left
   method getRight = right
   method getParent = parent
   method getColor = color
   method getData = d
   method setLeft (l: node option ref) = left := !l
   method setRight (l: node option ref) = right := !l
   method setParent (l: node option ref) = parent := !l
 end;;



class rbtree =
   object (self)
   val mutable root = ref (None: node option)
   val mutable nullNode = ref (None: node option)
   method searchNode (aNode: node option ref) (key: data) = begin
   if aNode = nullNode || key == (!aNode)#getData then aNode;
end;

end;;

我收到错误This expression has type node option It has no method getData

我正在尝试用 C++ 编写类似这样的代码:

 struct Node
 {
  int data;
  Node *parent;
  Node *left;
  Node *right;
  int color;
 };

 typedef Node *NodePtr;
 class RedBlackTree
 {
    private:
       NodePtr root;
       NodePtr TNULL;
       void initializeNULLNode(NodePtr node, NodePtr parent)
       {
           node->data = 0;
           node->parent = parent;
           node->left = nullptr;
           node->right = nullptr;
           node->color = 0;
       }

  NodePtr searchTreeHelper(NodePtr node, int key)
  {
     if (node == TNULL || key == node->data)
     {
      return node;
     }
     if (key < node->data)
     {
       return searchTreeHelper(node->left, key);
     }
     return searchTreeHelper(node->right, key);
  }
};

【问题讨论】:

  • 很难回答这个问题,因为没有明确的理由使用类来表示树类型。此外,您说“类”,就好像您希望拥有不止一个类。如果您更仔细地解释您的要求,这可能会有所帮助。但是,这听起来像是一项家庭作业,您应该准备好自己完成几乎所有的工作。特别是,您最好在编写一些代码并遇到特定问题后提出问题。
  • 感谢您的回答。是的你是对的。这是家庭作业。我现在将使用我尝试过的代码更新我的问题。
  • @JeffreyScofield,你能再看看吗?谢谢。
  • 关于错误This expression has type node option. It has no method getData,看起来这是因为该值的类型为node options,但您正试图访问node 类型的值的字段。所以你需要先把nodeoption中取出来。见stackoverflow.com/a/12288752/1187277
  • 请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。

标签: class oop ocaml binary-tree red-black-tree


【解决方案1】:

对于您看到的错误,@Shon 是正确的。你有这个表达式:

if aNode = nullNode || key == (!aNode)#getData then aNode;

aNode 的类型是node option ref。这意味着!anode 的类型是node option。选项值不是对象(类的实例)。所以不能在上面调用方法getData

同样,正如@Shon 所说,您需要检索!aNode 的值(如果有的话)。这将为您提供一个节点,该节点是一个对象,并且具有 getData 方法。

在您的if 测试中内联编写此代码会很麻烦。所以你可能会写一个这样的函数:

let node_option_has_value node_opt value =
   match node_opt with
   | None -> false
   | Some node -> node#getData = value

作为旁注,您不应该使用物理相等运算符 (==) 进行比较。这个运算符是针对特殊情况的,不是通用的。

OCaml 中的相等比较运算符是=(在我的示例代码中)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-06
    • 2010-09-06
    • 2013-06-05
    • 2018-02-07
    • 1970-01-01
    • 2015-11-17
    • 2012-11-30
    • 2014-07-05
    相关资源
    最近更新 更多