【问题标题】:Store Node children in mutable R-tree - Scala将节点子节点存储在可变 R 树中 - Scala
【发布时间】:2020-03-07 08:54:45
【问题描述】:

将节点的子节点存储在可变 R 树中的最佳集合是什么。

由于我的树是可变的,我的猜测可能是 ListBuffer、List、ArrayBuffer。但我不确定该选哪一个。我需要一个在移除和插入方面都具有相当复杂性的集合。

【问题讨论】:

    标签: scala nodes children r-tree


    【解决方案1】:

    可以结合ArrayBuffer, PriorityQueue来实现

    一个简单的非详尽示例

    object RTree {
    
      /**
       * Construct an empty RTree.
       */
      def empty[A]: RTree[A] = new RTree(Node.empty[A], 0)
    
      /**
       * Construct an RTree from a sequence of entries.
       */
      def apply[A](entries: Entry[A]*): RTree[A] =
        entries.foldLeft(RTree.empty[A])(_ insert _)
    }
    
    
    case class RTree[A](root: Node[A], size: Int) {
    
      /**
       * To be considered equal, two trees must have the same
       * number of entries, and each entry found in one should be found in
       * the other.
       */
      def ===(that: RTree[A]): Boolean =
        size == that.size && entries.forall(that.contains)
    
      /**
       * Trees can only be equal to other trees. Unlike some other
       * containers.
       *
       * This means comparing an RTree[Int] and an RTree[Long] will
       * always return false.
       */
      override def equals(that: Any): Boolean =
        that match {
          case rt: RTree[_] =>
            Try(this === rt.asInstanceOf[RTree[A]]).getOrElse(false)
          case _ =>
            false
        }
    
    
      /**
       * Insert a value into the tree at (x, y), returning a new tree.
       */
      def insert(x: Float, y: Float, value: A): RTree[A] =
        insert(Entry(Point(x, y), value))
    
      /**
       * Insert an entry into the tree, returning a new tree.
       */
      def insert(entry: Entry[A]): RTree[A] = {
        val r = root.insert(entry) match {
          case Left(rs) => Branch(rs, rs.foldLeft(Box.empty)(_ expand _.box))
          case Right(r) => r
        }
        RTree(r, size + 1)
      }
    
      /**
       * Remove an entry from the tree, returning a new tree.
       *
       * If the entry was not present, the tree is simply returned.
       */
      def remove(entry: Entry[A]): RTree[A] =
        root.remove(entry) match {
          case None =>
            this
          case Some((es, None)) =>
            e.foldLeft(RTree.empty[A])(_ insert _)
          case Some((e, Some(node))) =>
            e.foldLeft(RTree(node, size - e.size - 1))(_ insert _)
        }
    
      /**
       * Return a sequence of all entries found in the given search space.
       */
      def search(space: Box): Seq[Entry[A]] =
        root.search(space, _ => true)
    
      /**
       * Return a sequence of all entries found in the given search space.
       */
      def search(space: Box, f: Entry[A] => Boolean): Seq[Entry[A]] =
        root.search(space, f)
    

    【讨论】:

    • 为什么我更喜欢 ArrayBuffer 而不是 ListBuffer ?
    • 列表缓冲区是在链表上实现的,而数组缓冲区是基于数组的,因此与链表相比,数组中的索引查找通常更快。你可以在这里找到更多信息stackoverflow.com/questions/2712877/…。如果这有帮助,请接受答案。
    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2019-06-06
    相关资源
    最近更新 更多