gpt4 book ai didi

c++ - 在 C++ 中按字母顺序排序二叉搜索树

转载 作者:行者123 更新时间:2023-11-28 06:00:51 25 4
gpt4 key购买 nike

我是 stackoverflow 的新手,我很绝望,希望能有奇迹出现。

我必须从一个文件中读入一个人的列表,他们的信息是姓氏(最多 20 个字符)名字(最多 20 个字符)门牌号(整数)街道(最多 20 个字符)城市(最多 20 个字符)州缩写(最多 2 个字符)邮政编码(5 位数代码)

我必须有两棵二叉搜索树,一棵按邮政编码排序,另一棵按姓氏排序。教授通常会为我们提供大量有关代码背后推理的信息,但在实现方面绝对没有。我很迷茫,尤其是在按字母顺序对字符串进行排序时。我什至还没有读到我从文件中读入的代码部分,但我正在尝试先设置我的方法。

关于如何创建按字母顺序排列的 C++ 二叉搜索树的任何建议?我是否需要两个不同的节点结构和插入方法才能按 zip 和姓氏对其进行排序?

到目前为止,这是我的代码

#include<iostream>
#include<iterator>
#include<fstream>
#include<cstdlib>

using namespace std;

class inputInfo
{
private:
string tempLast;
string tempFirst;
int tempHouse;
string tempStreet;
string tempCity;
string tempState;
int tempZip;

public:
void setLast(string last);
void setFirst(string first);
void setHouse(int house);
void setStreet(string street);
void setCity(string city);
void setState(string sate);
void setZip(int zip);

string getLast();
string getFirst();
int getHouse();
string getStreet();
string getCity();
string getState();
int getZip();
}
void inputInfo::setLast(string last)
{tempLast=last;}
void inputInfo::setFirst(string first)
{tempFirst=first;}
void inputInfo::setHouse(int house)
{tempHouse=house;}
void inputInfo::setStreet(string street)
{tempStreet=street;}
void inputInfo::setCity(string city)
{tempCity=city;}
void inputInfo::setState(string state)
{tempState=state;}
void inputInfo::setZip(int zip)
{tempZip=zip;}

string inputInfo::getLast()
{return tempLast;}
string inputInfo::getFirst()
{return tempFirst;}
int inputInfo::getHouse()
{return tempHouse;}
string inputInfo::getStreet()
{return tempStreet;}
string inputInfo::getCity()
{return tempCity;}
string inputInfo::getState()
{return tempState;}
int inputInfo::getZip()
{return tempZip;}

//Node structure for binary tree organized by zip
struct zipNode{
inputInfo data;
zipNode* left;
zipNode* right;
}

//Function to creat a new node
zipNode* GetNewNode(inputInfo data){
zipNode* newNode = new zipNode();
newNode->data = data;
newNode->left=newNode->right=NULL;
return newNode;
}
//insert data in BST, returns address of root node
zipNode* InsertZip(zipNode* root, inputInfo data){
if(root == NULL)
{
root= GetNewNode(data);
}

else if(data.getZip() <= root->data.getZip())
{
root->left=Insert(root->left,data);
}
else
{
root->right=Insert(root->right,data);
}
return root;
}

struct nameNode{
inputInfo data;
nameNode* left;
nameNode* right;
}

//Function to creat a new node
nameNode* GetNewNode(inputInfo data){
nameNode* newNode = new nameNode();
nameNode->data = data;
nameNode->left=newNode->right=NULL;
return newNode;
}
//insert data in BST, returns address of root node
nameNode* InsertName(nameNode* root, inputInfo data){
if(root == NULL)
{
root= GetNewNode(data);
}

else if(data.getLast() <= root->data.getLast())
{
root->left=Insert(root->left,data);
}
else
{
root->right=Insert(root->right,data);
}
return root;
}

最佳答案

按中序遍历时,任何二叉搜索树都会生成排序数组。

如果你想对字符串进行排序,只需像比较整数一样正常比较它,因为 C++ 内置了所有这些函数。

在你成功插入数据之后。应用中序遍历,您将得到一个按字母顺序排序的数组。只需包括然后你可以像普通数据类型一样比较字符串[例如。 str1 > str2 ]

关于c++ - 在 C++ 中按字母顺序排序二叉搜索树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33321772/

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