Java 二叉树中的泛型,类型不兼容错误
Java Generics in a Binary Tree, incompatible types error
我正在尝试使用泛型在 java 中实现二叉树,我进行了搜索,发现了这个问题:Implementing Binary Tree in Java with Generic Comparable<T> data?,但我无法解决我的疑虑。所以我有两个 classes,
BST_Tree<T>
和
Node<T extends Comparable<T>>
我希望我的实现可以:
获取每种类型的对象并将其放入节点key
中的字段
将每个节点与 key
字段进行比较
这是代码:
public class Node < T extends Comparable < T >> {
private T key;
private Node left;
private Node right;
private Node p;
public void setKey(T key) {
this.key = key;
}
public T getKey() {
return key;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
public void setP(Node p) {
this.p = p;
}
public boolean getBolCompMin(T key) {
return this.key.compareTo(key) < 0;
}
}
我的节点 class 应该扩展 Comparable
以便比较密钥。
这是我的树:
public class BST_Tree < T > {
private ArrayList < Node > nodes;
private Node root;
public BST_Tree(Node root) {
this.root = root;
}
public void insertNode(T key) {
Node z = new Node();
z.setKey(key);
Node x = this.root;
Node y = new Node();
while (x != null) {
y = x;
if (z.getBolCompMin(x.getKey())) {
x = x.getLeft();
} else {
x = x.getRight();
}
}
z.setP(y);
if (z.getBolCompMin(y.getKey())) {
y.setLeft(z);
} else {
y.setRight(z);
}
}
public void InOderWalk(Node x) {
if (x != null) {
InOderWalk(x.getLeft());
System.out.println(x.getKey());
InOderWalk(x.getRight());
}
}
public Node getRoot() {
return root;
}
}
我的树试图在节点 z 中设置密钥,但失败了。这是错误:
incompatible types: T cannot be converted to java.lang.Comparable
提前致谢!
你的
public class BST_Tree<T>
应该是
public class BST_Tree<T extends Comparable<T>>
并且 BST_Tree
和 Node
class 中的每个 Node
变量应该是 Node<T>
.
这将确保您只能使用实现 Comparable
.
的元素类型实例化 BST_Tree
class
我正在尝试使用泛型在 java 中实现二叉树,我进行了搜索,发现了这个问题:Implementing Binary Tree in Java with Generic Comparable<T> data?,但我无法解决我的疑虑。所以我有两个 classes,
BST_Tree<T>
和
Node<T extends Comparable<T>>
我希望我的实现可以:
获取每种类型的对象并将其放入节点
key
中的字段将每个节点与
key
字段进行比较
这是代码:
public class Node < T extends Comparable < T >> {
private T key;
private Node left;
private Node right;
private Node p;
public void setKey(T key) {
this.key = key;
}
public T getKey() {
return key;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
public void setP(Node p) {
this.p = p;
}
public boolean getBolCompMin(T key) {
return this.key.compareTo(key) < 0;
}
}
我的节点 class 应该扩展 Comparable
以便比较密钥。
这是我的树:
public class BST_Tree < T > {
private ArrayList < Node > nodes;
private Node root;
public BST_Tree(Node root) {
this.root = root;
}
public void insertNode(T key) {
Node z = new Node();
z.setKey(key);
Node x = this.root;
Node y = new Node();
while (x != null) {
y = x;
if (z.getBolCompMin(x.getKey())) {
x = x.getLeft();
} else {
x = x.getRight();
}
}
z.setP(y);
if (z.getBolCompMin(y.getKey())) {
y.setLeft(z);
} else {
y.setRight(z);
}
}
public void InOderWalk(Node x) {
if (x != null) {
InOderWalk(x.getLeft());
System.out.println(x.getKey());
InOderWalk(x.getRight());
}
}
public Node getRoot() {
return root;
}
}
我的树试图在节点 z 中设置密钥,但失败了。这是错误:
incompatible types: T cannot be converted to java.lang.Comparable
提前致谢!
你的
public class BST_Tree<T>
应该是
public class BST_Tree<T extends Comparable<T>>
并且 BST_Tree
和 Node
class 中的每个 Node
变量应该是 Node<T>
.
这将确保您只能使用实现 Comparable
.
BST_Tree
class