codice:
#include "STree.hpp"
class CTree
{
private:
node tree;
int rad;
public:
CTree();
CTree(int n);
~CTree();
void add(int n);
int serch(int n);
};
CTree::CTree()
{
rad = 0;
tree = new STree(0, NULL, NULL);
}
// init tree
CTree::CTree(int n)
{
rad = n;
tree = new STree(n, NULL, NULL);
}
CTree::~CTree()
{
}
void CTree::add(int n)
{
node p = tree;
while( (p->l != NULL) && (p->r != NULL) )
{
if(n > rad)
{
p = p->l;
}
else
{
p = p->r;
}
}
if(p->l == NULL)
{
// ERRORE QUI
p->r = new STree(n, NULL, NULL);
}
else if(p->r == NULL)
{
// QUI
p->l = new STree(n, NULL, NULL);
}
}
int CTree::serch(int n)
{
node p = tree;
while( (p->l != NULL) && (p->r != NULL) && (p->item != n) )
{
if(n > rad)
{
p = p->l;
}
else
{
p = p->r;
}
}
return p->item;
}
Risolto