我正在为普通的二叉树(不是搜索)创建“查找”和“删除”功能。下面是查找函数的代码:
bool Find_Node(FileItem * item, Node *current ) //function starts with root
{
if (current->getNameItem() == item->getName() ) //getNameItem() retrieves the data name in node.
{
currentItem = current; //I have set currentItem as an attribute ( pointer to a Node ) in the tree class. I created it to point to the node I want to find.
return true;
}
Node* child [2];
child[0] = current->getLeft();
child[1] = current->getRight();
bool res = false;
for (int i = 0; res == false && i < 2 ; i++)
{
if(child[i] != NULL)
res = Find_Node(item, child[i]);
}
return res;
}
有没有更好的方法来查找节点?可能有人请帮助我删除功能。