Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need help on the last part. This code should, read a file, 1st number is the number of int in the file, order the int in ascending order and then write it in another file.

#include <iostream>
#include <fstream>

using namespace std;


struct node{
    int value;
    node *left;
    node *right;
};

class btree{
public:
    void insert(int key);
    node *search(int key);
    void inorder_print();
    void insert(int key, node *leaf);
    node *search(int key, node *leaf);
    void inorder_print(node *leaf);
    node *root;
};

void btree::insert(int key, node *leaf){

    if(key < leaf->value){
        if(leaf->left != NULL){
            insert(key, leaf->left);
        }else{
            leaf->left = new node;
            leaf->left->value = key;
            leaf->left->left = NULL;
            leaf->left->right = NULL;
        }
    }else if(key >= leaf->value){
        if(leaf->right != NULL){
            insert(key, leaf->right);
        }else{
            leaf->right = new node;
            leaf->right->value = key;
            leaf->right->right = NULL;
            leaf->right->left = NULL;
        }
    }

}

void btree::insert(int key){
    if(root != NULL){
        insert(key, root);
    }else{
        root = new node;
        root->value = key;
        root->left = NULL;
        root->right = NULL;
    }
}

node *btree::search(int key, node *leaf){
    if(leaf != NULL){
        if(key == leaf->value){
            return leaf;
        }
        if(key < leaf->value){
            return search(key, leaf->left);
        }else{
            return search(key, leaf->right);
        }
    }else{
        return NULL;
    }
}

node *btree::search(int key){
    return search(key, root);
}


void btree::inorder_print(){
    inorder_print(root);
    cout << "\n";
}

void btree::inorder_print(node *leaf){
    if(leaf != NULL){
        inorder_print(leaf->left);
        cout << leaf->value << ",";
        inorder_print(leaf->right);
    }
}


int main(){

    //btree tree;
    btree *tree = new btree();

    ifstream input;
    // input.open("test.txt");
    input.open("kartosan.dat");
    if (!input)
    {
        std::cerr << "Could not open the file - '" << std::endl;
        return -1;
    }

    int N, a;
    input >> N;

    for (int i = 0; i < N; i++)
    {
        input >> a;
        tree->insert(a);
    }

    tree->inorder_print();

}
Input file looks like this: 7 2 9 15 38 -4 1 0

And output should look like this: -4 0 1 2 9 15 38

I am also still looking on different sites for possible answers


What I have tried:

C++
ofstream output("kartosan.rez");
    inorder_print(output);
    output.close();


i have tried different ways to put it in the main function
Posted
Updated 21-Mar-22 13:57pm

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

So start by looking at how you read a file, and find matching functions to write to a file instead. (Hint: if ifstream reads input from a file, what might output to a file?)

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Code and result look correct, see no problem. If something else comes out, something is wrong with the input file "kartosan.dat".

Here is code that seems unnecessary:
C++
if (key value) 
{ ...}
else if (key >= leaf->value) 
{ ...}

You also forgot:
C++
input.close();

There should also be other methods, e.g.:
C++
void btree::inorder_print(ofstream &ofile)
I would avoid the name output.

Something is wrong with the class definition.
The member variables should never be external be changeable. So something like this:
C++
class btree {
public:
	...
private:
	node *root;
	void insert(int key, node *leaf);
	node *search(int key, node *leaf);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900