【发布时间】:2016-01-09 23:19:49
【问题描述】:
我是 C++ 新手,我通常使用 Java,所以我很难进入指针和引用。我必须使用内部节点和叶节点(只有叶包含数据)来做一个二叉搜索树的变体。
class Node
Node *parent;
Node *left;
Node *right;
//other stuff
}
我需要实现operator<<,它将一个具有值的新节点添加到树中。
//adding a node to tree
void operator<<(int value){
if(size == 0){
//stuff
} else {
Node* temp = root;
getLeaf(temp,value);
//other magic
//temp will be used to append a new node into tree,
//so it has to point to the actual node in the tree
delete temp;
}
}
getLeaf 函数的要点是找到一个叶子(可能包含也可能不包含所需的value)并将其存储到temp,它需要在operator<< 函数中访问。
int getLeaf( Node* temp, int value) const{
int depth = 0;
//goes trough all inner nodes until it finds specific leaf
while(temp->isInner()){
++depth;
if(value < temp->getValue()){ //searched value is smaller
temp = temp->getLeft(); // to left subtree
continue;
} else {
temp = temp->getRight(); //to rightsubtree
continue;
}
return depth;
}
我真的很困惑如何做到这一点以及指针和值的正确组合是什么。如果我设置
Node* temp = root;
getLeaf(temp,value);
在getLeaf 函数中遍历树时,root 不会被覆盖吗?
另外我需要temp 指向树中的实际节点,所以我可以在其中添加一个新节点。
你能解释一下吗?
【问题讨论】:
-
投反对票的原因是什么?
-
这个问题非常针对您的问题,因此很难以对其他人有用的方式回答...但我认为您的
getLeaf应该引用指针(Node*&,这样temp的实际值就可以在原来的调用者代码中更新了。 -
(我没有投反对票,但我明白为什么有人会投反对票 - 你的代码还不够完整,无法测试我们是否可以让它工作)
-
谢谢,我会试试 Node*&。我的代码还没有完成,所以我还不能在这里发布 :) 但是 Node* temp = root; 呢? getLeaf 中给 temp 新分配的值不会影响整棵树吗?
-
你说你懂Java。你可以看到,Java 对所有不是基本类型的东西都使用指针,比如
int或double。如果你在 Java 中有void doSomething(MyClass object)方法,你可以在 C++ 中编写void doSomething(MyClass *object)并使用->而不是.。如果在 C++ 中编写void doSomething(MyClass object),则不能修改函数内部的参数,就像在 Java 中使用基本类型一样。引用是某种类型的语法糖。 Thay 像指针一样工作,但您不能更改目标,也不能取消引用它来访问值。