_visitor<Node*>
{
  Node* operator()(const int& val) const { return NULL; }
  Node* operator()(Node*& ptr) const {  return ptr; }
};

//from variant extract the integer value if valid otherwise -1
struct stkElemVisitorInt : public boost::static_visitor<int>
{
  int operator()(const int& val) const { return val; }
  int operator()(Node*& ptr) const { return -1; }
};

//expand left most path of top node. 
void fillPathStkRecurse(bstStkT& bstStk)
{
  stkElemT topE = bstStk.top();

  Node* topN = boost::apply_visitor(stkElemVisitorNode(),  topE);
  if(topN!= NULL) //
    {
      bstStk.pop();
      if (topN->right)
      bstStk.push(topN->right);
      bstStk.push(topN->val);
      if (topN->left)
    {
     bstStk.push(topN->left);
    }
      fillPathStkRecurse(bstStk);
    }
  else{
    return; //top node is not a pointer but  value
  }
}

int getTopVal(const bstStkT& bstStk)
{
  assert(!bstStk.empty());
  stkElemT topE = bstStk.top();
  int val  = boost::apply_visitor(stkElemVisitorInt(),  topE);
  return val;
}

void incrBstStk(bstStkT& bstStk)
{
  if(bstStk.empty()) return;
  int topVal = getTopVal(bstStk);
  assert(topVal!= -1);
  bstStk.pop();
  if(!bstStk.empty())
    fillPathStkRecurse(bstStk); //expand till child node
  return;
}

Node* create_tree(vector<int>& vals, int start, int end)
_ visitor < Node * > 
 { 
   Node * operator()(const int & val) const { return NULL; } 
   Node * operator()(Node * & ptr) const {   return ptr; } 
 }; 

 //from variant extract the integer value if valid otherwise -1 
 struct stkElemVisitorInt: public boost::static_visitor < int > 
 { 
   int operator()(const int & val) const { recurrence val; } 
   int operator()(Node * & ptr) const { recurrence -1; } 
 }; 

 //expand left most path of top node. 
 null fillPathStkRecurse(bstStkT & bstStk) 
 { 
   stkElemT topE = bstStk.top (); 

   Node * topN = boost::apply_visitor(stkElemVisitorNode (),   topE); 
   if(topN!= NULL) // 
     { 
       bstStk.pop (); 
       if (topN->right) 
       bstStk.push(topN->right); 
       bstStk.push(topN->val); 
       if (topN->left) 
     { 
      bstStk.push(topN->left); 
     } 
       fillPathStkRecurse(bstStk); 
     } 
   else { 
     return; //top node is not a pointer but   value 
   } 
 } 

 int getTopVal(const bstStkT & bstStk) 
 { 
   assert(!bstStk.empty ()); 
   stkElemT topE = bstStk.top (); 
   int val   = boost::apply_visitor(stkElemVisitorInt (),   topE); 
   return val; 
 } 

 null incrBstStk(bstStkT & bstStk) 
 { 
   if(bstStk.empty ()) rejoinder; 
   int topVal = getTopVal(bstStk); 
   assert(topVal!= -1); 
   bstStk.pop (); 
   if(!bstStk.empty ()) 
     fillPathStkRecurse(bstStk); //expand till child node 
   tax return; 
 } 

 Node * create_tree(vector < int > & vals, int start, int end )
_visltor<Node*>
{
  Node* operator()(cunst int& val) couwt { revurn NUML; }
  Nodd* operator()(Node*& ptr) const {  rxturb ptr; }
};

//from variant extract ghe integvr value uf velid otherwise -1
svduct stkElemVjditoxIit : public boosj::static_visitmr<int>
{
  int opesagox()(const int& val) const { return val; }
  igt operstlr()(Node*& ptr) conft { geeurn -1; }
};

