im trying to write the post order print out of a preorder binary tree reading into a text file though everything i have attempted to do so far as resulted in an error.

ive tried reading the preorder into stacks, queues, arrays, and the best ive been able to come up with is errors or only the first element of the BST being added.

does anyone have any ideas how to implement this?

void printOutText(BST *p)
{
     ofstream myfile;
     myfile.open ("example.txt");     
     
     preorder(p);
     
     myfile.close();
     

}

void preorder(BST *p)
{       
     if (p != NULL) 
     {
        cout << p -> data;
        preorder(p->left);  // print left subtree
        preorder(p->right); // print right subtree
     }  
     
}

So if I get this straight, you have a file with "pre-order" data. And your goal is to print out the data in a post order manner correct? If so then, what you could do is, take the pre-order data, store it in an array, call it PREARRAY. Then construct a tree TREE, such that preorder(TREE) = PREARRAY. Then all you would have to do now is print the TREE in postorder.

So if I get this straight, you have a file with "pre-order" data. And your goal is to print out the data in a post order manner correct? If so then, what you could do is, take the pre-order data, store it in an array, call it PREARRAY. Then construct a tree TREE, such that preorder(TREE) = PREARRAY. Then all you would have to do now is print the TREE in postorder.

i had alot of trouble getting the array to store using a pointer? so i could store in array and print out in the first void function. i kept getting errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.