我试图在Scala中实现简单的二叉搜索树。每个二叉搜索树都有两个类型参数:存储值的键类型和存储元素的类型。 二进制搜索树的算法需要比较密钥<运营商。 如何使Scala接受仅作为Key参数的类<运营商定义? 在下面的代码中,Scala抛出错误,"值<不是Key"类型的成员。
class Tree[Key >: Null, T >: Null] {
//Node class
class Node(startLeft: Node = null,
startRight: Node = null,
startUp: Node = null,
startKey: Key = null,
startValue: T = null) {
//Node fields
var left = startLeft;
var right = startRight
var up = startUp
var key = startKey
var value = startValue
//insert element to node
def insert(key: Key, value: T, node: Node = root):Node = {
var nodeCopy = node
if(nodeCopy == null) {
nodeCopy = new Node(startKey = key, startValue = value)
return nodeCopy
}
else if(nodeCopy.key < key) //ERROR IS HERE
insert(key, value, nodeCopy.left)
else
insert(key, value, nodeCopy.right)
}