0

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
     }  
     
}
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by timb89
0

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.

Edited by firstPerson: n/a

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.