-1

I'm trying to implement a binary tree data structure in java. This is my code. I'm very new to java and don't quite understand where my errors are. Thank you. I got this error message: binTree.java:21: error: class, interface, or enum expected

class Node {
int value;
Node left;
Node right;

Node(int value) {
    this.value = value;
    right = null;
    left = null;
}
}

public class BinTree {

Node root;

}
private Node addRecursive(Node current, int value) {
if (current == null) {
    return new Node(value);
}

if (value < current.value) {
    current.left = addRecursive(current.left, value);
} else if (value > current.value) {
    current.right = addRecursive(current.right, value);
} else {
    // value already exists
    return current;
}

return current;
}
public void add(int value) {
root = addRecursive(root, value);
}
System.out.println();
    System.out.println("testcase 1:");
    BinTree binTree2 = new BinTree();
    binTree2.insertNode(new Node(3));
    binTree2.insertNode(new Node(201));
    binTree2.insertNode(new Node(60));
    binTree2.insertNode(new Node(30));
    binTree2.insertNode(new Node(45));
    binTree2.treeWalk();
3
  • 3
    Fix your indentation and check your braces. In Java you can't have methods outside of a class, and you can't have statements outside of a method or initializer block. Commented Jul 9, 2018 at 23:38
  • Check this for complete step by step: baeldung.com/java-binary-tree Commented Jul 9, 2018 at 23:57
  • Possible duplicate of BinaryTree implementation in java Commented Jul 10, 2018 at 0:04

2 Answers 2

0

You used a closing bracket at the line 21 just after the Node root field where you shouldn't Also, you will need a main methods or Junit tedt case to launch your test (at the bottom, starting before your System.out.println())

Sign up to request clarification or add additional context in comments.

Comments

0

your methods need to be placed inside of the class BinTree and the code you want to execute needs to be wrapped in a main function like so

public class BinTree {

    Node root;


    private Node addRecursive(Node current, int value) {
        if (current == null) {
            return new Node(value);
        }

        if (value < current.value) {
            current.left = addRecursive(current.left, value);
        } else if (value > current.value) {
            current.right = addRecursive(current.right, value);
        } else {
        // value already exists
            return current;
        }

        return current;
    }

    public void add(int value) {
        root = addRecursive(root, value);
    }

    public static void main(String []args){
        System.out.println();
        System.out.println("testcase 1:");
        BinTree binTree2 = new BinTree();
        binTree2.insertNode(new Node(3));
        binTree2.insertNode(new Node(201));
        binTree2.insertNode(new Node(60));
        binTree2.insertNode(new Node(30));
        binTree2.insertNode(new Node(45));
        binTree2.treeWalk();
    }

}

It looks like you're missing some methods like insertNode and treeWalk but it should compile after you have implemented those.

2 Comments

Thank you! Second question: How would I implement those methods?
Is this for a school assignment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.