提问人:Tiến Dũng Đỗ 提问时间:11/6/2023 最后编辑:Tiến Dũng Đỗ 更新时间:11/6/2023 访问量:22
类 Node<E#2 中的 compareTo 方法>不能应用于给定类型
method compareTo in class Node<E#2> cannot be applied to given types
问:
我自己构建了一棵二叉树,但发生了这个错误: LinkedBinaryTree.java:111:错误:类 Node<E#2 中的方法 compareTo> 无法应用于给定类型; if(leftNode.compareTo(currentNode) == 0){ ^ 必需:无参数 找到:Node<E#1> 原因:实际参数列表和正式参数列表的长度不同 其中 E#1,E#2 是类型变量: E#1 扩展了在类 LinkedBinaryTree 中声明的对象 E#2 扩展了在类 Node 中声明的对象
这是我的代码:
@SuppressWarnings("unchecked")
public class LinkedBinaryTree<E,T> implements BinaryTreeInterface<T> {
protected static class Node<E>{
private E element; // an element stored at this node
private Node <E> parent ; // a reference to the parent node (if any)
private Node <E> left ; // a reference to the left child
private Node <E> right ; // a reference to the right child
// Constructs a node with the given element and neighbors .
public Node ( E e , Node <E > above , Node <E > leftChild , Node <E > rightChild ) {
// To do
element = e;
parent = above;
left = leftChild;
right = rightChild;
}
}
Node<E> root = null;
int n = 0;
// update methods
public Node <E> addRoot ( E element ) {
// Add element to root of an empty tree
if(!isEmpty()){
return null;
}
root.element = element;
root.parent = null;
root.left = null;
root.right = null;
n++;
return root;
}
public Node <E> addLeft ( Node p , E element ) {
// Add element to left child node of p if empty
if(p.left.element != null){
return p.left;
}
Node<E> new_node = new Node<>(element, p, null, null);
p.left = new_node;
n++;
return new_node;
}
public Node <E > addRight ( Node p , E element ) {
// Add element to right child node of p if empty
if(p.right.element != null){
return p.right;
}
Node<E> new_node = new Node<>(element, p, null, null);
p.right = new_node;
n++;
return new_node;
}
public void set ( Node p , E element ) {
// set element to node p
p.element = element;
}
public T root(){
return (T) root;
}
public int size(){
return n;
}
public boolean isEmpty(){
return root == null;
}
public int numChildren (T p){
int count = 0;
Node<E> currentNode = (Node<E>) p;
if(currentNode.left != null){
count++;
}
if(currentNode.right != null){
count++;
}
return count;
}
public T parent(T p){
Node<E> currentNode = (Node<E>) p;
return (T) currentNode.parent;
}
public T left(T p){
Node<E> currentNode = (Node<E>) p;
return (T) currentNode.left;
}
public T right(T p){
Node<E> currentNode = (Node<E>) p;
return (T) currentNode.right;
}
public T sibling(T p){
Node<E> currentNode = (Node<E>) p;
Node<E> parentNode = currentNode.parent;
Node<E> leftNode = (Node<E>) parentNode.left;
if(leftNode.compareTo(currentNode) == 0){
return (T) parentNode.right;
}
return (T) parentNode.left;
}
}
public interface BinaryTreeInterface<T> {
T root();
int size(); // number of node in tree
boolean isEmpty();
int numChildren (T p) ; // nmber of children of element p;
T parent(T p) ; // return parent of p
T left(T p) ; // return left child of p
T right(T p) ; // return right child of p
T sibling(T p) ; // return sibling of p
}
您能否为我详细描述错误以及如何解决此错误
答: 暂无答案
上一个:获取二叉树的点括号表示
评论
compareTo()
Node
public int compareTo(){ }
T
implements BinaryTreeInterface<LinkedBinaryTree.Node<E>>
Node
public
T
implements BinaryTreeInterface<E>