[英]alphabetically sorting binary search tree in c++
我是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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.