简体   繁体   English

在C ++中按字母顺序对二进制搜索树进行排序

[英]alphabetically sorting binary search tree in c++

I'm new to stackoverflow, i just got desperate and am hoping for a miracle. 我是stackoverflow的新手,我很拼命,希望能有个奇迹。

I have to read in from a file a list of people with their info being Last name (up to 20 characters) First name (up to 20 characters) House number (an integer) Street (up to 20 characters) City (up to 20 characters) State abbreviation (up to 2 characters) Zip (a 5 digit code) 我必须从文件中读取一个信息列表,其姓名为姓氏(最多20个字符)名字(最多20个字符)门牌号(整数)街道(最多20个字符)城市(最多20个字符)字符)状态缩写(最多2个字符)邮政编码(5位代码)

I have to have two binary search trees, one sorted by zip code and one sorted by last name. 我必须有两个二进制搜索树,一棵按邮政编码排序,一棵按姓氏排序。 The professor generally gives us a ton of info about the reasoning behind the code but absolutely nothing in terms of implementation. 教授通常会为我们提供大量有关代码背后原因的信息,但在实现方面绝对没有。 I'm pretty lost, especially when it comes to sorting strings alphabetically. 我很迷茫,尤其是在按字母顺序对字符串进行排序时。 I haven't even gotten to the part of the code where I read in from the file yet, but I'm trying to set up my methods first. 我什至还没有从文件中读取代码的那一部分,但是我试图首先设置我的方法。

Any suggestions on how to create an alphabetically ordered c++ binary search tree? 关于如何创建按字母顺序排序的c ++二进制搜索树的任何建议? Do i need two different node structs and insert methods for it to be ordered by zip and last name? 我是否需要两个不同的节点结构和插入方法,以便按zip和姓氏对其进行排序?

here's my code so far 到目前为止,这是我的代码

#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;
}

Any Binary Search Tree when traversed in Inorder results in a sorted array. 当以“顺序”遍历任何二进制搜索树时,将生成一个排序数组。

If you want to sort strings just compare it normally like you do with integers as c++ has all those functions in built. 如果您想对字符串进行排序,只需像对待整数一样正常进行比较,因为c ++内置了所有这些函数。

And after you have successfully inserted the data. 并且在您成功插入数据之后。 Apply Inorder traversal and you will get an alphabetically sorted array. 应用顺序遍历,您将获得按字母顺序排序的数组。 Just include and then you can compare strings like normal data type[for eg. 只需包含即可,然后您就可以比较正常数据类型之类的字符串[例如, str1 > str2 ] str1> str2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM