gpt4 book ai didi

c++ - 二叉树遍历树不完整

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:52:08 24 4
gpt4 key购买 nike

<分区>

我为我的演示文稿制作了一个二叉树程序,试图使用文件输入进行后序遍历,结果是当插入步骤开始时树只有 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;
}

结果是: enter image description here

结果应该是 Postorder 中学生的所有详细信息,可以通过 Input 搜索结果树几乎是空的

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com