0

I get error

In BinarySearchTree.jack (line 20): In subroutine insert: Expected (
In BinarySearchTree.jack (line 25): In subroutine insert: Expected ( I have 2 classes, Node:

class Node { 
    field int key; 
        field Node left, right; 
   
    constructor Node new(int data){
        let key = data; 
        let left = null;
        let right = null;
        return this;
    }
    method int getKey() { return key; }
    method Node getLeft() { return left; }
    method Node getRight() { return right; }
    method void setKey(int _key) {let key=_key; return;}
    method void setLeft(Node _left) {let left=_left; return;}
    method void setRight(Node _right) {let right=_right; return;}
}

And BinarySearchTree:

class BinarySearchTree {
    field Node root;

    constructor BinarySearchTree new() {
        let root = null;
        return this;
    }

    method void insertBST(int num) {
        let root = BinarySearchTree.insert(num,root);
        return;
    }
    method Node insert(int num,Node p) {
        var Node tempNode;
        if (p = null){
            let p = Node.new(num);
            return p;
        }
        if (num < p.getKey()){
            let tempNode = BinarySearchTree.insert(num,p.left);
            do p.setLeft(tempNode);
            return p;
        }
        if (num > p.getKey()){
            let tempNode= BinarySearchTree.insert(num,p.right);
            do p.setRight(tempNode);
            return p;
        }
        do Output.printString("Item in tree and not inserted.");
        return p;
    }
}

How can i fix this error?

1 Answers1

0

The problem is p.left and p.right. You can't access fields directly like in other programming languages.

Use p.getLeft() p.getRight(). The error message means that the compiler doesn't know about expressions that have a "." after an classname, but have no "(" after the methode or function name. So it tells you to add a ( after the function or methodename. It won't check that this isn't a function or methode name, but a field if there are no brackets, so the error message is confusing.

apppelll
  • 1
  • 2