-1

how can I read a txt file and insert the values that are stored in the txt file into my binary tree. lets say the txt file has 1,2,3,4,5 and these values should now be inserted into my binary tree. Duplicate key values should be discarded when trying to insert them.

Hello guys, how can I read a txt file and insert the values that are stored in the txt file into my binary tree. lets say the txt file has 1,2,3,4,5 and these values should now be inserted into my binary tree. Duplicate key values should be discarded when trying to insert them.

// Simple program to create a BST of integers and search an element in it 
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
//Definition of Node for Binary search tree
struct node {
    int data;
    node* left;
    node* right;
};



Awox
  • 1
  • 3
  • 4
    Before posting their first question on stackoverflow.com, everyone should take the [tour], read the [help], understand all the requirements for a [mre] and [ask] questions here. Not doing any of this results in a poor quality question almost every time. It then gets downvoted, closed, and then deleted. – Sam Varshavchik Apr 24 '22 at 12:40
  • @SamVarshavchik what?? – Awox Apr 24 '22 at 12:48
  • 2
    I count over four hundred lines of code. Writing "how do I do a ", followed by a large pile of code, is not a useful question for Stackoverflow. The answer to "how do I do a " would be: same as anything else in C++: open a text editor, write C++ code, compile, run, see if it works. That's the literal answer to the only question that's asked here, and very few questions on Stackoverflow require >400 lines of code. A [mre] should be no more than one or two pages of code, and a precise, ***specific***, concise question. – Sam Varshavchik Apr 24 '22 at 13:08
  • it is specifics actually you dont even need the 400 lines of code i just want to insert a txt file so you just maybe need the main, the class, insert and the way how to do it – Awox Apr 24 '22 at 13:14
  • 2
    click on the links provided on the comments above to learn on how to ask your question. – Onyambu Apr 24 '22 at 13:15
  • Is there a reason you're writing your own tree implementation, given the existence of `std::map` and suchlike? – Paul Sanders Apr 24 '22 at 13:17
  • Like I said: "how to do it" is "same as any other C++ program". Are you asking for someone to write this code for you? Sorry, but we don't write programs for other people on Stackoverflow, we don't do their homework assignments, or debug their programs, we only answer ***specific*** technical questions on programming topics. – Sam Varshavchik Apr 24 '22 at 13:20
  • @PaulSanders have not heard of that yet – Awox Apr 24 '22 at 13:21
  • @SamVarshavchik it is not really possible to help someone without writing any code – Awox Apr 24 '22 at 13:22
  • Yes, it is. But someone needs to ask a ***specific*** question they want some help with. "how can I read a txt file and insert the values that are stored in the txt file into my binary tree" is not a question for which anyone can provide any "help" for. The only answer to that, as I already said: "open a text editor, write C++ code, compile it, and see if it works correctly", and that's how you do it. If you can articulate a single, ***specific*** technical question about programming, then someone might be able to answer it in a useful way. – Sam Varshavchik Apr 24 '22 at 13:25
  • @Awox Then you should take a look. Sorry to have to tell you this, but none of what you have written is actually needed. Time to take a look at one of [these](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) perhaps. – Paul Sanders Apr 24 '22 at 13:36
  • @SamVarshavchik if it was that easy – Awox Apr 24 '22 at 18:02
  • 2
    Well, if it's not easy to articulate a single, specifical technical question here, that I don't know what to tell you. Stackoverflow is not a C++ tutorial site, we don't provide customized, one-on-one tutorials and courses, or write other people's programs for them. We only answer specific questions on programming. – Sam Varshavchik Apr 24 '22 at 18:24

1 Answers1

0

To get the values from the text file, which you specified as CSV (Comma Separated Values), you would need to do the following steps.

  1. Open the text file by using a std::ifstream and its corresponding constructor
  2. Check, if the file could be opend (The bool operator of the std::ifstreamis overwritten for this purpose
  3. In a while loop, read the values and the comma, using normal C++ formatted input with the >> operator. This will skip white spaces and will stop, if EOF (End of file) is hit, or, if no conversion is possible any longer (e.g. the last missing comma)
  4. After you have this value, add it to your binary tree (in whatever way it is implemented)
  5. The destructor of your instance of the std::ifstream will automatically close the file for you.

A very simple example code for the above described approach could be:

#include <iostream>
#include <fstream>
#include <set>

// I will not implement the binary tree here and use a std::multiset for test purposes
// You will add your won functionality here
using BinaryTree = std::multiset<int>;

const std::string fileName{ "r:\\test.txt" };

int main() {

    // Define a in-file-stream variable and open the file using the constructor of the std::ifstream
    std::ifstream ifs(fileName);

    // Check, if the file could be opened successfully
    if (ifs) {

        BinaryTree bt{};

        // Temporaries
        int value{};     // Here we will get the value that shall be stored in the tree
        char comma{};    // This is a dummy for the comma and will be ignored

        // Now, in a loop, read all values from a file
        // Reading will stop on EOF or if there is no more comma
        while (ifs >> value) {

            // Add value to binary tree
            bt.insert(value);

            // Debug output
            std::cout << value << '\n';

            // Consume the comma. If none is present, then ifs is not good any longer.
            ifs >> comma;
        }
    }
    else std::cerr << "\n\n*** Error: Could not open file '" << fileName << "' for reading\n\n";
}

A M
  • 14,694
  • 5
  • 19
  • 44