Java 无法从内部类强制转换到另一个类
我有一个包含内部类节点的BinaryTree类。 我想做的是通过调用tree.insert(node)在我的BinaryTree树中插入一些节点。但是,为了保持干净一致,我不想在节点内部类中创建insert()方法。因此,我尝试了下面的代码,但出现了一个错误:无法从BinaryTree.Node强制转换为BinaryTree 我该怎么办 二叉树类Java 无法从内部类强制转换到另一个类,java,class,binary-tree,nodes,inner-classes,Java,Class,Binary Tree,Nodes,Inner Classes,我有一个包含内部类节点的BinaryTree类。 我想做的是通过调用tree.insert(node)在我的BinaryTree树中插入一些节点。但是,为了保持干净一致,我不想在节点内部类中创建insert()方法。因此,我尝试了下面的代码,但出现了一个错误:无法从BinaryTree.Node强制转换为BinaryTree 我该怎么办 二叉树类 public class BinaryTree { Node root = null; private class Node {
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);
}
}
主类
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);
}
}
}
公共类主{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪sc=新的扫描仪(System.in);
字符串[]str=sc.nextLine().split(“”);
二叉树;
对于(int i=0;i
谢谢,要在树中插入节点,您需要定义插入节点的位置,因此插入方法应类似于:
//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;
}
//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);
}
它不需要浇铸要查找
rightTo
节点,您可以使用:
//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);
}
要将节点作为最后一个节点插入,可以使用:
//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;
}
其中,getTail
类似于:
//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;
}
//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);
}
注意:代码没有经过测试,所以需要仔细调试 要在树中插入节点,需要定义插入节点的位置,因此插入方法应类似于:
//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;
}
//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);
}
它不需要浇铸要查找
rightTo
节点,您可以使用:
//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);
}
要将节点作为最后一个节点插入,可以使用:
//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;
}
其中,getTail
类似于:
//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;
}
//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);
}
注意:代码没有经过测试,所以需要仔细调试 您不需要在
insert
方法中进行类型转换。应该是这样的:
public void insert(Node n) {
if(n.value > root.value)
insert(root.right);
}
您不需要在
insert
方法中进行类型转换。应该是这样的:
public void insert(Node n) {
if(n.value > root.value)
insert(root.right);
}
root
属于Node
类型,因此root。right
也属于Node
类型,而不是BinaryTree
类型。因此,正如预期的那样,强制转换不起作用。但是节点
应该扩展二进制树
,正如它在其中声明的那样,对吗?不。您可以使用扩展BinaryTree
来实现这一点,但因为您没有这样做,所以它不是。此外,如果您试图在BinaryTree
类之外使用节点
类,则将该类设为私有将对您产生不利影响。根
属于节点
类型,所以root.right
也是Node
类型,而不是BinaryTree
类型。因此,正如预期的那样,强制转换不起作用。但是节点
应该扩展二进制树
,正如它在其中声明的那样,对吗?不。您可以使用扩展BinaryTree
来实现这一点,但因为您没有这样做,所以它不是。此外,如果您试图在BinaryTree
类之外使用节点,则将其设为私有将对您产生不利影响。