Get error: Incomparable types - Object and int

My code is below, but when I create a project Netbeat gives me this warning:

Error: incomparable types: Object and int if (p.info == x) {

Does anyone know how to fix this error? Thank.

Here's the error code:

    public void insert(int x) {
    if (root == null) {
        root = new Node(x);
        return;
    }
    Node f, p;
    p = root;
    f = null;
    while (p != null) {
        if (p.info == x) {
            System.out.println(" The key " + x + " already exists, no insertion");
            return;
        }

        f = p;
        if (x < (int) p.info) {
            p = p.left;
        } else {
            p = p.right;
        }
    }
    if (x < (int) f.info) {
        f.left = new Node(x);
    } else {
        f.right = new Node(x);
    }
}

      

Here's all the code I have so far:

package ass6;

import java.util.ArrayList;

public class Ass6 {

    public static void main(String[] args) {
        BinarySearchTree bsTree = new BinarySearchTree();
        String[] c = {"C6", "C2", "C7", "C1", "C5", "C9", "C4", "C8", "C3"};
        bsTree.insertMany(c);

        System.out.println("Calculate level of all nodes:");
        bsTree.breadth_first_traverse3();
        System.out.println("");
        bsTree.calLevel(bsTree.root, 1);
        bsTree.breadth_first_traverse();
        System.out.println("");

        System.out.println("\nCalculate the height of the tree:");
        bsTree.calheight(bsTree.root);
        bsTree.breadth_first_traverse1();
        System.out.println("");

        System.out.println("Calculate balance factor of all nodes:");
        bsTree.breadth_first_traverse3();
        System.out.println("");
        bsTree.calbaFactor(bsTree.root);
        bsTree.breadth_first_traverse2();
        bsTree.AVL(bsTree.root);
        System.out.println("");

        System.out.println("Balance a binary search tree:");
        bsTree.breadth_first_traverse3();
        System.out.println("");
        bsTree.balance();
        bsTree.breadth_first_traverse3();
        System.out.println("");
    }
}

class BinarySearchTree {

    Node root;
    int height;
    int i;

    void clear() {
        root = null;
    }

    public BinarySearchTree() {
        root = null;
    }

    Node search(Node p, int x) {
        if (p == null) {
            return (null);
        }
        if ((int) p.info == x) {
            return (p);
        }
        if (x < (int) p.info) {
            return (search(p.left, x));
        } else {
            return (search(p.right, x));
        }
    }

    public void insertMany(int a[]) {
        for (int x : a) {
            insert(x);
        }
    }

    public void insertMany(String a[]) {
        for (String x : a) {
            insert(x);
        }
    }

    public void insert(String x) {
        if (root == null) {
            root = new Node(x);
            return;
        }
        Node f, p;
        p = root;
        f = null;
        while (p != null) {
            if (p.info == x) {
                System.out.println(" The key " + x + " already exists, no insertion");
                return;
            }

            f = p;
            if (x.compareTo(p.info.toString()) < 0) {
                p = p.left;
            } else {
                p = p.right;
            }
        }
        if (x.compareTo(f.info.toString()) < 0) {
            f.left = new Node(x);
        } else {
            f.right = new Node(x);
        }
    }

    public void insert(int x) {
        if (root == null) {
            root = new Node(x);
            return;
        }
        Node f, p;
        p = root;
        f = null;
        while (p != null) {
            if (p.info == x) {
                System.out.println(" The key " + x + " already exists, no insertion");
                return;
            }

            f = p;
            if (x < (int) p.info) {
                p = p.left;
            } else {
                p = p.right;
            }
        }
        if (x < (int) f.info) {
            f.left = new Node(x);
        } else {
            f.right = new Node(x);
        }
    }

    public void visitLevel(Node x) {
        System.out.print("(" + x.info + "," + x.level + "), ");
    }

    public void visit(Node x) {
        System.out.print(x.info + ", ");
    }

    public void visitheight(Node x) {
        System.out.print("(" + x.info + "," + x.height + "), ");
    }

    public void visitbafator(Node x) {
        System.out.print("(" + x.info + "," + x.baFactor + "), ");
    }

    public void breadth_first_traverse() {
        MyQueue queue = new MyQueue();
        Node p = root;
        if (p != null) {
            queue.enqueue(p);
        }
        while (!queue.isEmpty()) {
            Node q = (Node) queue.dequeue();
            visitLevel(q);
            if (q.left != null) {
                queue.enqueue(q.left);
            }
            if (q.right != null) {
                queue.enqueue(q.right);
            }
        }
    }

    public void breadth_first_traverse1() {

        MyQueue queue = new MyQueue();
        Node p = root;
        if (p != null) {
            queue.enqueue(p);
        }
        while (!queue.isEmpty()) {
            Node q = (Node) queue.dequeue();
            visitheight(q);
            if (q.left != null) {
                queue.enqueue(q.left);
            }
            if (q.right != null) {
                queue.enqueue(q.right);
            }
        }
        System.out.println("");
    }

    public void breadth_first_traverse2() {

        MyQueue queue = new MyQueue();
        Node p = root;
        if (p != null) {
            queue.enqueue(p);
        }
        while (!queue.isEmpty()) {
            Node q = (Node) queue.dequeue();
            visitbafator(q);
            if (q.left != null) {
                queue.enqueue(q.left);
            }
            if (q.right != null) {
                queue.enqueue(q.right);
            }
        }
        System.out.println("");

    }

    public void breadth_first_traverse3() {
        MyQueue queue = new MyQueue();
        Node p = root;
        if (p != null) {
            queue.enqueue(p);
        }
        while (!queue.isEmpty()) {
            Node q = (Node) queue.dequeue();
            visit(q);
            if (q.left != null) {
                queue.enqueue(q.left);
            }
            if (q.right != null) {
                queue.enqueue(q.right);
            }
        }
    }

    public void inorder(Node x) {
        if (x == null) {
            return;
        }
        inorder(x.left);
        visit(x);
        inorder(x.right);
    }

    public void calLevel(Node n, int i) {
        if (n.left == null && n.right == null) {
            n.level = i;
        } else {
            n.level = i;
            if (n.left != null) {
                calLevel(n.left, i + 1);
            }
            if (n.right != null) {
                calLevel(n.right, i + 1);
            }
        }
    }

    public int treeheight(Node p) {
        if (p == null) {
            return 0;
        }
        int left = treeheight(p.left);
        int right = treeheight(p.right);
        return 1 + Math.max(left, right);

    }

    public void calheight(Node n) {
        if (n.left == null && n.right == null) {
            n.height = 1;
        } else {
            n.height = treeheight(n);
            if (n.left != null) {
                calheight(n.left);
            }
            if (n.right != null) {
                calheight(n.right);
            }
        }
    }

    public void calbaFactor(Node n) {
        if (n.left == null && n.right == null) {
            n.baFactor = 0;
        } else {
            n.baFactor = treeheight(n.right) - treeheight(n.left);
            if (n.baFactor > 1 || n.baFactor < -1) {
                i++;
            }
            if (n.left != null) {
                calbaFactor(n.left);
            }
            if (n.right != null) {
                calbaFactor(n.right);
            }
        }
    }

    public void AVL(Node n) {

        if (i > 0) {
            System.out.println("The tree is not an AVL tree .");
        } else {
            System.out.println("The tree is an AVL tree .");
        }
    }

    void inOrder(ArrayList<String> t, Node p) {
        if (p == null) {
            return;
        }
        inOrder(t, p.left);
        t.add((String) p.info);
        inOrder(t, p.right);
    }

    void balance(ArrayList<String> t, int i, int j) {
        if (i > j) {
            return;
        }
        int k = (i + j) / 2;
        String x = t.get(k);
        insert(x);
        balance(t, i, k - 1);
        balance(t, k + 1, j);
    }

    void balance() {
        ArrayList<String> t = new ArrayList<>();
        inOrder(t, root);
        int n = t.size();
        clear();
        balance(t, 0, n - 1);
    }
}

class Node {

    Object info;
    int level = 1;
    int height = 0;
    int baFactor = 0;
    Node left, right;

    public Node(Object inf, Node le, Node ri) {
        info = inf;
        left = le;
        right = ri;
    }

    public Node(Object inf) {
        info = inf;
        left = right = null;
    }
}

class MyQueue {

    ArrayList al = new ArrayList();

    MyQueue() {
    }

    public void enqueue(Object x) {
        al.add(x);
    }

    public Object dequeue() {
        Object obj = al.get(0);
        al.remove(0);
        return obj;
    }

    public boolean isEmpty() {
        if (al.size() <= 0) {
            return true;
        } else {
            return false;
        }
    }
}

      

+3


source to share


2 answers


In java, you cannot compare primitive types (int, float, double, byte, etc.) to instance types (everything is derived from Object and then instantiated).



Use p.info.equals (x)

+6


source


here p.info

is Object

and x

is int

, so you cannot simply compare with ==

. but

.equals

will work because the value will be compared.



 Object obj = Integer.valueOf(3);
    int x=3;

    if ( obj.equals(x) ) { // true

    }

      

0


source







All Articles