[英]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.