在下面的程序,树节点数据结构的解释
这是什么意思left.parent = this;
我知道它说让左子的父母是“this
”,但究竟是什么this
。这是指当前的实例方法吗?但任何人都可以更好地解释一下。
public class TreeNode {
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode root;
private int size = 0;
public TreeNode(int d) {
data = d;
size = 1;
}
public void setLeftChild(TreeNode left) {
this.left = left;
if (left != null) {
left.root= this;
}
}
会同样的功能setLeftChild
上述表示像下面的功能:
void setLeftChild(TreeNode node)
{
if(root == null)
{
this.root = node;
}
else
{
this.left = node;
}
}
- 是实施正确的?第一和第二?
- 如果不是那么第二次执行有什么问题?反之亦然
- 第一次和第二次实施有什么区别?
no,'node'不一定与'this'相同,它是正在设置的'left'的根。 –