我为我的演示文稿制作了一个二进制树程序,尝试使用文件输入进行后序遍历,当插入步骤启动时,结果显示树只有1个节点,然后重复直到它读到文件末尾。 任何想法在这里出了什么问题?
这是我在txt中使用的数据
5709611981 N 0623sweet@gmail.com A
5909680109 N 35563@bodin.ac.th A
5909680059 N a.supitcha@hotmail.com A
5909610114 N aladen009@hotmail.com A
5909610031 N aomsin3475@gmail.com A
5909520024 N apisittest@hotmail.com A
5909680018 N apstaan@gmail.com A
5709650567 S apuonn_ap@hotmail.com A
5709650062 S athachai-riders@hotmail.com A
5909610064 N babe-anusorn@hotmail.com A
5909650193 S baifeern@gmail.com A
5709650021 S ball.bus@hotmail.com A
5909610460 N bambee18341@gmail.com A
5909650011 S bell12546789@gmail.com A
5809650798 S big__007@hotmail.com A
代码:
class Person{
private:
string id;
string section;
string status;
string email;
public:
Person();
Person(string id,string section,string email,string status);
string getID();
string getSection();
string getStatus();
string getEmail();
void setID(string newid);
void setEmail(string newEmail);
void setSection(string newsection);
void setStatus(string newstatus);
};
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
Person data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root == NULL;}
void print_postorder();
void postorder(tree_node*);
void print_preorder();
void preorder(tree_node*);
void insert(Person);
void remove(string);
void search(string key);
void changeStatus(string key,string newstatus);
};
Person::Person()
{
}
Person::Person(string newid,string newsection,string newemail,string newstatus){
id = newid;
section = newsection;
email = newemail;
status = newstatus;
}
string Person::getID(){
return id;
}
string Person::getSection(){
return section;
}
string Person::getEmail(){
return email;
}
string Person::getStatus(){
return status;
}
void Person::setStatus(string newstatus){
status = newstatus;
}
void Person::setID(string newid){
status = newid;
}
void Person::setEmail(string newemail){
status = newemail;
}
void Person::setSection(string newsection){
status = newsection;
}
void BinarySearchTree::insert(Person p){
tree_node* t = new tree_node;
tree_node* parent;
t->data = p;
t->left = NULL;
t->right = NULL;
parent = NULL;
if(isEmpty()) root = t;
else{
tree_node* curr;
curr = root;
while(curr)
{
parent = curr;
if(t->data.getID() > curr->data.getID()){
curr=curr->right;
}
else{
curr = curr->left;
}
}
if(t->data.getID() < parent->data.getID()){
parent->left = t;
}
else{
parent->right = t;
}
}
}
void BinarySearchTree::print_postorder(){
postorder(root);
}
void BinarySearchTree::postorder(tree_node* p)
{
if(p != NULL)
{
if(p->left){
postorder(p->left);
}
if(p->right){
postorder(p->right);
}
cout<<" "<<p->data.getID() << " " << endl ;
}
else {
cout<<" NULL P " << endl ;
return;
}
}
void BinarySearchTree::search(string key){
bool found = false;
tree_node* curr;
tree_node* parent;
curr = root;
while(curr != NULL){
if(curr->data.getID() == key){
found = true ;
cout << "Contact Email : " << curr->data.getEmail() << endl;
}
else
{
parent = curr;
if(key>curr->data.getID()){
curr = curr->right;
}
else curr = curr->left;
}
}
if(!found){
cout<<" This student is not in this class. " << endl;
return;
}
}
void fillTree(BinarySearchTree b)
{
ifstream file;
file.open("classlist60.txt");
if(!file){
cout << "File error." << endl;
}
string id;
string section;
string email;
string status;
Person p;
int count = 0;
int halt = 0 ;
while(file >> id >> section >> email >> status)
{
p.setID(id);
p.setSection(section);
p.setEmail(email);
p.setStatus(status);
count++;
if(status == "W"){
halt++;
}
b.insert(p);
}
b.print_postorder();
cout << endl << " Total registered students :" << count << endl;
cout << " Num of withdrawal Students :" << halt << endl;
file.close();
}
int main(){
BinarySearchTree b;
string id;
string email;
fillTree(b);
cout << endl << " Search Email for student ID : " ;
cin >> id;
b.search(id);
return 0;
}
结果应该是Postorder中学生的所有细节,并且可以通过输入结果树搜索几乎是空的
答案 0 :(得分:2)
在insert
功能中,您将父级链接到节点t
,但您不必将节点t
链接到它要替换的节点。所以你最终失去了父母所指向的东西。您需要在父项及其子项之间插入新节点。
这个想法与插入链表相同。在插入之前,您有:
parent
/ \
child child
您的代码正在执行此操作:
parent
/ \
child t
但你想要的是:
parent
/ \
child t
\
child
这是对您的代码的简单修改:
if(t->data.getID() < parent->data.getID()){
t->left = parent->left; // link to parent's previous child
parent->left = t;
}
else{
t->right = parent->right; // link to parent's previous child
parent->right = t;
}
}
}