template<class type>
class BinaryTree{
private:
struct Tree_Node{
type Info;
BinaryTree<type> *left;
BinaryTree<type> *right;
};
Tree_Node *root;
public:
/*Creates and empty Binary Tree.
Children are set to NULL
*/
BinaryTree();
/*Creates a Binary Tree with only one node;
the other points to NULL
*/
BinaryTree(const type & element);
BinaryTree(const type & a, const type & b, const type & c);
};
template<class type>
BinaryTree<type>::BinaryTree(){
root=NULL;
node<type> *temp;
temp=new node<type>;
temp->data=*this;
}
template<class type>
BinaryTree<type>::BinaryTree(const type & element){
Tree_Node *temp;
temp=new Tree_Node;
temp->Info=element;
temp->left=NULL;
temp->right=NULL;
root=temp;
}
i have stack.h included dont worry just didnt paste it :b
the default constructor creates an empty tree and pushes it onto stack. just wondering if the "*this" assignment would create a tree and push it onto the stack or if this is logically incorect
The default constructor is incorrect. You create a local variable (temp) inside the constructor that will be destructed at the end of the function, causing the allocated memory to be inaccessible (memory leak).
Furthermore, inside the ctor you don't have a stack object to begin with, so it's impossible to push the tree into it, unless you create it locally (but then it would be destructed at the end of the function).
just wondering if the "*this" assignment would create a tree
It will most likely cause a compilation error. Let's say you have a BinaryTree<int>, then you create a node<int> with a int data member and try to assign a BinaryTree<int> to an int variable.
It's better to make a separate function to read the top of the stack.
You could return the data by value, but that creates copies even in cases where you don't need them after popping. If you store large data structures it is quite inefficient.