我正在编写一个程序,读取.CSV文件并将其加载到二进制搜索树中以对其内容进行排序,因为我设法加载了.CSV并实现了该树,但是文件的内容为:
1,彼得,230151515
5,梅里克,25551561
7,Lucky,4301616199
2,luis,2589191919
16,阿尔弗雷多,25891919
8,琳达,129616919
我将每行的第一个数据用作键,并随我携带的代码正确排序,但是我希望它显示行的其余部分,而不仅仅是键,有人可以告诉我如何我可以这样做,以显示每个已排序键的所有数据。
输出:
1
2
5
7
8
16
我要打印的内容如下:
1彼得230151515
2路易斯2589191919
5梅里克25551561
7 Lucky 4301616199
8琳达129616919
16 Alfredo 25891919
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node {
int key;
string name;
int num;
struct node *left, *right;
};
vector<node> persons;
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
//cout<<"\t";
inorder(root->left);
printf("\t%d\n", root->key);
inorder(root->right);
}
}
struct node* insert(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(key);
/* Otherwise, recur down the tree */
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
/* return the (unchanged) node pointer */
return node;
}
struct node * minValueNode(struct node* node)
{
struct node* current = node;
/* loop down to find the leftmost leaf */
while (current && current->left != NULL)
current = current->left;
return current;
}
int main()
{
struct node *root = NULL;
ifstream fin("data.txt");
if (!fin)
{
cout << "File not open\n";
return 1;
}
string line;
const char delim = ',';
while (getline(fin, line))
{
istringstream ss(line);
node person;
ss >> person.key; ss.ignore(10, delim);
getline(ss, person.name,delim);
ss >> person.num;
if (ss)
persons.push_back(person);
}
for (unsigned int i=0; i< persons.size(); i++)
{
cout << setw(5) << persons[i].key<< setw(20) << persons[i].name<< setw(15) << persons[i].num << '\n';
root = insert(root, persons[i].key);
insert(root, persons[i].key);
}
cout << "\n\nInorder:\n";
// cout<<node.name;
inorder(root);
/*/cout<<"\nDelete 50\n";
root = deleteNode(root, 50);
cout<<"Inorder traversal of the modified tree \n";
inorder(root);/*/
/*
insert(root, 80);*/
return 0;
}
答案 0 :(得分:1)
打印出key
时,还可以打印出每个node
的其他信息。这是使用cout
的解决方案:
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
std::cout << root->key << " " << root->name << " " << root->num << "\n";
inorder(root->right);
}
}
答案 1 :(得分:0)
存在一些主要问题和其他一些问题。主要问题是您没有存储和打印所有数据。我做了以下事情:
using namespace std;
node
重命名为Node
Person
的结构旁边添加Node
的结构Person
的构造函数添加到Node
insert
为一种方法#include <iostream>
#include <iomanip>
#include <fstream>
#include <memory>
#include <string>
#include <sstream>
#include <vector>
struct Person {
int key;
std::string name;
int num;
};
struct Node : Person {
Node(const Person &person) : Person(person) {}
std::unique_ptr<Node> left, right;
void insert(const Person &person);
};
void Node::insert(const Person &person) {
/* recur down the tree */
if (key > person.key) {
if (left)
left->insert(person);
else
left = std::make_unique<Node>(person);
} else if (key < person.key) {
if (right)
right->insert(person);
else
right = std::make_unique<Node>(person);
}
}
std::vector<Person> persons;
void inorder(Node *root) {
if (root) {
// cout<<"\t";
inorder(root->left.get());
std::cout << '\t' << root->key << ' ' << root->name << ' ' << root->num << '\n';
inorder(root->right.get());
}
}
Node *minValueNode(Node *node) {
Node *current = node;
/* loop down to find the leftmost leaf */
while (current && current->left) current = current->left.get();
return current;
}
int main() {
std::unique_ptr<Node> root;
std::ifstream fin("data.txt");
if (!fin) {
std::cout << "File not open\n";
return 1;
}
std::string line;
const char delim = ',';
while (std::getline(fin, line)) {
std::istringstream ss(line);
Person person;
ss >> person.key;
ss.ignore(10, delim);
std::getline(ss, person.name, delim);
ss >> person.num;
if (ss) persons.push_back(person);
}
for (unsigned int i = 0; i < persons.size(); i++) {
std::cout << std::setw(5) << persons[i].key << std::setw(20)
<< persons[i].name << std::setw(15) << persons[i].num << '\n';
if (!root) root = std::make_unique<Node>(persons[i]);
else root->insert(persons[i]);
}
std::cout << "\n\nInorder:\n";
// cout<<node.name;
inorder(root.get());
/*/cout<<"\nDelete 50\n";
root = deleteNode(root, 50);
cout<<"Inorder traversal of the modified tree \n";
inorder(root);/*/
/*
insert(root, 80);*/
return 0;
}