visual-c++ - 树数据结构:我无法获得任何数据输出
问题描述
每当我运行代码并输入文件时,我似乎都没有得到任何输出。
我试图让程序做的是从文件中获取 int 输入(忽略任何其他形式的数据,并且只接受数字 0-99);然后使用第一个数字作为树结构的顶部,对数据进行排序,较小的整数向左,较大的整数向右。
输出将被格式化,它按从低到高的顺序打印数字,并打印它们所在的树的级别(顶部为 0,然后低于 1 的级别为 1,低于 1 的为 2,依此类推) .
我不知道我做错了什么。我尝试在 printData 和 readAndInsert 函数中运行几个循环,但它并没有给我任何关于出了什么问题的想法。
// include libraries needed
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
// create the tree node
struct node
{
int number;
struct node *Left;
struct node *Right;
};
// declare functions that will be used and defined later
void printData(struct node *);
void deleteData(struct node *);
struct node *newNode(int);
struct node *insert(struct node *&, int);
string takeFileName();
int readAndInsert(string, struct node *&);
int main()
{
// create a node to be used in the program
struct node *Root;
Root = new node;
// make sure the node was created
if (Root == NULL)
{
cout << endl << "Fatal error, coul dnot create newRoot node. " << endl;
system("pause");
}
// set Root equal to NULL for the insert function
Root = NULL;
// call the apropriate functions
readAndInsert(takeFileName(), Root);
printData(Root);
deleteData(Root);
// pause the console to see any error output
system("pause");
// successful run
return 0;
}
// function to print the data to a file
void printData(struct node *root)
{
// create an out stream and text file
ofstream out;
out.open("tree.txt");
// Check to see that the file opened and output error message.
if (!out.is_open())
{
cout << "There was a problem opening the file. " << endl;
cout << "Please try again. " << endl;
}
// optional edit to print
out << "Data" << "\t" << "Level" << endl;
// create a node to be used in the program
struct node *Root;
Root = new node;
// make sure the node was created
if (Root == NULL)
{
cout << endl << "Fatal error, coul dnot create newRoot node. " << endl;
system("pause");
}
// set Root equal to NULL for the insert function
Root = root;
if (Root != NULL)
{
// print and rotate to a different node
printData(root-> Left);
out << root->number << endl;
printData(root-> Right);
}
// close the out stream file
out.close();
}
// function to delete the data
void deleteData(struct node *root)
{
if (root != NULL)
{
deleteData(root->Left);
deleteData(root->Right);
delete(root);
}
}
struct node *newNode(int data)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->number = data;
temp->Left = NULL;
temp->Right = NULL;
return temp;
}
// function to insert data
struct node *insert(struct node *&root, int data)
{
// create a node to be used in the program
struct node *Root;
Root = new node;
// make sure the node was created
if (Root == NULL)
{
cout << endl << "Fatal error, coul dnot create newRoot node. " << endl;
system("pause");
}
// set Root equal to NULL for the insert function
Root = root;
if (Root == NULL)
return newNode(data);
if (data < Root->number)
Root->Left = insert(Root->Left, data);
else if (data > Root->number)
Root->Right = insert(Root->Right, data);
return Root;
}
// This function will take input from the user to be used as the fileName then return the name so
// it can be used in other functions.
string takeFileName()
{
// Create a fileName variable that can hole the file name to be entered.
string fileName;
// Output instructions to the user.
cout << "Please enter a file name that you would like to read from (ex. InFile.txt): " << endl;
// Take input for the fileName
cin >> fileName;
// Return the fileName so that it can pass to another function
return fileName;
}
// read from the file and insert the numbers into the tree
int readAndInsert(string fileName, struct node *&root)
{
// Start reading in from whatever file name was input.
ifstream readFile;
readFile.open(fileName);
// Check to see that the file opened and output error message.
if (!readFile.is_open())
{
cout << "The fileName was not correct or there was a problem opening the file. " << endl;
cout << "Please try again. " << endl;
return (1); // unsuccessful run
}
// Run a loop to end of file and put the numbers into the tree
while (true)
{
// Create a variable x to hold the current value read from the file.
int x;
// Take a number from the file.
readFile >> x;
// Set if statement to ignore any bad non-numeric data.
if (readFile.fail())
{
// Clear the bad entry, and then ignore it so as to avoid an infinite loop of bad data.
readFile.clear();
readFile.ignore(INT_MAX, '\n');
// Take in the next line of the file.
readFile >> x;
}
// Break the loop at the end of the file.
if (readFile.eof())
{
break;
}
// create a node to be used in the program
struct node *Root;
Root = new node;
// make sure the node was created
if (Root == NULL)
{
cout << endl << "Fatal error, coul dnot create newRoot node. " << endl;
system("pause");
}
// set Root equal to NULL for the insert function
Root = root;
// Set if statement to sort only numbers that are 0-99.
if (x >= 0 && x <= 99)
{
if (Root == NULL)
{
Root = insert(Root, x);
}
else if (Root != NULL)
{
// insert and sort them into the tree
insert(Root, x);
}
}
}
// Close the readFile;
readFile.close();
return (0); // successful run
}
使用上面的代码,我希望按照从小到大(或从小到大)的顺序将输入到树中的数字输出,但是在输入文件时没有任何输出。
解决方案
推荐阅读
- docker - Kafka Spark Streaming:代理可能不可用 [Docker]
- c# - 如何将 slug 添加到 asp.net 核心网站中的所有链接生成?
- ansible - 使用 ansible Ad-Hoc 命令在 /home/ubuntu/ 位置的“主机”机器文件夹 1 中创建一个文件夹
- regex - python:去除各种引号
- python - Python正则表达式模式匹配数字和单词问题?
- java - 如何在 Java 编程(CodeHS)中找到金字塔的表面积?
- javascript - Discord bot 的无垃圾邮件功能不起作用
- javascript - 使用 jquery ajax 需要来自 API 的响应才能进入表格 HTML
- jupyter - 向 Jupyter Hub/Lab 实例添加横幅消息?
- php - mysqli _connect 类的对象无法在第 135 行转换为字符串