zamsler
zamsler

Reputation: 307

Scala inner classes

I am struggling with using inner classes in Scala. I am trying to implement a tree that uses a subclass of Node. I keep getting the error that type Node is not being found. I keep getting the error in both of the constructors.

class avlTree[T] private (var root:Node) {
    class Node (val element:T){
        var leftChild:Node = _;
        var rightChild:Node = _;
        var height:Int=0;
    }

    def this(x:T) = this(new Node(x))

} 

Upvotes: 0

Views: 41

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170723

If Node is an inner class, this means each Node belongs to an avlTree and you can't create a Node without first having the tree it'll belong to:

val tree: avlTree[Int] = ...
new tree.Node(1)

So your constructor would mean that to create a tree you need to have a node which belongs to this tree and so the tree must already exist.

Upvotes: 1

Related Questions