使用 getter 作为参数调用函数不保留引用?
Calling a function with a getter as parameter doesn't retain refrence?
我的代码有问题,我正在创建一个二叉搜索树数据结构,当我调用一个带有节点 child 的函数时,然后为该 child 赋值在函数内,它不会更新节点的 child。
//*** 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
}
我仍然想自己弄清楚代码,但我很好奇为什么我要 运行 这段代码:
BST testBST = new BST();
testBST.insert(testBST.getRoot(), 10);
testBST.insert(testBST.getRoot(), 7);
System.out.print(testBST.getRoot());
System.out.print(" ");
System.out.print(testBST.getRoot().getlChild());
这将输出 10
然后是 NullPointerException。我知道这是因为不知何故 7 没有被分配为 10 的 lChild,但我不知道为什么?这是我遇到的范围问题,还是因为我在插入函数中使用 getlChild() 递归调用而无法访问实际的私有 lChild 字段?
注意:我正在使用 sysout 来调试我的代码,我注意到递归确实有效,并且它确实将 7 正确分配给了 currentNode,但是一旦函数完成 运行ning,它就是like currentNode不再引用初始根节点的lChild。
问题在这里:
BSTNode newNode = new BSTNode(value);
每次计算机调用递归方法 insert()
时,它都会创建一个 new BSTNode()
。您只想每次添加一个 new BSTNode()
但它会一次又一次地创建节点。例如,您想添加 3 并且为此它必须调用 insert()
4 次。它将创建 4 个节点,而不是仅创建 1 个节点。
我所做的,除了消除一些错误外,我还在 BSTNode class
中创建了递归 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);
}
}
注意: 我添加了一些方法,例如 getRoot()
只是为了让您的代码正常工作,因为您没有提供它们。
我的代码有问题,我正在创建一个二叉搜索树数据结构,当我调用一个带有节点 child 的函数时,然后为该 child 赋值在函数内,它不会更新节点的 child。
//*** 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
}
我仍然想自己弄清楚代码,但我很好奇为什么我要 运行 这段代码:
BST testBST = new BST();
testBST.insert(testBST.getRoot(), 10);
testBST.insert(testBST.getRoot(), 7);
System.out.print(testBST.getRoot());
System.out.print(" ");
System.out.print(testBST.getRoot().getlChild());
这将输出 10
然后是 NullPointerException。我知道这是因为不知何故 7 没有被分配为 10 的 lChild,但我不知道为什么?这是我遇到的范围问题,还是因为我在插入函数中使用 getlChild() 递归调用而无法访问实际的私有 lChild 字段?
注意:我正在使用 sysout 来调试我的代码,我注意到递归确实有效,并且它确实将 7 正确分配给了 currentNode,但是一旦函数完成 运行ning,它就是like currentNode不再引用初始根节点的lChild。
问题在这里:
BSTNode newNode = new BSTNode(value);
每次计算机调用递归方法 insert()
时,它都会创建一个 new BSTNode()
。您只想每次添加一个 new BSTNode()
但它会一次又一次地创建节点。例如,您想添加 3 并且为此它必须调用 insert()
4 次。它将创建 4 个节点,而不是仅创建 1 个节点。
我所做的,除了消除一些错误外,我还在 BSTNode class
中创建了递归 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);
}
}
注意: 我添加了一些方法,例如 getRoot()
只是为了让您的代码正常工作,因为您没有提供它们。