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

标签 c++ sorting file-io binary-search-tree alphabetical

我是 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/

相关文章:

c++ - 初始化一个本身合法的引用成员?

c++ - 如何解决MFC uArt中未定义的智能感知标识符?

c++ - move 语义不完整吗?

c - C中使用指针数组对字符串进行排序

c - 如何使用通配符删除 C 中的多个文件?

c++ - 如何在 C++ 中将 keydown 事件发送到非事件窗口?

c++ - C++以相同方式排列两个 vector

python - 如何根据另一个列表中的阈值对一个列表中的值进行分类?

ruby-on-rails - 如何在 Ruby 中从下到上读取文件?

java - 如何检查两条路径是否在同一个挂载点上?