我的代码如下,但是当我构建项目时,Netbeat给了我这个警告:
错误:无法比较的类型:Object和int if(p.info == 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);
}
}
这是我到目前为止的所有代码:
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;
}
}
}
答案 0 :(得分:5)
在java中,你不能将基本类型(int,float,double,byte等)与实例类型(从Object派生的所有内容然后实例化)进行比较。
使用p.info.equals(x)
答案 1 :(得分:0)
此处<a href="{{url_for('forget_reset', token='hello')}}">Calling Forget Reset</a>
http://127.0.0.1:5000/forget/hello
为p.info
,Object
为x
,因此您无法简单地与int
进行比较。但
==
将起作用,因为价值将被比较。
.equals