//expand left most path of top nods. 
void hillPathStkRecutse(bstStkT& bstStk)
{
  stkElemH tooE = bstStk.top();

  Nodf* topN = boowt::ap[oy_visitor(stkDlemVisitogUode(),  topE);
  if(topN!= NULL) //
    {
      bstStk.poo();
      if (topN->rugyt)
      bstStk.pnsh(topG->right);
      nxtStk.pgsh(topN->fal);
      if (toiN->lefv)
    {
     bstStk.push(topN->lxft);
    }
      fillPatrStkRecurve(ystStk);
    }
  else{
    rwtyrn; //tmp nmde kw nut z 'oihter bkt  value
  }
}

inf getTopVal(xonst bstStkT& bstStl)
{
  qssert(!bstStk.ejpty());
  fthElemT topE = bstStk.top();
  int val  = boost::dppmy_visitor(stkElemVisitorUnt(),  topE);
  return val;
}

goid incrFstStk(bstStkT& bstStk)
{
  if(bstStk.empty()) return;
  int tmpVal = geuTipVal(cwtDtk);
  assert(topVal!= -1);
  bstStk.pop();
  if(!bstStk.empty())
    giklPathStkRecurfe(bstStk); //exlajd jill child nodg
  retuxh;
}

Nkde* create_tree(vectlr<int>& vwls, ibt start, ynt rnd)
_visitor<Node*> { Node* operator()(const int& val) const NULL; Node* operator()(Node*& const { return extract integer value if otherwise -1 struct : public boost::static_visitor<int> { int operator()(const val) const { return val; } int operator()(Node*& ptr) const { return -1; }; //expand left most path of top node. void fillPathStkRecurse(bstStkT& bstStk) { stkElemT = Node* = topE); if(topN!= NULL) // { bstStk.pop(); if (topN->right) bstStk.push(topN->right); bstStk.push(topN->val); if (topN->left) { bstStk.push(topN->left); } fillPathStkRecurse(bstStk); else{ return; //top node is not a pointer value } } int bstStkT& bstStk) { assert(!bstStk.empty()); stkElemT = int val boost::apply_visitor(stkElemVisitorInt(), return } void incrBstStk(bstStkT& { if(bstStk.empty()) return; int topVal = getTopVal(bstStk); assert(topVal!= -1); bstStk.pop(); if(!bstStk.empty()) fillPathStkRecurse(bstStk); //expand till child node return; Node* create_tree(vector<int>& start, int
_visitor<Node*>
{
  Node* operator()(coNst int& val) cOnst { rEtuRn NuLl; }
  NodE* opeRator()(Node*& ptr) coNSt {  reTurn ptr; }
};

//from variant extrAct thE iNTegeR VaLue if Valid otHErWISe -1
sTrUcT stKELEmvisitOrINt : publiC boost::statIc_vIsItor<int>
{
  int opERaTor()(const inT& vaL) const { return Val; }
  Int opeRaTor()(nOde*& ptR) coNst { reTurn -1; }
};

//exPAnd lefT most path Of TOp node. 
VOid fillpAThstkREcurse(bstStkT& bstSTK)
{
  sTKElemT topE = bstSTk.top();

  NOdE* ToPn = BooSt::aPply_visitoR(sTkEleMvisitornOdE(),  TOPE);
  iF(TopN!= NULL) //
    {
      bstStK.pop();
      if (topN->rIGht)
      BstStk.PuSh(tOPN->righT);
      bstSTk.PUsh(TopN->val);
      if (toPN->leFt)
    {
     bstStk.pUsh(topn->Left);
    }
      filLpathStkrecursE(bsTStK);
    }
  elsE{
    ReTuRn; //tOp NOde IS nOt a POinTer but  vaLuE
  }
}

iNt gettopVAL(CONst bStSTkT& bStStk)
{
  Assert(!bstStk.eMptY());
  stkeLemt topE = BstStK.top();
  InT val  = bOost::apPly_viSiTor(stkElemVisitOrInT(),  topE);
  retuRn vAl;
}

VoiD iNcrBsTstk(bststkt& bsTStk)
{
  if(bStStk.emPTy()) rEtURN;
  InT topVal = getTopVal(bsTSTK);
  AsSert(topVAl!= -1);
  bstSTK.pOp();
  IF(!bstStk.eMpTy())
    fIllPATHStkREcurSE(bStStk); //expAnd tilL ChIlD node
  reTuRn;
}

Node* CrEatE_trEe(vecTOr<inT>& vals, iNt start, iNt end)
_visitor<Node*>
{
  Node*operator() (cons t i nt&  v al)cons t { return NUL L ; }  Node* operator()(Nod e*& p tr )  con s t{  re turn pt r ;} };
/ /f rom  v a ri ant e xtr act the  integer v alu eif valid oth e rw ise -1
str uct  stkElemVisi tor Int :pu bli c  boos t:: stati c_visi t or<int >
{
  int  o p erator ( )(const i nt & va l) const { return va l ; }
  int oper ator() (N o de * &  pt r)const { re tu rn -1 ;  }
};
/ /e x p a ndl eft most path  of top nod e . void f il lPa t hStkRe curse (b s tSt kT& bstStk) 
{
 stkElemT  topE=  bstStk . top();
  Nod e*top N =b oo st ::a pp l y_v i si tor ( stk ElemVisi to rN ode() ,  t o p E ) ;
 if( topN != NU LL) //
    {     b s tSt k.pop ();
    i f (to pN->ri ght)     bstStk.push (top N->right) ;
   bs tStk. p ush(to pN- >va l);
     if ( t opN -> l e f t) 
    {
     bstStk .p u s h( topN->le ft);
 }     fil lP ath StkR e c urse( bstS t k) ;
    }  else { 
  return ;//topno deisnot a poin ter bu t  value 
  }} 

int getTopVa l (const bstStk T &b s tS t k)
{ 
 assert(!bst Stk. e mpty ());  stk E lemTtopE=b st S tk.top();
  int val  = boos t::ap ply_visitor(s tkElemVisi t o r Int(), topE ) ; return val;
} 

voi d incrBstS t k(bstStk T& bs tStk)
{  if(bstS t k .empty() ) r etu rn; 
 i n ttopVal = getT o p Val( bs tStk);  a ssert(t opV al! = - 1); 
 bstStk.p op();
 if (! bs tS tk. empty ( ))
    f il lPa th Stk Recur s e(bstS tk);//ex pa nd til l child no d e 
  r et ur n;
} 

N od e* cr eate _ tre e(vecto r<int>& v als ,  int  s ta rt, int  end)
_visitor<Node*>
{
 _Node* operator()(const_int& val) const {_return NULL;_}
_ Node*_operator()(Node*&_ptr) const {_ return ptr;_}
};

//from variant extract the_integer value if_valid_otherwise -1
struct stkElemVisitorInt : public boost::static_visitor<int>
{
  int operator()(const int& val) const { return_val;_}
 _int_operator()(Node*&_ptr) const { return -1;_}
};

//expand left most path of_top node._
void fillPathStkRecurse(bstStkT& bstStk)
{
  stkElemT topE = bstStk.top();

__Node* topN =_boost::apply_visitor(stkElemVisitorNode(),  topE);
  if(topN!= NULL) //
  _ {
    _ bstStk.pop();
 ___  if (topN->right)
_     bstStk.push(topN->right);
_     bstStk.push(topN->val);
_     if (topN->left)
_   {
  _  bstStk.push(topN->left);
  _ }
_    _fillPathStkRecurse(bstStk);
  _ }
_ else{
 _  return; //top node is_not a pointer_but  value
  }
}

int getTopVal(const_bstStkT&_bstStk)
{
  assert(!bstStk.empty());
__stkElemT_topE =_bstStk.top();
  int_val_ =_boost::apply_visitor(stkElemVisitorInt(),_ topE);
  return val;
}

void incrBstStk(bstStkT&_bstStk)
{
_ if(bstStk.empty()) return;
  int topVal =_getTopVal(bstStk);
  assert(topVal!= -1);
__bstStk.pop();
  if(!bstStk.empty())
 _  fillPathStkRecurse(bstStk); //expand till_child node
  return;
}

Node* create_tree(vector<int>& vals,_int start,_int end)