existence19 0 Newbie Poster

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

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.

Here is my binary tree class and questions i have:

#ifndef TREENODE_H
#define	TREENODE_H

#include"stack.h"
#include<iostream>
#include<fstream>

using namespace std;




template<class type>
class binaryTree
{
    struct treeNode {

        type data;
        binaryTree<type> *left;
        binaryTree<type> *right;

    };

    treeNode * root;

    void inOrder(treeNode *tnode);

    void preOrder(treeNode *tnode);

    void postOrder(treeNode *tnode);

    void destroy(treeNode *&tnode);


public:

    binaryTree();

    binaryTree(type data);

    binaryTree(type data, binaryTree<type> , binaryTree<type>);

    bool isEmpty() const;

    void inOrder();

    void postOrder();

    void preOrder();

    void destroy();

    ~binaryTree();

};

template<class type>
binaryTree<type>::binaryTree()
{
    root = NULL;
}

template<class type>
binaryTree<type>::binaryTree(type data)
{
    root = new treeNode;
    root->data = data;
    root->left = NULL;
    root->right = NULL;
}

template<class type>
binaryTree<type>::binaryTree(type data, binaryTree<type> leftSub, binaryTree<type> rightSub)
{
    root = new treeNode;
    root->data=data;

    root->left=&leftSub;

    root->right=&rightSub;
}

template<class type>
bool binaryTree<type>::isEmpty() const
{
    return (root==NULL);
}

template<class type>
void binaryTree<type>::inOrder()
{
    inOrder(root);

}

template<class type>
void binaryTree<type>::preOrder()
{
    preOrder(root);

}

template<class type>
void binaryTree<type>::postOrder()
{
    postOrder(root);

}

template<class type>
void binaryTree<type>::inOrder(treeNode *tnode)
{

    if(tnode==NULL)
        return;

    if(tnode->left!=NULL)
        inOrder(tnode->left->root);
    cout << tnode->data << " ";

    if(tnode->right!=NULL)
        inOrder(tnode->right->root);

}

template<class type>
void binaryTree<type>::preOrder(treeNode* tnode)
{
    if(tnode == NULL)
        return;

    cout << tnode->data << " ";

    if(tnode->left!=NULL)
        preOrder(tnode->left->root);

    if(tnode->right!=NULL)
        preOrder(tnode->right->root);

}

template<class type>
void binaryTree<type>::postOrder(treeNode* tnode)
{
    if(tnode == NULL)
        return;

    if(tnode->left!=NULL)
        postOrder(tnode->left->root);

    if(tnode->right!=NULL)
        postOrder(tnode->right->root);

    cout << tnode->data << " ";
}

template<class type>
void binaryTree<type>::destroy()
{
    destroy(root);
}

template<class type>
void binaryTree<type>::destroy(treeNode*& tnode)
{
    if(tnode == NULL)
        return;



    if(tnode->left!=NULL)
        destroy(tnode->left->root);

    if(tnode->right!=NULL)
        destroy(tnode->right->root);
    delete tnode;
    tnode = NULL;
}

template<class type>
binaryTree<type>::~binaryTree()
{
    destroy(root);
}

what my problems are now:

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.

Please someone i really need help...

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.