bobier2
bobier2

Reputation: 147

Cannot cast from inner class to class

I have a BinaryTree class which contains an inner class Node. What I would like to do is to be able to insert some nodes in my BinaryTree tree by calling tree.insert(node). However, to keep it clean and consistent, I dont want to create an insert() method inside Node inner class. So I tried the code below, but I have an error: Cannot cast from BinaryTree.Node to BinaryTree.

What should I do?

BinaryTree class

public class BinaryTree {

    Node root = null;

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


    }

    public BinaryTree(int v) {
        root.value = v;
        root.left = null;
        root.right = null;
    }

    public void insert(Node n) {
                                      /* Error */
        if(n.value > root.value)    ((BinaryTree) root.right).insert(n);
    }

}

Main class

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        String[] str = sc.nextLine().split(" ");
        BinaryTree tree;

        for(int i = 0; i < str.length-1; i++) {
            int val = Integer.parseInt(str[i]);
            //tree.insert(node);
        }

    }

}

Thanks,

Upvotes: 0

Views: 120

Answers (2)

curlyBraces
curlyBraces

Reputation: 1105

You don't need typecasting inside insert method. It should be like this:

public void insert(Node n) {                             
   if(n.value > root.value)  
      insert(root.right);
}

Upvotes: 2

c0der
c0der

Reputation: 18792

To insert a node in a tree you need to define where to insert it, so your insert methods should be something like:

//insert a new node right to a node. not null safe 
public void insert(Node newNode, Node rightTo) {                                    
    newNode.right = rightTo.right;
    newNode.left = rightTo;
    rightTo.right = newNode;
}

which does not require casting.
To find the rightTo node you could use:

//get the last node which has a value lower than `value` 
//may return null 
public Node getNodeWithValueLowerThan(int value) {
    if(root == null) return null;
    return getNodeWithValueLowerThan(root, value);
}

//recursive method. null safe 
private Node getNodeWithValueLowerThan(Node node, int value) {
    if(node == null) return null; 
    if(node.value > value) return node.left; //return previous node. may be null
    return getNodeWithValueLowerThan(node.right, value);
}

To insert a node as a last node, you could use:

//insert a new node as last 
public void insertLastNode(Node newNode) {

    Node lastNode = getTail();
    if(lastNode == null) {//empty tree 
        root = newNode;
        return;
    }
    newNode.left  = lastNode;
    lastNode.right = newNode;
}

where getTail is something like:

//find last node 
private Node getTail() {

    if(root == null) return null;
    return getTail(root);
}

//recursive method to find last node. not null safe 
private Node getTail(Node node) {

    if(node.right == null) return node;
    return getTail(node.right);
}

Note: code was not tested so debug carefully.

Upvotes: 0

Related Questions