也許之前要求百萬次,但我根本不明白這是什麼問題。我不想在互聯網上使用任何代碼,所以我只是試圖對我的想法進行編程。這個或我的打印功能都是錯誤的。下面的代碼有什麼問題嗎?二進制搜索樹插入C++
void addNode(int value)
{
Node* newNode=new Node;
newNode->data=value;
if(root==NULL)
root=newNode;
else {
Node* temp=root,*parent;
while(temp!=NULL)
{
parent=temp;
if(temp->data == value)
return;
else if(temp->data < value)
temp=temp->left;
else
temp=temp->right;
}
temp=newNode;
}
}
您從不指定任何節點的「left」或「right」成員。 – 2012-04-18 18:21:45
我正在使用'temp = temp-> left'和'temp = temp-> right'。這不算什麼? – Ali 2012-04-18 18:22:36
@rolandbishop:不;這會將您的本地變量更改爲引用不同的節點,但是一旦找到插入點,您就不會修改樹。 – 2012-04-18 18:25:52