//---------------------------------------------------------------------------
#ifndef bufferstructH
  #define bufferstructH
//---------------------------------------------------------------------------
TBoolFnct * CreateHistory( TBoolFnct * fn );
void AddToHistory( TBoolFnct * fn, TPfnct * ff );
//---------------------------------------------------------------------------
class TermTree
{
private:
  struct TreeNode
  {
    TreeNode * on;
    TreeNode * off;
    TreeNode * dc;
    int value;
    TPfnct * ff;

    int SearchInterNode( char * iv, int pos, int n )
    {
      if ( pos == n ) return 1;
      if ( iv[pos] == '0' )
      {
        if ( this->on->SearchInterNode( iv, pos + 1, n ) == 0 ) return this->dc->SearchInterNode( iv, pos + 1, n );
        else
          return 1;
      }
      else if ( this->on->SearchInterNode( iv, pos + 1, n ) == 0 )
      {
        if ( this->off->SearchInterNode( iv, pos + 1, n ) == 0 )
          return this->dc->SearchInterNode( iv, pos + 1, n );
      }
      else
        return 1;
      return 0;
    }
  };
private:
  TreeNode * root;
  int inpvars;
  long nodes;
  long items;

  TreeNode * AddTreeNode();
  void DestroyTreeNode( TreeNode * tn );
  TreeNode * ValAddToTreeHistory( char * t, int val );
public:
  TermTree( TBoolFnct * fn );
  TermTree( int iv );

  ~TermTree()
  {
    DestroyTreeNode( this->root );
  };

  TreeNode * ValAddToTreeHistory( TPfnct * ff, int val );
  int AddToTreeHistory( TPfnct * ff );
  int AddToTreeHistory( char * t );
  int IsInTreeHistory( TPfnct * ff );
  TPfnct * IsTermInTreeHistory2( char * t );
  void DeleteTermFromTreeHistory( char * t );
  int IsInTreeHistory( char * t );
  int AddToTreeHistory2( char * t );
  TreeNode * GetRoot()
  {
    return root;
  };
  TPfnct * AddTermToTreeHistory( TPfnct * ff );
};
//---------------------------------------------------------------------------
#endif
