SORRY I TRIED TO INDENT THE CODE BUT IT DIDNT WORK OUT
I am trying to make a binary tree for conversion of postfix to infix. the binary trees class is as follows
template<class type>
class binaryTree
{
struct treeNode
{
type data;
binaryTree<type> *left;
binaryTree<type> *right;
WHAT I HAVE TO DO AND WHAT I AN HAVING TROUBLE WITH
1) READ A POSTFIX EXPRESSION FROM A FILE AND MAKE A TREE. I HAVE TO DO THAT BY THE STACK METHOD.
**** I KNOW HOW TO WRITE THE CODE AND THE ALGORITHM BUT I AM CONFUSED.. I KNOW THAT I MAKE A STACK OF TYPE binaryTree and then write the algorithm. I don't know WHERE DO I READ FROM THE FILE, INSIDE OR OUTSIDE THE CLASS? WHENEVER I TRY TO MAKE A STACK<binaryTree> in the main file i get an error
2) i think i have a problem with the CONSTRUCTORS. THIS IS WHERE I GET A RUN TIME ERROR AND THE PROGRAM FAILS TO RUN. SUPPOSE I MAKE A function read file() in the class binaryTree
stack<binaryTree> treeStack;
treeNode * n;
n=new treeNode;
n->data='a';
binaryTree<char> b;
binaryTree<char> c(n); // this line gives an error on run and the program fails
I don't know why this constructor is not working. PLEASEEEEEEE HELPPPPPPPPPP.
ALSO TELL ME IF THE OTHER CONSTRUCTOR IS FINE. ANY OTHER SUGGESTIONS/ADVICE ARE WELCOME
Hey.. thanks for t he help but i don't think that the destructor would work. i would recursively have to delete every node.
I am still stuck please help. i finally had my logic going and have made changes to my code. here is the new code.
1) my destructor and destroy() are still giving problems..
2)inorder and everything else works but for some cases they do not. i think they are the destructor dependent though. they seem to be working when i don't use the destructor and the destroy function. example: something like this always works when i comment out the destructor and destroy..
binaryTree<char> a('a');
binaryTree<char> b('b');
binaryTree<char> c('+',a,b);
binaryTree<char> d('+',b,c);
binaryTree<char> e('*',a,d);
binaryTree<char> f('z',final,d);
f.inOrder();
but when i destroy a tree object and use it later in order seems to give problems.
3)my final question and most important.. how do i store the popped tree in another tree object. for example :
stack<binaryTree> s; // consider the stack has some objects
binaryTree tree;
tree=s.pop() gives a malloc error and run time error.
I see your problem: You're trying to build a tree with local variables, which does not make sense. The pointer to such an object is invalid as soon as the local goes out of scope (the function they're in ends).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
template<class type>
binaryTree<type>::binaryTree(type data, binaryTree<type> leftSub, binaryTree<type> rightSub) // leftSub and rightSub are local variables to 'binaryTree()' and it is passed as a copy i.e. it copies the pointer of 'root'
{
root = new treeNode;
root->data=data;
root->left=&leftSub; // this is a pointer to a local variable
root->right=&rightSub; // this too
} // at this point leftSub and rightSub don't exist anymore (and 'root' of the original passed variables is destroyed) but root->left and root->right are still hold the pointer to those now invalid local variables
...
binaryTree<char> c('+',a,b); // This passes 'a' and 'b' as copies. After the constructor of 'c' is done. 'root' of 'a' and 'b' is not valid anymore because 'leftSub' and 'rightSub' destroyed 'root'
...
There're 2 ways to solve your problem: Either work only with pointers (binaryTree<char> *a = new binaryTree<char>('a');) or copy the content and not the pointer of 'root' when you pass the variables.
I recommend using just pointer and then my suggestion of the destructor/destroy will work.