hi, i m havin problems with this TAD for the binary tree
#ifndef __BINTREE__H__
#define __BINTREE__H__
#include <queue>
#include <iostream>
using namespace std;
template< class T >
class BinTree
{
public:
typedef BinTree Self;
private:
class Node
{
public:
typedef Node _Self;
private:
T m_Data;
_Self* m_Left;
_Self* m_Right;
public:
Node( )
: m_Left( NULL ),
m_Right( NULL )
{
}
Node( const _Self& n )
: m_Data( n.m_Data )
{
if( n.m_Left != NULL )
{
_Self* nl = new _Self( n.m_Left );
this->m_Left = nl;
}
else
this->m_Left = NULL;
if( n.m_Right != NULL )
{
_Self* nl = new _Self( n.m_Right );
this->m_Right = nl;
}
else
this->m_Right = NULL;
}
virtual ~Node( )
{
if( this->m_Left != NULL )
delete this->m_Left;
if( this->m_Right != NULL )
delete this->m_Right;
}
const T& getData( ) const
{
return( this->m_Data );
}
_Self* getLeft( )
{
return( this->m_Left );
}
_Self* getRight( )
{
return( this->m_Right );
}
unsigned int height( ) const
{
unsigned int l = 0, r = 0;
if( this->m_Left != NULL )
l = this->m_Left->height( );
if( this->m_Right != NULL )
r = this->m_Right->height( );
return( 1 + ( ( l > r )? l: r ) );
}
unsigned int weight( ) const
{
unsigned int l = 0, r = 0;
if( this->m_Left != NULL )
l = this->m_Left->weight( );
if( this->m_Right != NULL )
r = this->m_Right->weight( );
return( 1 + l + r );
}
void setData( const T& v )
{
this->m_Data = v;//ERROR
}
void setLeft( _Self* l )
{
this->m_Left = l;
}
void setRight( _Self* l )
{
this->m_Right = l;
}
void PrintPreOrder( )
{
std::cout << this->m_Data << " ";
if( this->m_Left != NULL )
this->m_Left->PrintPreOrder( );
if( this->m_Right != NULL )
this->m_Right->PrintPreOrder( );
}
void PrintInOrder( )
{
if( this->m_Left != NULL )
this->m_Left->PrintInOrder( );
std::cout << this->m_Data << " ";
if( this->m_Right != NULL )
this->m_Right->PrintInOrder( );
}
void PrintPostOrder( )
{
if( this->m_Left != NULL )
this->m_Left->PrintPostOrder( );
if( this->m_Right != NULL )
this->m_Right->PrintPostOrder( );
std::cout << this->m_Data << " ";
}
void PrintLevels( )
{
std::queue< _Self* > q;
q.push( this );
while( !q.empty( ) )
{
_Self* act = q.front( );
q.pop( );
std::cout << act->m_Data << " ";
if( act->m_Left != NULL )
q.push( act->m_Left );
if( act->m_Right != NULL )
q.push( act->m_Right );
} // elihw
}
};
private:
Node* m_Root;
public:
BinTree( )
: m_Root( NULL )
{
}
virtual ~BinTree( )
{
if( this->m_Root )
delete this->m_Root;
}
const T& getData( ) const
{
return( this->m_Root->getData( ) );
}
void setData(const T& v)
{
this->m_Root->setData(v);
}
const Self getLeft( ) const
{
Self b;
b.m_Root = this->m_Root->getLeft( );
return( b );
}
void setLeft(BinTree l)
{
this->m_Root->setLeft(l.m_Root);
}
const Self getRight( ) const
{
Self b;
b.m_Root = this->m_Root->getRight( );
return( b );
}
void setRight(BinTree r)
{
this->m_Root->setRight(r.m_Root);
}
void Insert(const T& e)
{
if(this->m_Root==NULL)
{
this->setData(e);
}
else
{
if(this->getData()>e)
{
if(this->getLeft().m_Root != NULL)
this->getLeft().Insert(e);
else
{
Self a;
a.setData(e);
this->setLeft(a);
}
}
else
{
if(this->getRight().m_Root != NULL)
this->getRight().Insert(e);
else
{
Self a;
a.setData(e);
this->setRight(a);
}
}
}
}
};
#endif // __BINTREE__H__
// eof
every time i insert the a new data for the tree (when the tree is empty) the assigned data to m_Data in Node* wont work, any ideas why?
Can you please edit your post and use the code formatting tag on the right to format your code. I find it unreadable as is.