template <class T>
struct TreeNode{
string value;
T key;
TreeNode<T> *Parent;
TreeNode<T> *LeftChild;
TreeNode<T> *RightChild;
TreeNode (T k,string Val)
{
this->value=Val;
this->key=k;
this->Parent=NULL;
this->LeftChild=NULL;
this->RightChild=NULL;
}
};
template <class T>
class BinaryTree{
private:
TreeNode<T> *Root;
public:
BinaryTree();
~BinaryTree();
void insertNode(T Key,string Val);
void deleteNode(T Key);
string searchNode(T Key);
void UpdateKey(T newkey,T oldkey);
int Height(TreeNode<T> *node);
int height();
};
template <class T>
string BinaryTree<T>::searchNode(T Key)
{
TreeNode<T> *temp=Root;
while (temp!=NULL)
{
if (temp->key==Key)
{
cout<<temp->key<<endl;
return temp->value;
}
if (temp->key>Key)
{
temp=temp->LeftChild;
}
else if (temp->key<Key)
{
temp=temp->RightChild;
}
}
return "\0";
}
이진 검색 트리를 만들고 있습니다. 그러나 검색 기능을 실행할 때 값이 트리에 존재하더라도 항상 NULL 값을 반환합니다. 내 생성자가 올바르지 않거나 검색 기능에 문제가 있습니다. 나는 그 문제를 알아낼 수 없다. 여기 생성자입니다 : 모든이진 검색 트리 (검색 기능)
template <class T>
BinaryTree<T>::BinaryTree()
{
Root=NULL;
ifstream fin;
fin.open("names.txt");
string buffer;
T buff;
while (!fin.eof())
{
getline(fin,buffer,'~');
fin>>buff;
TreeNode<T> *temp,*temp1;
temp=Root;
temp1=temp;
while (temp!=NULL)
{
temp1=temp;
TreeNode<T> *Right=temp->RightChild;
TreeNode<T> *Left=temp->LeftChild;
if (temp->key>buff)
{
temp=temp->LeftChild;
}
else if (temp->key<buff)
{
temp=temp->RightChild;
}
else
{
temp=temp->LeftChild;
}
}
if (temp!=Root)
temp->Parent=temp1;
temp=new TreeNode<T>(buff,buffer);
}
fin.close();
}
트리를 사용할 때 인덱스의 유형은 무엇입니까? – speeder
int 유형입니다. – User14229754