我使用的是Scala 2.11和Scala IDE。我有以下问题: 我有2个文件,其中一个实现了Weight Biased Leftist Heap和一个驱动程序,它是一个对象,主要方法。
WBLHeap.scala
package heaps
abstract class WBLHeap[+A] {
/**
* O(1)
* @return weight of the heap
*/
def weight() : Int
/**
*
*/
def isWeightedLeftist(): Boolean
/**
* For any WBLT with n elements, the length of its right spine
* is <= floor(log2(n+1))
*/
def rightSpine() : List[A]
/**
* O(log(n))
* @return A WBLHeap with the heap this and the heap h
*/
def merge[B >: A](h: WBLHeap[B]) : WBLHeap[B]
case object EmptyHeap extends WBLHeap[Nothing] {
def weight(): Int = 0
def isWeightedLeftist(): Boolean = true
def rightSpine() : List[Nothing] = List()
def merge[B >: Nothing](h: WBLHeap[B]) : WBLHeap[B] = h
}
case class Node[A](elem: A, weightNode: Int, left: WBLHeap[A], right: WBLHeap[A]) extends WBLHeap[A] {
def weight(): Int = weightNode
def isWeightedLeftist(): Boolean =
left.weight >= right.weight && left.isWeightedLeftist() &&
right.isWeightedLeftist()
def rightSpine() : List[A] = elem :: right.rightSpine()
def merge[B >: A](h: WBLHeap[B]) : WBLHeap[B] = h match {
case EmptyHeap => this
case Node(e, w, l: WBLHeap[B], r: WBLHeap[B]) if this.weightNode <= w => buildNode(elem, left, r.merge(h) )
case Node(e: B, w, l: WBLHeap[B], r: WBLHeap[B]) if this.weightNode > w => buildNode(e, l, this.merge(r))
//There is a warning here but I don't know why.
//abstract type pattern A is unchecked since it is eliminated by erasure
}
private def buildNode[B >: A](x: B, h1: WBLHeap[B], h2: WBLHeap[B]): WBLHeap[B] = {
val w1 = h1.weight()
val w2 = h2.weight()
val newWeight = w1 + w2 + 1
if(w1 >= w2)
return new Node[B](x, newWeight, h1, h2)
else
return new Node[B](x, newWeight, h2, h1)
}
}
}
Driver.scala
package heaps
object Driver {
def main(args:Array[String]) = {
val h = new Node[Char]('b', 2,
new Node[Char]('c', 1, EmptyHeap(), EmptyHeap()),
EmptyHeap())
}
}
在行中:&#34; val h =新节点[Char](&#39; b&#39;,2,&#34;,我有一个错误:找不到:输入Node。它也会发生每次我使用对象EmptyHeap。
有人知道我做错了吗?
谢谢!
答案 0 :(得分:3)
将Node
定义移出abstract class WBLHeap
,将abstract class WBLHeap
更改为object
,或将extends WBLHeap[...]
添加到Driver
,然后{ {1}}可以访问。