Java 调用以getter作为参数的函数不会';你不能保留引用吗?
我的代码有问题,我正在创建一个二叉搜索树数据结构,当我使用节点的子节点调用函数,然后在函数中为该子节点赋值时,它不会更新节点的子节点Java 调用以getter作为参数的函数不会';你不能保留引用吗?,java,binary-search-tree,recurrence,Java,Binary Search Tree,Recurrence,我的代码有问题,我正在创建一个二叉搜索树数据结构,当我使用节点的子节点调用函数,然后在函数中为该子节点赋值时,它不会更新节点的子节点 //*** Pseudo-ish Code *** class BSTNode { private BSTNode lChild; private BSTNode rChild; private int key; public BSTNode(int key) { this.lChild = null;
//*** Pseudo-ish Code ***
class BSTNode {
private BSTNode lChild;
private BSTNode rChild;
private int key;
public BSTNode(int key) {
this.lChild = null;
this.rChild = null;
this.key = key;
}
//getters and setters for each field ^
}
class BST {
private BSTNode root;
public BST() {
this.root = null;
}
public void insert(BSTNode currentNode, int value) {
BSTNode newNode = new BSTNode(value);
if (currentNode == null) {
currentNode = newNode;
if (this.root == null) {
this.root = currentNode;
}
} else {
//ignore the newNode == currentNode value statement right now
if (newNode.getValue() < currentNode.getValue()) {
insert(currentNode.getlChild(), value);
} else if (newNode.getValue() > curNode.getValue()) {
insert(curNode.getrChild(), value);
}
}
}
//getters and setters
}
这将输出10
,然后输出NullPointerException。我理解这是因为不知何故7没有被分配为10的lChild,但我不知道为什么?这是我遇到的范围问题,还是因为我在insert函数中使用getlChild()递归调用而无法访问实际的私有lChild字段
注意:我使用sysout调试代码,我注意到递归确实有效,它确实将7正确分配给currentNode,但一旦函数运行完毕,currentNode就不再引用初始根节点的lChild 问题在于:
BSTNode newNode = new BSTNode(value);
每次计算机调用递归方法insert()
,它都会创建一个新的BSTNode()
。每次只需添加一个new BSTNode()
,但它会一次又一次地创建节点。例如,要添加3,为此必须调用insert()
4次。它将创建4节点,而不是仅创建1节点
我所做的,除了删除一些错误之外,我还在BSTNode类
中创建了递归的insertValue()
方法。因此,您不必每次调用此方法时都跟踪currentNode
。同样,每个节点都将调用自己的insertValue()
方法
//*** Pseudo-ish Code ***
class BSTNode
{
public BSTNode lChild;
public BSTNode rChild;
public int key;
public BSTNode(int key)
{
this.lChild = null;
this.rChild = null;
this.key = key;
}
/* Create INSERT function in BSTNode class so that you dont have to give the "CurrentNode" everytime
you call this method, Now you just have to pass the "Key"*/
public void insertValue(int insertValue)
{
if(insertValue < key)
{
if(lChild == null)
lChild = new BSTNode(insertValue);
else
lChild.insertValue(insertValue);
}
else if(insertValue > key)
{
if(rChild == null)
rChild = new BSTNode(insertValue);
else
rChild.insertValue(insertValue);
}
else;
}
}
class BST
{
private BSTNode root;
public BST()
{
this.root = null;
}
// just create the root if not present else it'll call the recursive method of BSTNode class
public void insert(int value)
{
if(root == null)
root = new BSTNode(value);
else
root.insertValue(value);
}
// you didn't provide these methods so i wrote my own just to get your code runing
public BSTNode getRoot()
{
return root;
}
public int getRootValue()
{
return root.key;
}
}
public class BSTMain
{
public static void main(String[] args)
{
BST testBST = new BST();
testBST.insert(10);
testBST.insert(7);
System.out.print(testBST.getRootValue());
System.out.print(" ");
System.out.print(testBST.getRoot().lChild.key);
}
}
//***伪ish代码***
类节点
{
公共儿童;
公共节点rChild;
公钥;
公共节点(int键)
{
this.lChild=null;
this.rChild=null;
this.key=key;
}
/*在BSTNode类中创建插入函数,这样就不必每次都给出“CurrentNode”
调用此方法后,现在只需传递“键”*/
公共void insertValue(int insertValue)
{
如果(插入值<键)
{
if(lChild==null)
lChild=新的BSTNode(insertValue);
其他的
lChild.insertValue(insertValue);
}
else if(插入值>键)
{
如果(rChild==null)
rChild=新节点(insertValue);
其他的
rChild.insertValue(insertValue);
}
其他的
}
}
BST级
{
私有节点根;
公共BST()
{
this.root=null;
}
//只要创建根,否则它将调用BSTNode类的递归方法
公共空白插入(int值)
{
if(root==null)
根=新节点(值);
其他的
root.insertValue(value);
}
//您没有提供这些方法,所以我编写了自己的方法,只是为了让您的代码运行
公共节点getRoot()
{
返回根;
}
public int getRootValue()
{
返回root.key;
}
}
公共类BSTMain
{
公共静态void main(字符串[]args)
{
BST testBST=新的BST();
测试BST.插入(10);
测试BST.插入(7);
System.out.print(testBST.getRootValue());
系统输出打印(“”);
System.out.print(testBST.getRoot().lChild.key);
}
}
注意:我添加了一些方法,比如
getRoot()
只是为了让代码正常工作,因为您没有提供它们。在哪里将currentNode
分配到左侧或右侧?我在你的代码中看不到它。在递归之前,需要类似于if(newNode.getValue()currentNode.getChild().getValue()){/*将当前节点分配给左节点并返回/*}
(以及右节点的等效值)。
//*** Pseudo-ish Code ***
class BSTNode
{
public BSTNode lChild;
public BSTNode rChild;
public int key;
public BSTNode(int key)
{
this.lChild = null;
this.rChild = null;
this.key = key;
}
/* Create INSERT function in BSTNode class so that you dont have to give the "CurrentNode" everytime
you call this method, Now you just have to pass the "Key"*/
public void insertValue(int insertValue)
{
if(insertValue < key)
{
if(lChild == null)
lChild = new BSTNode(insertValue);
else
lChild.insertValue(insertValue);
}
else if(insertValue > key)
{
if(rChild == null)
rChild = new BSTNode(insertValue);
else
rChild.insertValue(insertValue);
}
else;
}
}
class BST
{
private BSTNode root;
public BST()
{
this.root = null;
}
// just create the root if not present else it'll call the recursive method of BSTNode class
public void insert(int value)
{
if(root == null)
root = new BSTNode(value);
else
root.insertValue(value);
}
// you didn't provide these methods so i wrote my own just to get your code runing
public BSTNode getRoot()
{
return root;
}
public int getRootValue()
{
return root.key;
}
}
public class BSTMain
{
public static void main(String[] args)
{
BST testBST = new BST();
testBST.insert(10);
testBST.insert(7);
System.out.print(testBST.getRootValue());
System.out.print(" ");
System.out.print(testBST.getRoot().lChild.key);
}
}